-
Notifications
You must be signed in to change notification settings - Fork 480
/
Copy pathmcount.c
2404 lines (1911 loc) · 58.3 KB
/
mcount.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
/*
* mcount() handling routines for uftrace
*
* Copyright (C) 2014-2018, LG Electronics, Namhyung Kim <[email protected]>
*
* Released under the GPL v2.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <unistd.h>
/* This should be defined before #include "utils.h" */
#define PR_FMT "mcount"
#define PR_DOMAIN DBG_MCOUNT
#include "libmcount/dynamic.h"
#include "libmcount/internal.h"
#include "libmcount/mcount.h"
#include "mcount-arch.h"
#include "utils/filter.h"
#include "utils/script.h"
#include "utils/socket.h"
#include "utils/symbol.h"
#include "utils/utils.h"
#include "version.h"
/*
* mcount global variables.
*
* These are to control various features in the libmcount.
* They are set during initialization (mcount_startup) which I believe, runs in
* a single thread. After that multiple threads (mostly) read the value so it's
* not protected by lock or something. So special care needs to be taken if you
* want to change it at runtime (like in the agent).
*
* Some are marked as 'maybe unused' because they are only used when filter
* functions are implemented. Note that libmcount is built with different
* settings like -fast and -single to be more efficient in some situation like
* when no filter is specified in the command line and/or single-thread only.
*/
/* time filter in nsec */
uint64_t mcount_threshold;
/* size filter */
unsigned mcount_min_size;
/* symbol info for current process */
struct uftrace_sym_info mcount_sym_info = {
.flags = SYMTAB_FL_DEMANGLE | SYMTAB_FL_ADJ_OFFSET,
};
/* size of shmem buffer to save uftrace_record */
int shmem_bufsize = SHMEM_BUFFER_SIZE;
/* recover return address of parent automatically */
bool mcount_auto_recover = ARCH_SUPPORT_AUTO_RECOVER;
/* global flag to control mcount behavior */
unsigned long mcount_global_flags = MCOUNT_GFL_SETUP;
/* TSD key to save mtd below */
pthread_key_t mtd_key = (pthread_key_t)-1;
/*
* A thread local data to trace function execution.
* While this is itself TLS so ok to by accessed safely by each thread,
* mcount routines use TSD APIs to access it for performance reason.
* Also TSD provides destructor so it can release the resources when the thread
* exits.
*/
TLS struct mcount_thread_data mtd;
/* pipe file descriptor to communite to uftrace */
int mcount_pfd = -1;
/* maximum depth of mcount rstack */
static int mcount_rstack_max = MCOUNT_RSTACK_MAX;
/* name of main executable */
char *mcount_exename;
/* whether it should update pid filter manually */
bool kernel_pid_update;
/* system page size */
int page_size_in_kb;
/* call depth to filter */
int __maybe_unused mcount_depth = MCOUNT_DEFAULT_DEPTH;
/* setting for all filter actions */
struct uftrace_filter_setting mcount_filter_setting = {
.ptype = PATT_REGEX,
.auto_args = false,
.allow_kernel = false,
};
/* boolean flag to turn on/off recording */
bool __maybe_unused mcount_enabled = true;
/* triggers definition and counters */
struct uftrace_triggers_info __maybe_unused *mcount_triggers;
/* bitmask of active watch points */
static unsigned long __maybe_unused mcount_watchpoints;
/* address of function will be called when a function returns */
unsigned long mcount_return_fn;
/* do not hook return address and inject EXIT record between functions */
bool mcount_estimate_return;
__weak void dynamic_return(void)
{
}
/* list of watch points (of global variables) */
static LIST_HEAD(mcount_watch_list);
#ifdef DISABLE_MCOUNT_FILTER
/*
* These functions are only the FAST version of libmcount libraries which don't
* implement filters (other than time and size filters).
*/
static void mcount_filter_init(struct uftrace_filter_setting *filter_setting, bool force)
{
if (getenv("UFTRACE_SRCLINE") == NULL)
return;
load_module_symtabs(&mcount_sym_info);
/* use debug info if available */
prepare_debug_info(&mcount_sym_info, filter_setting->ptype, NULL, NULL, false, force);
save_debug_info(&mcount_sym_info, mcount_sym_info.dirname);
}
static void mcount_filter_finish(void)
{
finish_debug_info(&mcount_sym_info);
}
static void mcount_watch_finish(void)
{
}
#else
/*
* Here goes the regular libmcount's filter and trigger functions.
*/
/* be careful: this can be called from signal handler */
static void mcount_finish_trigger(void)
{
if (mcount_global_flags & MCOUNT_GFL_FINISH)
return;
/* mark other threads can see the finish flag */
mcount_global_flags |= MCOUNT_GFL_FINISH;
}
static LIST_HEAD(siglist);
struct signal_trigger_item {
struct list_head list;
int sig;
struct uftrace_trigger tr;
};
static struct uftrace_trigger *get_signal_trigger(int sig)
{
struct signal_trigger_item *item;
list_for_each_entry(item, &siglist, list) {
if (item->sig == sig)
return &item->tr;
}
return NULL;
}
static void add_signal_trigger(int sig, const char *name, struct uftrace_trigger *tr)
{
struct signal_trigger_item *item;
item = xmalloc(sizeof(*item));
item->sig = sig;
memcpy(&item->tr, tr, sizeof(*tr));
pr_dbg("add signal trigger: %s (%d), flags = %lx\n", name, sig, (unsigned long)tr->flags);
list_add(&item->list, &siglist);
}
static void mcount_signal_trigger(int sig)
{
struct uftrace_trigger *tr;
tr = get_signal_trigger(sig);
if (tr == NULL)
return;
pr_dbg("got signal %d\n", sig);
if (tr->flags & TRIGGER_FL_TRACE_ON) {
mcount_enabled = true;
}
if (tr->flags & TRIGGER_FL_TRACE_OFF) {
mcount_enabled = false;
}
if (tr->flags & TRIGGER_FL_FINISH) {
mcount_finish_trigger();
}
}
/* clang-format off */
#define SIGTABLE_ENTRY(s) { #s, s }
/* clang-format on */
static const struct sigtable {
const char *name;
int sig;
} sigtable[] = {
SIGTABLE_ENTRY(SIGHUP), SIGTABLE_ENTRY(SIGINT), SIGTABLE_ENTRY(SIGQUIT),
SIGTABLE_ENTRY(SIGILL), SIGTABLE_ENTRY(SIGTRAP), SIGTABLE_ENTRY(SIGABRT),
SIGTABLE_ENTRY(SIGBUS), SIGTABLE_ENTRY(SIGFPE), SIGTABLE_ENTRY(SIGKILL),
SIGTABLE_ENTRY(SIGUSR1), SIGTABLE_ENTRY(SIGSEGV), SIGTABLE_ENTRY(SIGUSR2),
SIGTABLE_ENTRY(SIGPIPE), SIGTABLE_ENTRY(SIGALRM), SIGTABLE_ENTRY(SIGTERM),
SIGTABLE_ENTRY(SIGSTKFLT), SIGTABLE_ENTRY(SIGCHLD), SIGTABLE_ENTRY(SIGCONT),
SIGTABLE_ENTRY(SIGSTOP), SIGTABLE_ENTRY(SIGTSTP), SIGTABLE_ENTRY(SIGTTIN),
SIGTABLE_ENTRY(SIGTTOU), SIGTABLE_ENTRY(SIGURG), SIGTABLE_ENTRY(SIGXCPU),
SIGTABLE_ENTRY(SIGXFSZ), SIGTABLE_ENTRY(SIGVTALRM), SIGTABLE_ENTRY(SIGPROF),
SIGTABLE_ENTRY(SIGWINCH), SIGTABLE_ENTRY(SIGIO), SIGTABLE_ENTRY(SIGPWR),
SIGTABLE_ENTRY(SIGSYS),
};
#undef SIGTABLE_ENTRY
static int parse_sigspec(char *spec, struct uftrace_filter_setting *setting)
{
char *pos, *tmp;
unsigned i;
int sig = -1;
int off = 0;
const char *signame = NULL;
bool num_spec = false;
char num_spec_str[16];
struct uftrace_trigger tr = {
.flags = 0,
};
struct sigaction old_sa;
struct sigaction sa = {
.sa_handler = mcount_signal_trigger,
.sa_flags = SA_RESTART,
};
const char *sigrtm = "SIGRTM";
const char *sigrtmin = "SIGRTMIN";
const char *sigrtmax = "SIGRTMAX";
pos = strchr(spec, '@');
if (pos == NULL)
return -1;
*pos = '\0';
if (isdigit(spec[0]))
num_spec = true;
else if (strncmp(spec, "SIG", 3))
off = 3; /* skip "SIG" prefix */
for (i = 0; i < ARRAY_SIZE(sigtable); i++) {
if (num_spec) {
int num = strtol(spec, &tmp, 0);
if (num == sigtable[i].sig) {
sig = num;
signame = sigtable[i].name;
break;
}
continue;
}
if (!strcmp(sigtable[i].name + off, spec)) {
sig = sigtable[i].sig;
signame = sigtable[i].name;
break;
}
}
/* real-time signals */
if (!strncmp(spec, sigrtm + off, 6 - off)) {
if (!strncmp(spec, sigrtmin + off, 8 - off))
sig = SIGRTMIN + strtol(&spec[8 - off], NULL, 0);
if (!strncmp(spec, sigrtmax + off, 8 - off))
sig = SIGRTMAX + strtol(&spec[8 - off], NULL, 0);
signame = spec;
}
if (sig == -1 && num_spec) {
int sigrtmid = (SIGRTMIN + SIGRTMAX) / 2;
sig = strtol(spec, &tmp, 0);
/* SIGRTMIN/MAX might not be constant, avoid switch/case */
if (sig == SIGRTMIN) {
strcpy(num_spec_str, "SIGRTMIN");
}
else if (SIGRTMIN < sig && sig <= sigrtmid) {
snprintf(num_spec_str, sizeof(num_spec_str), "%s+%d", "SIGRTMIN",
sig - SIGRTMIN);
}
else if (sigrtmid < sig && sig < SIGRTMAX) {
snprintf(num_spec_str, sizeof(num_spec_str), "%s-%d", "SIGRTMAX",
SIGRTMAX - sig);
}
else if (sig == SIGRTMAX) {
strcpy(num_spec_str, "SIGRTMAX");
}
else {
sig = -1;
}
signame = num_spec_str;
}
if (sig == -1) {
pr_use("failed to parse signal: %s\n", spec);
return -1;
}
/* setup_trigger_action() requires the '@' sign */
*pos = '@';
tmp = NULL;
if (setup_trigger_action(spec, &tr, &tmp, TRIGGER_FL_SIGNAL, setting) < 0)
return -1;
if (tmp != NULL) {
pr_warn("invalid signal action: %s\n", tmp);
free(tmp);
return -1;
}
add_signal_trigger(sig, signame, &tr);
if (sigaction(sig, &sa, &old_sa) < 0) {
pr_warn("cannot overwrite signal handler for %s\n", spec);
sigaction(sig, &old_sa, NULL);
return -1;
}
return 0;
}
static int mcount_signal_init(char *sigspec, struct uftrace_filter_setting *setting)
{
struct strv strv = STRV_INIT;
char *spec;
int i;
int ret = 0;
if (sigspec == NULL)
return 0;
strv_split(&strv, sigspec, ";");
strv_for_each(&strv, spec, i) {
if (parse_sigspec(spec, setting) < 0)
ret = -1;
}
strv_free(&strv);
return ret;
}
static void mcount_signal_finish(void)
{
struct signal_trigger_item *item;
while (!list_empty(&siglist)) {
item = list_first_entry(&siglist, typeof(*item), list);
list_del(&item->list);
free(item);
}
}
struct uftrace_triggers_info *mcount_trigger_init(struct uftrace_filter_setting *filter_setting)
{
struct uftrace_triggers_info *triggers;
char *filter_str = getenv("UFTRACE_FILTER");
char *trigger_str = getenv("UFTRACE_TRIGGER");
char *argument_str = getenv("UFTRACE_ARGUMENT");
char *retval_str = getenv("UFTRACE_RETVAL");
char *autoargs_str = getenv("UFTRACE_AUTO_ARGS");
char *patch_str = getenv("UFTRACE_PATCH");
char *caller_str = getenv("UFTRACE_CALLER");
char *loc_str = getenv("UFTRACE_LOCATION");
bool needs_debug_info = false;
/* setup auto-args only if argument/return value is used */
if (argument_str || retval_str || autoargs_str ||
(trigger_str && (strstr(trigger_str, "arg") || strstr(trigger_str, "retval")))) {
setup_auto_args(filter_setting);
needs_debug_info = true;
}
if (getenv("UFTRACE_SRCLINE"))
needs_debug_info = true;
/* use debug info if available */
if (needs_debug_info) {
prepare_debug_info(&mcount_sym_info, filter_setting->ptype, argument_str,
retval_str, !!autoargs_str, !!patch_str);
save_debug_info(&mcount_sym_info, mcount_sym_info.dirname);
}
if (!filter_str && !trigger_str && !argument_str && !retval_str && !autoargs_str &&
!caller_str && !loc_str)
return NULL;
triggers = xzalloc(sizeof(*triggers));
triggers->root = RB_ROOT;
filter_setting->auto_args = false;
uftrace_setup_filter(filter_str, &mcount_sym_info, triggers, filter_setting);
uftrace_setup_trigger(trigger_str, &mcount_sym_info, triggers, filter_setting);
uftrace_setup_argument(argument_str, &mcount_sym_info, triggers, filter_setting);
uftrace_setup_retval(retval_str, &mcount_sym_info, triggers, filter_setting);
if (needs_debug_info)
uftrace_setup_loc_filter(loc_str, &mcount_sym_info, triggers, filter_setting);
if (caller_str)
uftrace_setup_caller_filter(caller_str, &mcount_sym_info, triggers, filter_setting);
if (autoargs_str) {
char *autoarg = ".";
char *autoret = ".";
if (filter_setting->ptype == PATT_GLOB)
autoarg = autoret = "*";
filter_setting->auto_args = true;
uftrace_setup_argument(autoarg, &mcount_sym_info, triggers, filter_setting);
uftrace_setup_retval(autoret, &mcount_sym_info, triggers, filter_setting);
}
return triggers;
}
static void mcount_filter_init(struct uftrace_filter_setting *filter_setting, bool force)
{
filter_setting->lp64 = host_is_lp64();
filter_setting->arch = host_cpu_arch();
load_module_symtabs(&mcount_sym_info);
mcount_signal_init(getenv("UFTRACE_SIGNAL"), filter_setting);
mcount_triggers = mcount_trigger_init(filter_setting);
if (mcount_triggers == NULL) {
/* make sure it has the root of triggers */
mcount_triggers = xzalloc(sizeof(*mcount_triggers));
mcount_triggers->root = RB_ROOT;
}
if (getenv("UFTRACE_DEPTH"))
mcount_depth = strtol(getenv("UFTRACE_DEPTH"), NULL, 0);
if (getenv("UFTRACE_TRACE_OFF"))
mcount_enabled = false;
}
static void mcount_filter_setup(struct mcount_thread_data *mtdp)
{
mtdp->filter.max_depth = FILTER_NO_MAX_DEPTH;
mtdp->filter.depth = 0;
mtdp->filter.time = FILTER_NO_TIME;
mtdp->filter.size = mcount_min_size;
mtdp->enable_cached = mcount_enabled;
mtdp->argbuf = xmalloc(mcount_rstack_max * ARGBUF_SIZE);
INIT_LIST_HEAD(&mtdp->pmu_fds);
}
static void mcount_filter_release(struct mcount_thread_data *mtdp)
{
free(mtdp->argbuf);
mtdp->argbuf = NULL;
finish_pmu_event(mtdp);
}
static void mcount_filter_finish(void)
{
uftrace_cleanup_triggers(mcount_triggers);
free(mcount_triggers);
finish_auto_args();
finish_debug_info(&mcount_sym_info);
mcount_signal_finish();
}
static void mcount_watch_init(void)
{
char *watch_str = getenv("UFTRACE_WATCH");
struct strv watch = STRV_INIT;
char *str;
int i;
if (watch_str == NULL)
return;
strv_split(&watch, watch_str, ";");
strv_for_each(&watch, str, i) {
if (!strcasecmp(str, "cpu")) {
mcount_watchpoints |= MCOUNT_WATCH_CPU;
continue;
}
if (!strncasecmp(str, "var:", 4)) {
struct mcount_watchpoint_item *w;
struct uftrace_mmap *map = mcount_sym_info.exec_map;
struct uftrace_symbol *sym;
w = xmalloc(sizeof(*w));
sym = find_symname(&map->mod->symtab, str + 4);
if (sym == NULL) {
pr_dbg("ignore watchpoint for %s\n", str);
free(w);
continue;
}
w->kind = MCOUNT_WATCH_VAR;
w->addr = map->start + sym->addr;
w->size = sym->size;
if (w->size > 8) {
pr_dbg("symbol is too big, ignored... %s\n", str);
free(w);
continue;
}
list_add_tail(&w->list, &mcount_watch_list);
mcount_watchpoints |= MCOUNT_WATCH_VAR;
continue;
}
}
strv_free(&watch);
}
static void mcount_watch_finish(void)
{
struct mcount_watchpoint_item *w;
while (!list_empty(&mcount_watch_list)) {
w = list_first_entry(&mcount_watch_list, typeof(*w), list);
list_del(&w->list);
free(w);
}
}
bool mcount_watch_update(unsigned long addr, void *data, int size)
{
static pthread_mutex_t watch_mutex = PTHREAD_MUTEX_INITIALIZER;
struct mcount_watchpoint_item *w;
bool updated = false;
pthread_mutex_lock(&watch_mutex);
list_for_each_entry(w, &mcount_watch_list, list) {
if (w->addr != addr)
continue;
/* someone already updated for us? */
if (w->inited && !memcmp(data, w->data, size))
break;
mcount_memcpy1(w->data, data, size);
w->inited = true;
updated = true;
break;
}
pthread_mutex_unlock(&watch_mutex);
return updated;
}
static void mcount_watch_setup(struct mcount_thread_data *mtdp)
{
struct mcount_watchpoint_item *pos, *w;
mtdp->watch.cpu = -1;
INIT_LIST_HEAD(&mtdp->watch.list);
/* each thread gets a copy of the global watch items */
list_for_each_entry(pos, &mcount_watch_list, list) {
w = xmalloc(sizeof(*w) + pos->size);
memcpy(w, pos, sizeof(*w));
memcpy(w->data, (void *)w->addr, w->size);
list_add_tail(&w->list, &mtdp->watch.list);
}
}
static void mcount_watch_release(struct mcount_thread_data *mtdp)
{
struct mcount_watchpoint_item *w;
while (!list_empty(&mtdp->watch.list)) {
w = list_first_entry(&mtdp->watch.list, typeof(*w), list);
list_del(&w->list);
free(w);
}
}
#endif /* DISABLE_MCOUNT_FILTER */
/*
* These are common routines used in every libmcount libraries.
*/
static void send_session_msg(struct mcount_thread_data *mtdp, const char *sess_id)
{
struct uftrace_msg_sess sess = {
.task = {
.time = mcount_gettime(),
.pid = getpid(),
.tid = mcount_gettid(mtdp),
},
.namelen = strlen(mcount_exename),
};
struct uftrace_msg msg = {
.magic = UFTRACE_MSG_MAGIC,
.type = UFTRACE_MSG_SESSION,
.len = sizeof(sess) + sess.namelen,
};
struct iovec iov[3] = {
{
.iov_base = &msg,
.iov_len = sizeof(msg),
},
{
.iov_base = &sess,
.iov_len = sizeof(sess),
},
{
.iov_base = mcount_exename,
.iov_len = sess.namelen,
},
};
int len = sizeof(msg) + msg.len;
if (mcount_pfd < 0)
return;
mcount_memcpy4(sess.sid, sess_id, sizeof(sess.sid));
if (writev(mcount_pfd, iov, 3) != len) {
if (!mcount_should_stop())
pr_err("send session msg failed");
}
}
static void mcount_trace_finish(bool send_msg)
{
static pthread_mutex_t finish_lock = PTHREAD_MUTEX_INITIALIZER;
static bool trace_finished = false;
pthread_mutex_lock(&finish_lock);
if (trace_finished)
goto unlock;
/* dtor for script support */
if (SCRIPT_ENABLED && script_str)
script_uftrace_end();
/* notify to uftrace that we're finished */
if (send_msg)
uftrace_send_message(UFTRACE_MSG_FINISH, NULL, 0);
if (mcount_pfd != -1) {
close(mcount_pfd);
mcount_pfd = -1;
}
trace_finished = true;
pr_dbg("mcount trace finished\n");
unlock:
pthread_mutex_unlock(&finish_lock);
}
static void mcount_rstack_estimate_finish(struct mcount_thread_data *mtdp)
{
uint64_t ret_time = mcount_gettime();
pr_dbg2("generates EXIT records for task %d (idx = %d)\n", mcount_gettid(mtdp), mtdp->idx);
while (mtdp->idx > 0) {
mtdp->idx--;
ret_time++;
/* add fake exit records */
mtdp->rstack[mtdp->idx].end_time = ret_time;
mcount_exit_filter_record(mtdp, &mtdp->rstack[mtdp->idx], NULL);
}
}
/* to be used by pthread_create_key() */
void mtd_dtor(void *arg)
{
struct mcount_thread_data *mtdp = arg;
struct uftrace_msg_task tmsg;
if (mtdp->dead)
return;
if (mcount_should_stop())
mcount_trace_finish(true);
/* this thread is done, do not enter anymore */
mtdp->recursion_marker = true;
mtdp->dead = true;
if (mcount_estimate_return)
mcount_rstack_estimate_finish(mtdp);
mcount_rstack_restore(mtdp);
if (ARCH_CAN_RESTORE_PLTHOOK || !mcount_rstack_has_plthook(mtdp)) {
free(mtdp->rstack);
mtdp->rstack = NULL;
mtdp->idx = 0;
}
mcount_filter_release(mtdp);
mcount_watch_release(mtdp);
finish_mem_region(&mtdp->mem_regions);
shmem_finish(mtdp);
tmsg.pid = getpid();
tmsg.tid = mcount_gettid(mtdp);
tmsg.time = mcount_gettime();
uftrace_send_message(UFTRACE_MSG_TASK_END, &tmsg, sizeof(tmsg));
}
void __mcount_guard_recursion(struct mcount_thread_data *mtdp)
{
mtdp->recursion_marker = true;
}
void __mcount_unguard_recursion(struct mcount_thread_data *mtdp)
{
mtdp->recursion_marker = false;
}
bool mcount_guard_recursion(struct mcount_thread_data *mtdp)
{
if (unlikely(mtdp->recursion_marker))
return false;
if (unlikely(mcount_should_stop())) {
mtd_dtor(mtdp);
return false;
}
mtdp->recursion_marker = true;
return true;
}
void mcount_unguard_recursion(struct mcount_thread_data *mtdp)
{
mtdp->recursion_marker = false;
if (unlikely(mcount_should_stop()))
mtd_dtor(mtdp);
}
static struct sigaction old_sigact[2];
static const struct {
int code;
char *msg;
} sigsegv_codes[] = {
{ SEGV_MAPERR, "address not mapped" },
{ SEGV_ACCERR, "invalid permission" },
#ifdef SEGV_BNDERR
{ SEGV_BNDERR, "bound check failed" },
#endif
#ifdef SEGV_PKUERR
{ SEGV_PKUERR, "protection key check failed" },
#endif
};
static void segv_handler(int sig, siginfo_t *si, void *ctx)
{
struct mcount_thread_data *mtdp;
struct mcount_ret_stack *rstack;
int idx;
int i;
/* set line buffer mode not to discard crash message */
setlinebuf(outfp);
mtdp = get_thread_data();
if (check_thread_data(mtdp))
goto out;
if (mtdp->idx <= 0)
goto out;
mcount_rstack_restore(mtdp);
idx = mtdp->idx - 1;
/* flush current rstack on crash */
rstack = &mtdp->rstack[idx];
record_trace_data(mtdp, rstack, NULL);
/* print backtrace */
for (i = 0; i < (int)ARRAY_SIZE(sigsegv_codes); i++) {
if (sig != SIGSEGV)
break;
if (si->si_code == sigsegv_codes[i].code) {
pr_warn("Segmentation fault: %s (addr: %p)\n", sigsegv_codes[i].msg,
si->si_addr);
break;
}
}
if (sig != SIGSEGV || i == (int)ARRAY_SIZE(sigsegv_codes)) {
pr_warn("process crashed by signal %d: %s (si_code: %d)\n", sig, strsignal(sig),
si->si_code);
}
if (!mcount_estimate_return) {
pr_warn(" if this happens only with uftrace,"
" please consider -e/--estimate-return option.\n\n");
}
pr_warn("Backtrace from uftrace " UFTRACE_VERSION "\n");
pr_warn("=====================================\n");
while (rstack >= mtdp->rstack) {
struct uftrace_symbol *parent, *child;
char *pname, *cname;
parent = find_symtabs(&mcount_sym_info, rstack->parent_ip);
pname = symbol_getname(parent, rstack->parent_ip);
child = find_symtabs(&mcount_sym_info, rstack->child_ip);
cname = symbol_getname(child, rstack->child_ip);
pr_warn("[%d] (%s[%lx] <= %s[%lx])\n", idx--, cname, rstack->child_ip, pname,
rstack->parent_ip);
symbol_putname(parent, pname);
symbol_putname(child, cname);
rstack--;
}
pr_out("\n");
pr_red(BUG_REPORT_MSG);
out:
sigaction(sig, &old_sigact[(sig == SIGSEGV)], NULL);
raise(sig);
}
static void mcount_init_file(void)
{
struct sigaction sa = {
.sa_sigaction = segv_handler,
.sa_flags = SA_SIGINFO,
};
send_session_msg(&mtd, mcount_session_name());
pr_dbg("new session started: %.*s: %s\n", SESSION_ID_LEN, mcount_session_name(),
uftrace_basename(mcount_exename));
sigemptyset(&sa.sa_mask);
sigaction(SIGABRT, &sa, &old_sigact[0]);
sigaction(SIGSEGV, &sa, &old_sigact[1]);
}
struct mcount_thread_data *mcount_prepare(void)
{
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
struct mcount_thread_data *mtdp = &mtd;
struct uftrace_msg_task tmsg;
if (unlikely(mcount_should_stop()))
return NULL;
/*
* If an executable implements its own malloc(),
* following recursion could occur
*
* mcount_entry -> mcount_prepare -> xmalloc -> mcount_entry -> ...
*/
if (!mcount_guard_recursion(mtdp))
return NULL;
compiler_barrier();
mcount_filter_setup(mtdp);
mcount_watch_setup(mtdp);
mtdp->rstack = xmalloc(mcount_rstack_max * sizeof(*mtd.rstack));
pthread_once(&once_control, mcount_init_file);
prepare_shmem_buffer(mtdp);
pthread_setspecific(mtd_key, mtdp);
/* time should be get after session message sent */
tmsg.pid = getpid(), tmsg.tid = mcount_gettid(mtdp), tmsg.time = mcount_gettime();
uftrace_send_message(UFTRACE_MSG_TASK_START, &tmsg, sizeof(tmsg));
update_kernel_tid(tmsg.tid);
return mtdp;
}
static void mcount_finish(void)
{
if (!mcount_should_stop())
mcount_trace_finish(false);
if (mcount_estimate_return) {
struct mcount_thread_data *mtdp = get_thread_data();
if (!check_thread_data(mtdp))
mcount_rstack_estimate_finish(mtdp);
}
mcount_global_flags |= MCOUNT_GFL_FINISH;
}
static bool mcount_check_rstack(struct mcount_thread_data *mtdp)
{
if (mtdp->idx >= mcount_rstack_max) {
if (!mtdp->warned) {
struct mcount_ret_stack *rstack;
pr_warn("call depth beyond %d is not recorded.\n"
" (use --max-stack=DEPTH to record more)\n",
mtdp->idx);
/* flush current rstack */
rstack = &mtdp->rstack[mcount_rstack_max - 1];
record_trace_data(mtdp, rstack, NULL);
mtdp->warned = true;
}
return true;
}
mtdp->warned = false;
return false;
}
#ifndef DISABLE_MCOUNT_FILTER
/*
* Again, this implements filter functionality used in !fast versions.
*/
extern void *get_argbuf(struct mcount_thread_data *, struct mcount_ret_stack *);
/**
* mcount_get_filter_mode - compute the filter mode from the filter count
*/
static inline enum filter_mode mcount_get_filter_mode(void)
{
return mcount_triggers->filter_count > 0 ? FILTER_MODE_IN : FILTER_MODE_OUT;
}
/**
* mcount_get_loc_mode - compute the location filter mode from the location count
*/
static inline enum filter_mode mcount_get_loc_mode(void)
{
return mcount_triggers->loc_count > 0 ? FILTER_MODE_IN : FILTER_MODE_OUT;
}
static void mcount_save_filter(struct mcount_thread_data *mtdp)
{
/* save original depth and time to restore at exit time */
mtdp->filter.saved_depth = mtdp->filter.depth;
mtdp->filter.saved_max_depth = mtdp->filter.max_depth;
mtdp->filter.saved_time = mtdp->filter.time;
mtdp->filter.saved_size = mtdp->filter.size;
}