/
erl_bif_trace.c
2359 lines (2149 loc) · 60 KB
/
erl_bif_trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1999-2013. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Trace BIFs.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#include "erl_driver.h"
#include "bif.h"
#include "big.h"
#include "dist.h"
#include "erl_version.h"
#include "beam_bp.h"
#include "erl_binary.h"
#include "erl_thr_progress.h"
#define DECL_AM(S) Eterm AM_ ## S = am_atom_put(#S, sizeof(#S) - 1)
const struct trace_pattern_flags erts_trace_pattern_flags_off = {0, 0, 0, 0, 0};
/*
* The following variables are protected by code write permission.
*/
static int erts_default_trace_pattern_is_on;
static Binary *erts_default_match_spec;
static Binary *erts_default_meta_match_spec;
static struct trace_pattern_flags erts_default_trace_pattern_flags;
static Eterm erts_default_meta_tracer_pid;
static struct { /* Protected by code write permission */
int current;
int install;
int local;
BpFunctions f; /* Local functions */
BpFunctions e; /* Export entries */
#ifdef ERTS_SMP
Process* stager;
ErtsThrPrgrLaterOp lop;
#endif
} finish_bp;
static Eterm
trace_pattern(Process* p, Eterm MFA, Eterm Pattern, Eterm flaglist);
#ifdef ERTS_SMP
static void smp_bp_finisher(void* arg);
#endif
static BIF_RETTYPE
system_monitor(Process *p, Eterm monitor_pid, Eterm list);
static void new_seq_trace_token(Process* p); /* help func for seq_trace_2*/
static int already_traced(Process *p, Process *tracee_p, Eterm tracer);
static int port_already_traced(Process *p, Port *tracee_port, Eterm tracer);
static Eterm trace_info_pid(Process* p, Eterm pid_spec, Eterm key);
static Eterm trace_info_func(Process* p, Eterm pid_spec, Eterm key);
static Eterm trace_info_on_load(Process* p, Eterm key);
static void reset_bif_trace(void);
static void setup_bif_trace(void);
static void install_exp_breakpoints(BpFunctions* f);
static void uninstall_exp_breakpoints(BpFunctions* f);
static void clean_export_entries(BpFunctions* f);
void
erts_bif_trace_init(void)
{
erts_default_trace_pattern_is_on = 0;
erts_default_match_spec = NULL;
erts_default_meta_match_spec = NULL;
erts_default_trace_pattern_flags = erts_trace_pattern_flags_off;
erts_default_meta_tracer_pid = NIL;
}
/*
* Turn on/off call tracing for the given function(s).
*/
Eterm
trace_pattern_2(BIF_ALIST_2)
{
return trace_pattern(BIF_P, BIF_ARG_1, BIF_ARG_2, NIL);
}
Eterm
trace_pattern_3(BIF_ALIST_3)
{
return trace_pattern(BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3);
}
static Eterm
trace_pattern(Process* p, Eterm MFA, Eterm Pattern, Eterm flaglist)
{
DeclareTmpHeap(mfa,3,p); /* Not really heap here, but might be when setting pattern */
int i;
int matches = -1;
int specified = 0;
enum erts_break_op on;
Binary* match_prog_set;
Eterm l;
struct trace_pattern_flags flags = erts_trace_pattern_flags_off;
int is_global;
Process *meta_tracer_proc = p;
Eterm meta_tracer_pid = p->common.id;
if (!erts_try_seize_code_write_permission(p)) {
ERTS_BIF_YIELD3(bif_export[BIF_trace_pattern_3], p, MFA, Pattern, flaglist);
}
finish_bp.current = -1;
UseTmpHeap(3,p);
/*
* Check and compile the match specification.
*/
if (Pattern == am_false) {
match_prog_set = NULL;
on = 0;
} else if (is_nil(Pattern) || Pattern == am_true) {
match_prog_set = NULL;
on = 1;
} else if (Pattern == am_restart) {
match_prog_set = NULL;
on = erts_break_reset;
} else if (Pattern == am_pause) {
match_prog_set = NULL;
on = erts_break_stop;
} else if ((match_prog_set = erts_match_set_compile(p, Pattern)) != NULL) {
MatchSetRef(match_prog_set);
on = 1;
} else{
goto error;
}
is_global = 0;
for(l = flaglist; is_list(l); l = CDR(list_val(l))) {
if (is_tuple(CAR(list_val(l)))) {
Eterm *tp = tuple_val(CAR(list_val(l)));
if (arityval(tp[0]) != 2 || tp[1] != am_meta) {
goto error;
}
meta_tracer_pid = tp[2];
if (is_internal_pid(meta_tracer_pid)) {
meta_tracer_proc = erts_pid2proc(NULL, 0, meta_tracer_pid, 0);
if (!meta_tracer_proc) {
goto error;
}
} else if (is_internal_port(meta_tracer_pid)) {
Port *meta_tracer_port;
meta_tracer_proc = NULL;
meta_tracer_port = (erts_port_lookup(
meta_tracer_pid,
ERTS_PORT_SFLGS_INVALID_TRACER_LOOKUP));
if (!meta_tracer_port)
goto error;
} else {
goto error;
}
if (is_global) {
goto error;
}
flags.breakpoint = 1;
flags.meta = 1;
} else {
switch (CAR(list_val(l))) {
case am_local:
if (is_global) {
goto error;
}
flags.breakpoint = 1;
flags.local = 1;
break;
case am_meta:
if (is_global) {
goto error;
}
flags.breakpoint = 1;
flags.meta = 1;
break;
case am_global:
if (flags.breakpoint) {
goto error;
}
is_global = !0;
break;
case am_call_count:
if (is_global) {
goto error;
}
flags.breakpoint = 1;
flags.call_count = 1;
break;
case am_call_time:
if (is_global) {
goto error;
}
flags.breakpoint = 1;
flags.call_time = 1;
break;
default:
goto error;
}
}
}
if (l != NIL) {
goto error;
}
if (match_prog_set && !flags.local && !flags.meta && (flags.call_count || flags.call_time)) {
/* A match prog is not allowed with just call_count or call_time*/
goto error;
}
/*
* Check the MFA specification.
*/
if (MFA == am_on_load) {
if (flags.local || (! flags.breakpoint)) {
MatchSetUnref(erts_default_match_spec);
erts_default_match_spec = match_prog_set;
MatchSetRef(erts_default_match_spec);
}
if (flags.meta) {
MatchSetUnref(erts_default_meta_match_spec);
erts_default_meta_match_spec = match_prog_set;
MatchSetRef(erts_default_meta_match_spec);
erts_default_meta_tracer_pid = meta_tracer_pid;
if (meta_tracer_proc) {
ERTS_TRACE_FLAGS(meta_tracer_proc) |= F_TRACER;
}
} else if (! flags.breakpoint) {
MatchSetUnref(erts_default_meta_match_spec);
erts_default_meta_match_spec = NULL;
erts_default_meta_tracer_pid = NIL;
}
if (erts_default_trace_pattern_flags.breakpoint &&
flags.breakpoint) {
/* Breakpoint trace -> breakpoint trace */
ASSERT(erts_default_trace_pattern_is_on);
if (on) {
erts_default_trace_pattern_flags.local
|= flags.local;
erts_default_trace_pattern_flags.meta
|= flags.meta;
erts_default_trace_pattern_flags.call_count
|= (on == 1) ? flags.call_count : 0;
erts_default_trace_pattern_flags.call_time
|= (on == 1) ? flags.call_time : 0;
} else {
erts_default_trace_pattern_flags.local
&= ~flags.local;
erts_default_trace_pattern_flags.meta
&= ~flags.meta;
erts_default_trace_pattern_flags.call_count
&= ~flags.call_count;
erts_default_trace_pattern_flags.call_time
&= ~flags.call_time;
if (! (erts_default_trace_pattern_flags.breakpoint =
erts_default_trace_pattern_flags.local |
erts_default_trace_pattern_flags.meta |
erts_default_trace_pattern_flags.call_count |
erts_default_trace_pattern_flags.call_time)) {
erts_default_trace_pattern_is_on = !!on; /* i.e off */
}
}
} else if (! erts_default_trace_pattern_flags.breakpoint &&
! flags.breakpoint) {
/* Global call trace -> global call trace */
erts_default_trace_pattern_is_on = !!on;
} else if (erts_default_trace_pattern_flags.breakpoint &&
! flags.breakpoint) {
/* Breakpoint trace -> global call trace */
if (on) {
erts_default_trace_pattern_flags = flags; /* Struct copy */
erts_default_trace_pattern_is_on = !!on;
}
} else {
ASSERT(! erts_default_trace_pattern_flags.breakpoint &&
flags.breakpoint);
/* Global call trace -> breakpoint trace */
if (on) {
if (on != 1) {
flags.call_count = 0;
flags.call_time = 0;
}
flags.breakpoint = flags.local | flags.meta | flags.call_count | flags.call_time;
erts_default_trace_pattern_flags = flags; /* Struct copy */
erts_default_trace_pattern_is_on = !!flags.breakpoint;
}
}
matches = 0;
} else if (is_tuple(MFA)) {
Eterm *tp = tuple_val(MFA);
if (tp[0] != make_arityval(3)) {
goto error;
}
mfa[0] = tp[1];
mfa[1] = tp[2];
mfa[2] = tp[3];
if (!is_atom(mfa[0]) || !is_atom(mfa[1]) ||
(!is_small(mfa[2]) && mfa[2] != am_Underscore)) {
goto error;
}
for (i = 0; i < 3 && mfa[i] != am_Underscore; i++, specified++) {
/* Empty loop body */
}
for (i = specified; i < 3; i++) {
if (mfa[i] != am_Underscore) {
goto error;
}
}
if (is_small(mfa[2])) {
mfa[2] = signed_val(mfa[2]);
}
if (meta_tracer_proc) {
ERTS_TRACE_FLAGS(meta_tracer_proc) |= F_TRACER;
}
matches = erts_set_trace_pattern(p, mfa, specified,
match_prog_set, match_prog_set,
on, flags, meta_tracer_pid, 0);
}
error:
MatchSetUnref(match_prog_set);
UnUseTmpHeap(3,p);
#ifdef ERTS_SMP
if (finish_bp.current >= 0) {
ASSERT(matches >= 0);
ASSERT(finish_bp.stager == NULL);
finish_bp.stager = p;
erts_schedule_thr_prgr_later_op(smp_bp_finisher, NULL, &finish_bp.lop);
erts_smp_proc_inc_refc(p);
erts_suspend(p, ERTS_PROC_LOCK_MAIN, NULL);
ERTS_BIF_YIELD_RETURN(p, make_small(matches));
}
#endif
erts_release_code_write_permission();
if (matches >= 0) {
return make_small(matches);
}
else {
BIF_ERROR(p, BADARG);
}
}
#ifdef ERTS_SMP
static void smp_bp_finisher(void* null)
{
if (erts_finish_breakpointing()) { /* Not done */
/* Arrange for being called again */
erts_schedule_thr_prgr_later_op(smp_bp_finisher, NULL, &finish_bp.lop);
}
else { /* Done */
Process* p = finish_bp.stager;
#ifdef DEBUG
finish_bp.stager = NULL;
#endif
erts_release_code_write_permission();
erts_smp_proc_lock(p, ERTS_PROC_LOCK_STATUS);
if (!ERTS_PROC_IS_EXITING(p)) {
erts_resume(p, ERTS_PROC_LOCK_STATUS);
}
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_STATUS);
erts_smp_proc_dec_refc(p);
}
}
#endif /* ERTS_SMP */
void
erts_get_default_trace_pattern(int *trace_pattern_is_on,
Binary **match_spec,
Binary **meta_match_spec,
struct trace_pattern_flags *trace_pattern_flags,
Eterm *meta_tracer_pid)
{
ERTS_SMP_LC_ASSERT(erts_has_code_write_permission() ||
erts_smp_thr_progress_is_blocking());
if (trace_pattern_is_on)
*trace_pattern_is_on = erts_default_trace_pattern_is_on;
if (match_spec)
*match_spec = erts_default_match_spec;
if (meta_match_spec)
*meta_match_spec = erts_default_meta_match_spec;
if (trace_pattern_flags)
*trace_pattern_flags = erts_default_trace_pattern_flags;
if (meta_tracer_pid)
*meta_tracer_pid = erts_default_meta_tracer_pid;
}
int erts_is_default_trace_enabled(void)
{
ERTS_SMP_LC_ASSERT(erts_has_code_write_permission() ||
erts_smp_thr_progress_is_blocking());
return erts_default_trace_pattern_is_on;
}
Uint
erts_trace_flag2bit(Eterm flag)
{
switch (flag) {
case am_all: return TRACEE_FLAGS;
case am_send: return F_TRACE_SEND;
case am_receive: return F_TRACE_RECEIVE;
case am_set_on_spawn: return F_TRACE_SOS;
case am_procs: return F_TRACE_PROCS;
case am_set_on_first_spawn: return F_TRACE_SOS1;
case am_set_on_link: return F_TRACE_SOL;
case am_set_on_first_link: return F_TRACE_SOL1;
case am_timestamp: return F_TIMESTAMP;
case am_running: return F_TRACE_SCHED;
case am_exiting: return F_TRACE_SCHED_EXIT;
case am_garbage_collection: return F_TRACE_GC;
case am_call: return F_TRACE_CALLS;
case am_arity: return F_TRACE_ARITY_ONLY;
case am_return_to: return F_TRACE_RETURN_TO;
case am_silent: return F_TRACE_SILENT;
case am_scheduler_id: return F_TRACE_SCHED_NO;
case am_running_ports: return F_TRACE_SCHED_PORTS;
case am_running_procs: return F_TRACE_SCHED_PROCS;
case am_ports: return F_TRACE_PORTS;
default: return 0;
}
}
/* Scan the argument list and sort out the trace flags.
**
** Returns !0 on success, 0 on failure.
**
** Sets the result variables on success, if their flags has
** occurred in the argument list.
*/
int
erts_trace_flags(Eterm List,
Uint *pMask, Eterm *pTracer, int *pCpuTimestamp)
{
Eterm list = List;
Uint mask = 0;
Eterm tracer = NIL;
int cpu_timestamp = 0;
while (is_list(list)) {
Uint bit;
Eterm item = CAR(list_val(list));
if (is_atom(item) && (bit = erts_trace_flag2bit(item))) {
mask |= bit;
#ifdef HAVE_ERTS_NOW_CPU
} else if (item == am_cpu_timestamp) {
cpu_timestamp = !0;
#endif
} else if (is_tuple(item)) {
Eterm* tp = tuple_val(item);
if (arityval(tp[0]) != 2 || tp[1] != am_tracer) goto error;
if (is_internal_pid(tp[2]) || is_internal_port(tp[2])) {
tracer = tp[2];
} else goto error;
} else goto error;
list = CDR(list_val(list));
}
if (is_not_nil(list)) goto error;
if (pMask && mask) *pMask = mask;
if (pTracer && tracer != NIL) *pTracer = tracer;
if (pCpuTimestamp && cpu_timestamp) *pCpuTimestamp = cpu_timestamp;
return !0;
error:
return 0;
}
Eterm trace_3(BIF_ALIST_3)
{
Process* p = BIF_P;
Eterm pid_spec = BIF_ARG_1;
Eterm how = BIF_ARG_2;
Eterm list = BIF_ARG_3;
int on;
Eterm tracer = NIL;
int matches = 0;
Uint mask = 0;
int cpu_ts = 0;
#ifdef ERTS_SMP
int system_blocked = 0;
#endif
if (! erts_trace_flags(list, &mask, &tracer, &cpu_ts)) {
BIF_ERROR(p, BADARG);
}
if (!erts_try_seize_code_write_permission(BIF_P)) {
ERTS_BIF_YIELD3(bif_export[BIF_trace_3], BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3);
}
if (is_nil(tracer) || is_internal_pid(tracer)) {
Process *tracer_proc = erts_pid2proc(p,
ERTS_PROC_LOCK_MAIN,
is_nil(tracer) ? p->common.id : tracer,
ERTS_PROC_LOCKS_ALL);
if (!tracer_proc)
goto error;
ERTS_TRACE_FLAGS(tracer_proc) |= F_TRACER;
erts_smp_proc_unlock(tracer_proc,
(tracer_proc == p
? ERTS_PROC_LOCKS_ALL_MINOR
: ERTS_PROC_LOCKS_ALL));
} else if (is_internal_port(tracer)) {
Port *tracer_port = erts_id2port_sflgs(tracer,
p,
ERTS_PROC_LOCK_MAIN,
ERTS_PORT_SFLGS_INVALID_TRACER_LOOKUP);
if (!tracer_port)
goto error;
ERTS_TRACE_FLAGS(tracer_port) |= F_TRACER;
erts_port_release(tracer_port);
} else
goto error;
switch (how) {
case am_false:
on = 0;
break;
case am_true:
on = 1;
if (is_nil(tracer))
tracer = p->common.id;
break;
default:
goto error;
}
/*
* Set/reset the call trace flag for the given Pids.
*/
if (is_port(pid_spec)) {
Port *tracee_port;
#ifdef HAVE_ERTS_NOW_CPU
if (cpu_ts) {
goto error;
}
#endif
if (pid_spec == tracer)
goto error;
tracee_port = erts_id2port_sflgs(pid_spec,
p,
ERTS_PROC_LOCK_MAIN,
ERTS_PORT_SFLGS_INVALID_LOOKUP);
if (!tracee_port)
goto error;
if (tracer != NIL && port_already_traced(p, tracee_port, tracer)) {
erts_port_release(tracee_port);
goto already_traced;
}
if (on)
ERTS_TRACE_FLAGS(tracee_port) |= mask;
else
ERTS_TRACE_FLAGS(tracee_port) &= ~mask;
if (!ERTS_TRACE_FLAGS(tracee_port))
ERTS_TRACER_PROC(tracee_port) = NIL;
else if (tracer != NIL)
ERTS_TRACER_PROC(tracee_port) = tracer;
erts_port_release(tracee_port);
matches = 1;
} else if (is_pid(pid_spec)) {
Process *tracee_p;
#ifdef HAVE_ERTS_NOW_CPU
if (cpu_ts) {
goto error;
}
#endif
/* Check that the tracee is not dead, not tracing
* and not about to be tracing.
*/
if (pid_spec == tracer)
goto error;
tracee_p = erts_pid2proc(p, ERTS_PROC_LOCK_MAIN,
pid_spec, ERTS_PROC_LOCKS_ALL);
if (!tracee_p)
goto error;
if (tracer != NIL && already_traced(p, tracee_p, tracer)) {
erts_smp_proc_unlock(tracee_p,
(tracee_p == p
? ERTS_PROC_LOCKS_ALL_MINOR
: ERTS_PROC_LOCKS_ALL));
goto already_traced;
}
if (on)
ERTS_TRACE_FLAGS(tracee_p) |= mask;
else
ERTS_TRACE_FLAGS(tracee_p) &= ~mask;
if ((ERTS_TRACE_FLAGS(tracee_p) & TRACEE_FLAGS) == 0)
ERTS_TRACER_PROC(tracee_p) = NIL;
else if (tracer != NIL)
ERTS_TRACER_PROC(tracee_p) = tracer;
erts_smp_proc_unlock(tracee_p,
(tracee_p == p
? ERTS_PROC_LOCKS_ALL_MINOR
: ERTS_PROC_LOCKS_ALL));
matches = 1;
} else {
int ok = 0;
#ifdef HAVE_ERTS_NOW_CPU
if (cpu_ts) {
if (pid_spec == am_all) {
if (on) {
if (!erts_cpu_timestamp) {
#ifdef HAVE_CLOCK_GETTIME
/*
Perhaps clock_gettime was found during config
on a different machine than this. We check
if it works here and now, then don't bother
about checking return value for error later.
*/
{
SysCpuTime start, stop;
SysTimespec tp;
int i;
if (sys_get_proc_cputime(start, tp) < 0)
goto error;
start = ((SysCpuTime)tp.tv_sec * 1000000000LL) +
(SysCpuTime)tp.tv_nsec;
for (i = 0; i < 100; i++)
sys_get_proc_cputime(stop, tp);
stop = ((SysCpuTime)tp.tv_sec * 1000000000LL) +
(SysCpuTime)tp.tv_nsec;
if (start == 0) goto error;
if (start == stop) goto error;
}
#else /* HAVE_GETHRVTIME */
if (erts_start_now_cpu() < 0) {
goto error;
}
#endif /* HAVE_CLOCK_GETTIME */
erts_cpu_timestamp = !0;
}
}
} else {
goto error;
}
}
#endif
if (pid_spec == am_all || pid_spec == am_existing) {
int i;
int procs = 0;
int ports = 0;
int mods = 0;
if (mask & (ERTS_PROC_TRACEE_FLAGS & ~ERTS_TRACEE_MODIFIER_FLAGS))
procs = 1;
if (mask & (ERTS_PORT_TRACEE_FLAGS & ~ERTS_TRACEE_MODIFIER_FLAGS))
ports = 1;
if (mask & ERTS_TRACEE_MODIFIER_FLAGS)
mods = 1;
#ifdef ERTS_SMP
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_MAIN);
erts_smp_thr_progress_block();
system_blocked = 1;
#endif
ok = 1;
if (procs || mods) {
int max = erts_ptab_max(&erts_proc);
/* tracing of processes */
for (i = 0; i < max; i++) {
Process* tracee_p = erts_pix2proc(i);
if (! tracee_p)
continue;
if (tracer != NIL) {
if (tracee_p->common.id == tracer)
continue;
if (already_traced(NULL, tracee_p, tracer))
continue;
}
if (on) {
ERTS_TRACE_FLAGS(tracee_p) |= mask;
} else {
ERTS_TRACE_FLAGS(tracee_p) &= ~mask;
}
if(!(ERTS_TRACE_FLAGS(tracee_p) & TRACEE_FLAGS)) {
ERTS_TRACER_PROC(tracee_p) = NIL;
} else if (tracer != NIL) {
ERTS_TRACER_PROC(tracee_p) = tracer;
}
matches++;
}
}
if (ports || mods) {
int max = erts_ptab_max(&erts_port);
/* tracing of ports */
for (i = 0; i < max; i++) {
erts_aint32_t state;
Port *tracee_port = erts_pix2port(i);
if (!tracee_port)
continue;
state = erts_atomic32_read_nob(&tracee_port->state);
if (state & ERTS_PORT_SFLGS_DEAD)
continue;
if (tracer != NIL) {
if (tracee_port->common.id == tracer)
continue;
if (port_already_traced(NULL, tracee_port, tracer))
continue;
}
if (on) ERTS_TRACE_FLAGS(tracee_port) |= mask;
else ERTS_TRACE_FLAGS(tracee_port) &= ~mask;
if (!(ERTS_TRACE_FLAGS(tracee_port) & TRACEE_FLAGS)) {
ERTS_TRACER_PROC(tracee_port) = NIL;
} else if (tracer != NIL) {
ERTS_TRACER_PROC(tracee_port) = tracer;
}
/* matches are not counted for ports since it would violate compatibility */
/* This could be a reason to modify this function or make a new one. */
}
}
}
if (pid_spec == am_all || pid_spec == am_new) {
Uint def_flags = mask;
Eterm def_tracer = tracer;
ok = 1;
erts_change_default_tracing(on, &def_flags, &def_tracer);
#ifdef HAVE_ERTS_NOW_CPU
if (cpu_ts && !on) {
/* cpu_ts => pid_spec == am_all */
if (erts_cpu_timestamp) {
#ifdef HAVE_GETHRVTIME
erts_stop_now_cpu();
#endif
erts_cpu_timestamp = 0;
}
}
#endif
}
if (!ok)
goto error;
}
#ifdef ERTS_SMP
if (system_blocked) {
erts_smp_thr_progress_unblock();
erts_smp_proc_lock(p, ERTS_PROC_LOCK_MAIN);
}
#endif
erts_release_code_write_permission();
BIF_RET(make_small(matches));
already_traced:
erts_send_error_to_logger_str(p->group_leader,
"** can only have one tracer per process\n");
error:
#ifdef ERTS_SMP
if (system_blocked) {
erts_smp_thr_progress_unblock();
erts_smp_proc_lock(p, ERTS_PROC_LOCK_MAIN);
}
#endif
erts_release_code_write_permission();
BIF_ERROR(p, BADARG);
}
/* Check that the process to be traced is not already traced
* by a valid other tracer than the tracer to be.
*/
static int port_already_traced(Process *c_p, Port *tracee_port, Eterm tracer)
{
/*
* SMP build assumes that either system is blocked or:
* * main lock is held on c_p
* * all locks are held on port tracee_p
*/
if ((ERTS_TRACE_FLAGS(tracee_port) & TRACEE_FLAGS)
&& ERTS_TRACER_PROC(tracee_port) != tracer) {
/* This tracee is already being traced, and not by the
* tracer to be */
if (is_internal_port(ERTS_TRACER_PROC(tracee_port))) {
if (!erts_is_valid_tracer_port(ERTS_TRACER_PROC(tracee_port))) {
/* Current trace port now invalid
* - discard it and approve the new. */
goto remove_tracer;
} else
return 1;
}
else if(is_internal_pid(ERTS_TRACER_PROC(tracee_port))) {
Process *tracer_p = erts_proc_lookup(ERTS_TRACER_PROC(tracee_port));
if (!tracer_p) {
/* Current trace process now invalid
* - discard it and approve the new. */
goto remove_tracer;
} else
return 1;
}
else {
remove_tracer:
ERTS_TRACE_FLAGS(tracee_port) &= ~TRACEE_FLAGS;
ERTS_TRACER_PROC(tracee_port) = NIL;
}
}
return 0;
}
/* Check that the process to be traced is not already traced
* by a valid other tracer than the tracer to be.
*/
static int already_traced(Process *c_p, Process *tracee_p, Eterm tracer)
{
/*
* SMP build assumes that either system is blocked or:
* * main lock is held on c_p
* * all locks multiple are held on tracee_p
*/
if ((ERTS_TRACE_FLAGS(tracee_p) & TRACEE_FLAGS)
&& ERTS_TRACER_PROC(tracee_p) != tracer) {
/* This tracee is already being traced, and not by the
* tracer to be */
if (is_internal_port(ERTS_TRACER_PROC(tracee_p))) {
if (!erts_is_valid_tracer_port(ERTS_TRACER_PROC(tracee_p))) {
/* Current trace port now invalid
* - discard it and approve the new. */
goto remove_tracer;
} else
return 1;
}
else if(is_internal_pid(ERTS_TRACER_PROC(tracee_p))) {
Process *tracer_p;
tracer_p = erts_proc_lookup(ERTS_TRACER_PROC(tracee_p));
if (!tracer_p) {
/* Current trace process now invalid
* - discard it and approve the new. */
goto remove_tracer;
} else
return 1;
}
else {
remove_tracer:
ERTS_TRACE_FLAGS(tracee_p) &= ~TRACEE_FLAGS;
ERTS_TRACER_PROC(tracee_p) = NIL;
}
}
return 0;
}
/*
* Return information about a process or an external function being traced.
*/
Eterm trace_info_2(BIF_ALIST_2)
{
Process* p = BIF_P;
Eterm What = BIF_ARG_1;
Eterm Key = BIF_ARG_2;
Eterm res;
if (!erts_try_seize_code_write_permission(p)) {
ERTS_BIF_YIELD2(bif_export[BIF_trace_info_2], p, What, Key);
}
if (What == am_on_load) {
res = trace_info_on_load(p, Key);
} else if (is_atom(What) || is_pid(What)) {
res = trace_info_pid(p, What, Key);
} else if (is_tuple(What)) {
res = trace_info_func(p, What, Key);
} else {
erts_release_code_write_permission();
BIF_ERROR(p, BADARG);
}
erts_release_code_write_permission();
BIF_RET(res);
}
static Eterm
trace_info_pid(Process* p, Eterm pid_spec, Eterm key)
{
Eterm tracer;
Uint trace_flags;
Eterm* hp;
if (pid_spec == am_new) {
erts_get_default_tracing(&trace_flags, &tracer);
} else if (is_internal_pid(pid_spec)) {
Process *tracee;
tracee = erts_pid2proc(p, ERTS_PROC_LOCK_MAIN,
pid_spec, ERTS_PROC_LOCKS_ALL);
if (!tracee) {
return am_undefined;
} else {
tracer = ERTS_TRACER_PROC(tracee);
trace_flags = ERTS_TRACE_FLAGS(tracee);
}
if (is_internal_pid(tracer)) {
if (!erts_proc_lookup(tracer)) {
reset_tracer:
ERTS_TRACE_FLAGS(tracee) &= ~TRACEE_FLAGS;
trace_flags = ERTS_TRACE_FLAGS(tracee);
tracer = ERTS_TRACER_PROC(tracee) = NIL;
}
}
else if (is_internal_port(tracer)) {
if (!erts_is_valid_tracer_port(tracer))
goto reset_tracer;
}
#ifdef ERTS_SMP
erts_smp_proc_unlock(tracee,
(tracee == p
? ERTS_PROC_LOCKS_ALL_MINOR
: ERTS_PROC_LOCKS_ALL));
#endif
} else if (is_external_pid(pid_spec)
&& external_pid_dist_entry(pid_spec) == erts_this_dist_entry) {
return am_undefined;
} else {
error:
BIF_ERROR(p, BADARG);
}
if (key == am_flags) {
int num_flags = 19; /* MAXIMUM number of flags. */
Uint needed = 3+2*num_flags;
Eterm flag_list = NIL;
Eterm* limit;
#define FLAG0(flag_mask,flag) \
if (trace_flags & (flag_mask)) { flag_list = CONS(hp, flag, flag_list); hp += 2; } else {}
#if defined(DEBUG)
/*
* Check num_flags if this assertion fires.
*/
# define FLAG ASSERT(num_flags-- > 0); FLAG0
#else
# define FLAG FLAG0
#endif
hp = HAlloc(p, needed);
limit = hp+needed;
FLAG(F_TRACE_SEND, am_send);
FLAG(F_TRACE_RECEIVE, am_receive);
FLAG(F_TRACE_SOS, am_set_on_spawn);
FLAG(F_TRACE_CALLS, am_call);