forked from namhyung/uftrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-record.c
1617 lines (1272 loc) · 35.7 KB
/
cmd-record.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <assert.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/eventfd.h>
#include <sys/resource.h>
#include <sys/epoll.h>
#include "uftrace.h"
#include "libmcount/mcount.h"
#include "utils/utils.h"
#include "utils/symbol.h"
#include "utils/list.h"
#include "utils/filter.h"
#define SHMEM_NAME_SIZE (64 - (int)sizeof(struct list_head))
struct shmem_list {
struct list_head list;
char id[SHMEM_NAME_SIZE];
};
static LIST_HEAD(shmem_list_head);
static LIST_HEAD(shmem_need_unlink);
struct buf_list {
struct list_head list;
int tid;
void *shmem_buf;
};
static LIST_HEAD(buf_free_list);
static LIST_HEAD(buf_write_list);
/* currently active writers */
static LIST_HEAD(writer_list);
static pthread_mutex_t free_list_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t write_list_lock = PTHREAD_MUTEX_INITIALIZER;
static bool buf_done;
static int thread_ctl[2];
static bool can_use_fast_libmcount(struct opts *opts)
{
if (opts->filter || opts->trigger || opts->args || opts->retval || debug)
return false;
if (opts->depth != MCOUNT_DEFAULT_DEPTH)
return false;
return true;
}
static char *build_debug_domain_string(void)
{
int i, d;
static char domain[2*DBG_DOMAIN_MAX + 1];
for (i = 0, d = 0; d < DBG_DOMAIN_MAX; d++) {
if (dbg_domain[d]) {
domain[i++] = DBG_DOMAIN_STR[d];
domain[i++] = dbg_domain[d] + '0';
}
}
domain[i] = '\0';
return domain;
}
static void setup_child_environ(struct opts *opts, int pfd)
{
char buf[4096];
char *old_preload, *old_libpath;
bool must_use_multi_thread = check_libpthread(opts->exename);
if (opts->lib_path)
snprintf(buf, sizeof(buf), "%s/libmcount/", opts->lib_path);
else
buf[0] = '\0'; /* to make strcat() work */
if (opts->nop) {
strcat(buf, "libmcount-nop.so");
}
else if (opts->libmcount_single && !must_use_multi_thread) {
if (can_use_fast_libmcount(opts))
strcat(buf, "libmcount-fast-single.so");
else
strcat(buf, "libmcount-single.so");
}
else {
if (must_use_multi_thread && opts->libmcount_single)
pr_dbg("--libmcount-single is off because it calls pthread_create()\n");
if (can_use_fast_libmcount(opts))
strcat(buf, "libmcount-fast.so");
else
strcat(buf, "libmcount.so");
}
pr_dbg("using %s library for tracing\n", buf);
old_preload = getenv("LD_PRELOAD");
if (old_preload) {
size_t len = strlen(buf) + strlen(old_preload) + 2;
char *preload = xmalloc(len);
snprintf(preload, len, "%s:%s", buf, old_preload);
setenv("LD_PRELOAD", preload, 1);
free(preload);
}
else
setenv("LD_PRELOAD", buf, 1);
if (opts->lib_path) {
strcpy(buf, opts->lib_path);
strcat(buf, "/libmcount:");
} else {
/* to make strcat() work */
buf[0] = '\0';
}
#ifdef INSTALL_LIB_PATH
strcat(buf, INSTALL_LIB_PATH);
#endif
old_libpath = getenv("LD_LIBRARY_PATH");
if (old_libpath) {
size_t len = strlen(buf) + strlen(old_libpath) + 2;
char *libpath = xmalloc(len);
snprintf(libpath, len, "%s:%s", buf, old_libpath);
setenv("LD_LIBRARY_PATH", libpath, 1);
free(libpath);
}
else
setenv("LD_LIBRARY_PATH", buf, 1);
if (opts->filter)
setenv("UFTRACE_FILTER", opts->filter, 1);
if (opts->trigger)
setenv("UFTRACE_TRIGGER", opts->trigger, 1);
if (opts->args)
setenv("UFTRACE_ARGUMENT", opts->args, 1);
if (opts->retval)
setenv("UFTRACE_RETVAL", opts->retval, 1);
if (opts->depth != OPT_DEPTH_DEFAULT) {
snprintf(buf, sizeof(buf), "%d", opts->depth);
setenv("UFTRACE_DEPTH", buf, 1);
}
if (opts->max_stack != OPT_RSTACK_DEFAULT) {
snprintf(buf, sizeof(buf), "%d", opts->max_stack);
setenv("UFTRACE_MAX_STACK", buf, 1);
}
if (opts->threshold) {
snprintf(buf, sizeof(buf), "%"PRIu64, opts->threshold);
setenv("UFTRACE_THRESHOLD", buf, 1);
}
if (opts->libcall) {
setenv("UFTRACE_PLTHOOK", "1", 1);
if (opts->want_bind_not) {
/* do not update GOTPLT after resolving symbols */
setenv("LD_BIND_NOT", "1", 1);
}
}
if (strcmp(opts->dirname, UFTRACE_DIR_NAME))
setenv("UFTRACE_DIR", opts->dirname, 1);
if (opts->bufsize != SHMEM_BUFFER_SIZE) {
snprintf(buf, sizeof(buf), "%lu", opts->bufsize);
setenv("UFTRACE_BUFFER", buf, 1);
}
if (opts->logfile) {
snprintf(buf, sizeof(buf), "%d", fileno(logfp));
setenv("UFTRACE_LOGFD", buf, 1);
}
snprintf(buf, sizeof(buf), "%d", pfd);
setenv("UFTRACE_PIPE", buf, 1);
setenv("UFTRACE_SHMEM", "1", 1);
if (debug) {
snprintf(buf, sizeof(buf), "%d", debug);
setenv("UFTRACE_DEBUG", buf, 1);
setenv("UFTRACE_DEBUG_DOMAIN", build_debug_domain_string(), 1);
}
if(opts->disabled)
setenv("UFTRACE_DISABLED", "1", 1);
if (log_color == COLOR_ON) {
snprintf(buf, sizeof(buf), "%d", log_color);
setenv("UFTRACE_COLOR", buf, 1);
}
snprintf(buf, sizeof(buf), "%d", demangler);
setenv("UFTRACE_DEMANGLE", buf, 1);
}
static uint64_t calc_feat_mask(struct opts *opts)
{
uint64_t features = 0;
/* mcount code creates task and sid-XXX.map files */
features |= TASK_SESSION;
/* symbol file saves relative address */
features |= SYM_REL_ADDR;
/* save mcount_max_stack */
features |= MAX_STACK;
if (opts->libcall)
features |= PLTHOOK;
if (opts->kernel)
features |= KERNEL;
if (opts->args)
features |= ARGUMENT;
if (opts->retval)
features |= RETVAL;
return features;
}
static int fill_file_header(struct opts *opts, int status, struct rusage *rusage)
{
int fd, efd;
int ret = -1;
char *filename = NULL;
struct ftrace_file_header hdr;
char elf_ident[EI_NIDENT];
xasprintf(&filename, "%s/info", opts->dirname);
pr_dbg3("fill header (metadata) info in %s\n", filename);
fd = open(filename, O_WRONLY | O_CREAT| O_TRUNC, 0644);
if (fd < 0) {
pr_log("cannot open info file: %s\n", strerror(errno));
free(filename);
return -1;
}
efd = open(opts->exename, O_RDONLY);
if (efd < 0)
goto close_fd;
if (read(efd, elf_ident, sizeof(elf_ident)) < 0)
goto close_efd;
strncpy(hdr.magic, UFTRACE_MAGIC_STR, UFTRACE_MAGIC_LEN);
hdr.version = UFTRACE_FILE_VERSION;
hdr.header_size = sizeof(hdr);
hdr.endian = elf_ident[EI_DATA];
hdr.class = elf_ident[EI_CLASS];
hdr.feat_mask = calc_feat_mask(opts);
hdr.info_mask = 0;
hdr.max_stack = opts->max_stack;
hdr.unused1 = 0;
hdr.unused2 = 0;
if (write(fd, &hdr, sizeof(hdr)) != (int)sizeof(hdr))
pr_err("writing header info failed");
fill_ftrace_info(&hdr.info_mask, fd, opts, status, rusage);
try_write:
ret = pwrite(fd, &hdr, sizeof(hdr), 0);
if (ret != (int)sizeof(hdr)) {
static int retry = 0;
if (ret > 0 && retry++ < 3)
goto try_write;
pr_dbg("writing header info failed.\n");
goto close_efd;
}
ret = 0;
close_efd:
close(efd);
close_fd:
close(fd);
free(filename);
return ret;
}
static void parse_msg_id(char *id, uint64_t *sid, int *tid, int *seq)
{
uint64_t _sid;
unsigned _tid;
unsigned _seq;
/*
* parse message id of "/uftrace-SESSION-TID-SEQ".
*/
if (sscanf(id, "/uftrace-%016"SCNx64"-%u-%03u", &_sid, &_tid, &_seq) != 3)
pr_err("parse msg id failed");
if (sid)
*sid = _sid;
if (tid)
*tid = _tid;
if (seq)
*seq = _seq;
}
static char *make_disk_name(const char *dirname, int tid)
{
char *filename = NULL;
xasprintf(&filename, "%s/%d.dat", dirname, tid);
return filename;
}
static void write_buffer_file(const char *dirname, struct buf_list *buf)
{
int fd;
char *filename;
struct mcount_shmem_buffer *shmbuf = buf->shmem_buf;
filename = make_disk_name(dirname, buf->tid);
fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd < 0)
pr_err("open disk file");
if (write_all(fd, shmbuf->data, shmbuf->size) < 0)
pr_err("write shmem buffer");
close(fd);
free(filename);
}
static void write_buffer(struct buf_list *buf, struct opts *opts, int sock)
{
struct mcount_shmem_buffer *shmbuf = buf->shmem_buf;
if (!opts->host)
return write_buffer_file(opts->dirname, buf);
send_trace_data(sock, buf->tid, shmbuf->data, shmbuf->size);
}
struct writer_arg {
struct list_head list;
struct list_head bufs;
struct opts *opts;
struct ftrace_kernel *kern;
int sock;
int idx;
int tid;
int nr_cpu;
int cpus[];
};
static void write_buf_list(struct list_head *buf_head, struct opts *opts,
struct writer_arg *warg)
{
struct buf_list *buf;
list_for_each_entry(buf, buf_head, list) {
struct mcount_shmem_buffer *shmbuf = buf->shmem_buf;
write_buffer(buf, opts, warg->sock);
/*
* Now it has consumed all contents in the shmem buffer,
* make it so that mcount can reuse it.
* This is paired with get_new_shmem_buffer().
*/
__sync_synchronize();
shmbuf->flag = SHMEM_FL_WRITTEN;
munmap(shmbuf, opts->bufsize);
buf->shmem_buf = NULL;
}
pthread_mutex_lock(&free_list_lock);
while (!list_empty(buf_head)) {
struct list_head *l = buf_head->next;
list_move(l, &buf_free_list);
}
pthread_mutex_unlock(&free_list_lock);
}
void *writer_thread(void *arg)
{
struct buf_list *buf, *pos;
struct writer_arg *warg = arg;
struct opts *opts = warg->opts;
struct pollfd pollfd[warg->nr_cpu + 1];
int i, dummy;
if (opts->rt_prio) {
struct sched_param param = {
.sched_priority = opts->rt_prio,
};
if (sched_setscheduler(0, SCHED_FIFO, ¶m) < 0)
pr_log("set scheduling param failed\n");
}
pollfd[0].fd = thread_ctl[0];
pollfd[0].events = POLLIN;
for (i = 0; i < warg->nr_cpu; i++) {
pollfd[i + 1].fd = warg->kern->traces[warg->cpus[i]];
pollfd[i + 1].events = POLLIN;
}
pr_dbg2("start writer thread %d\n", warg->idx);
while (!buf_done) {
LIST_HEAD(head);
bool check_list = false;
if (poll(pollfd, warg->nr_cpu + 1, 1000) < 0)
goto out;
for (i = 0; i < warg->nr_cpu + 1; i++) {
if (pollfd[i].revents & POLLIN) {
if (i == 0)
check_list = true;
else
record_kernel_trace_pipe(warg->kern,
warg->cpus[i-1]);
}
}
if (!check_list)
continue;
if (read(thread_ctl[0], &dummy, sizeof(dummy)) < 0) {
if (errno == EAGAIN && errno == EINTR)
continue;
/* other errors are problematic */
break;
}
pthread_mutex_lock(&write_list_lock);
if (!list_empty(&buf_write_list)) {
/* pick first unhandled buf */
buf = list_first_entry(&buf_write_list,
struct buf_list, list);
list_move(&buf->list, &head);
warg->tid = buf->tid;
list_add(&warg->list, &writer_list);
}
list_for_each_entry_safe(buf, pos, &buf_write_list, list) {
/* list may have multiple buf for this task */
if (buf->tid == warg->tid)
list_move_tail(&buf->list, &head);
}
pthread_mutex_unlock(&write_list_lock);
while (!list_empty(&head)) {
write_buf_list(&head, opts, warg);
pthread_mutex_lock(&write_list_lock);
/* check someone sends bufs for me directly */
list_splice_tail_init(&warg->bufs, &head);
if (list_empty(&head)) {
/* I'm done with this tid */
warg->tid = -1;
list_del_init(&warg->list);
}
pthread_mutex_unlock(&write_list_lock);
if (!opts->kernel)
continue;
poll(&pollfd[1], warg->nr_cpu, 0);
for (i = 0; i < warg->nr_cpu; i++) {
if (pollfd[i+1].revents & POLLIN) {
record_kernel_trace_pipe(warg->kern,
warg->cpus[i]);
}
}
}
}
pr_dbg2("stop writer thread %d\n", warg->idx);
out:
free(warg);
return NULL;
}
static struct buf_list *make_write_buffer(void)
{
struct buf_list *buf;
buf = malloc(sizeof(*buf));
if (buf == NULL)
return NULL;
INIT_LIST_HEAD(&buf->list);
return buf;
}
static void copy_to_buffer(struct mcount_shmem_buffer *shm, char *sess_id)
{
struct buf_list *buf = NULL;
struct writer_arg *writer;
pthread_mutex_lock(&free_list_lock);
if (!list_empty(&buf_free_list)) {
buf = list_first_entry(&buf_free_list, struct buf_list, list);
list_del(&buf->list);
}
pthread_mutex_unlock(&free_list_lock);
if (buf == NULL) {
buf = make_write_buffer();
if (buf == NULL)
pr_err_ns("not enough memory!\n");
pr_dbg3("make a new write buffer\n");
}
buf->shmem_buf = shm;
parse_msg_id(sess_id, NULL, &buf->tid, NULL);
pthread_mutex_lock(&write_list_lock);
/* check some writers work for this tid */
list_for_each_entry(writer, &writer_list, list) {
if (buf->tid == writer->tid) {
/* if so, pass the buf directly */
list_add_tail(&buf->list, &writer->bufs);
break;
}
}
if (list_no_entry(writer, &writer_list, list)) {
int kick = 1;
/* no writer is dealing with the tid */
list_add_tail(&buf->list, &buf_write_list);
if (write(thread_ctl[1], &kick, sizeof(kick)) < 0 && !buf_done)
pr_err("copying to buffer failed");
}
pthread_mutex_unlock(&write_list_lock);
}
static int record_mmap_file(const char *dirname, char *sess_id, int bufsize)
{
int fd;
struct shmem_list *sl;
struct mcount_shmem_buffer *shmem_buf;
/* write (append) it to disk */
fd = shm_open(sess_id, O_RDWR, 0600);
if (fd < 0) {
pr_dbg("open shmem buffer failed: %s: %m\n", sess_id);
return 0;
}
shmem_buf = mmap(NULL, bufsize, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (shmem_buf == MAP_FAILED)
pr_err("mmap shmem buffer");
close(fd);
if (shmem_buf->flag & SHMEM_FL_RECORDING) {
if (shmem_buf->flag & SHMEM_FL_NEW) {
bool found = false;
if (!list_empty(&shmem_need_unlink)) {
sl = list_last_entry(&shmem_need_unlink,
struct shmem_list, list);
/* length of "uftrace-<session id>-" is 25 */
if (!strncmp(sl->id, sess_id, 25))
found = true;
}
if (!found) {
sl = xmalloc(sizeof(*sl));
memcpy(sl->id, sess_id, sizeof(sl->id));
/* link to shmem_list */
list_add_tail(&sl->list, &shmem_need_unlink);
}
}
if (shmem_buf->size) {
/* shmem_buf will be unmapped */
copy_to_buffer(shmem_buf, sess_id);
}
}
return 0;
}
static void stop_all_writers(void)
{
buf_done = true;
close(thread_ctl[1]);
thread_ctl[1] = -1;
}
static void record_remaining_buffer(struct opts *opts, int sock)
{
struct buf_list *buf;
/* called after all writers gone, no lock is needed */
while (!list_empty(&buf_write_list)) {
buf = list_first_entry(&buf_write_list, struct buf_list, list);
write_buffer(buf, opts, sock);
munmap(buf->shmem_buf, opts->bufsize);
list_del(&buf->list);
free(buf);
}
while (!list_empty(&buf_free_list)) {
buf = list_first_entry(&buf_free_list, struct buf_list, list);
list_del(&buf->list);
free(buf);
}
}
static void flush_shmem_list(const char *dirname, int bufsize)
{
struct shmem_list *sl, *tmp;
/* flush remaining list (due to abnormal termination) */
list_for_each_entry_safe(sl, tmp, &shmem_list_head, list) {
pr_dbg("flushing %s\n", sl->id);
list_del(&sl->list);
record_mmap_file(dirname, sl->id, bufsize);
free(sl);
}
}
static char shmem_session[20];
static int filter_shmem(const struct dirent *de)
{
/* compare session ID after the "uftrace-" part */
return !memcmp(&de->d_name[8], shmem_session, 16);
}
static void unlink_shmem_list(void)
{
struct shmem_list *sl, *tmp;
/* unlink shmem list (not used anymore) */
list_for_each_entry_safe(sl, tmp, &shmem_need_unlink, list) {
char sid[128];
struct dirent **shmem_bufs;
int i, num;
list_del(&sl->list);
sscanf(sl->id, "/uftrace-%[^-]-%*d-%*d", shmem_session);
pr_dbg2("unlink for session: %s\n", shmem_session);
num = scandir("/dev/shm/", &shmem_bufs, filter_shmem, alphasort);
for (i = 0; i < num; i++) {
sid[0] = '/';
strcpy(&sid[1], shmem_bufs[i]->d_name);
pr_dbg3("unlink %s\n", sid);
shm_unlink(sid);
free(shmem_bufs[i]);
}
free(shmem_bufs);
free(sl);
}
}
static void flush_old_shmem(const char *dirname, int tid, int bufsize)
{
struct shmem_list *sl;
/* flush remaining list (due to abnormal termination) */
list_for_each_entry(sl, &shmem_list_head, list) {
int sl_tid;
sscanf(sl->id, "/uftrace-%*x-%d-%*d", &sl_tid);
if (tid == sl_tid) {
pr_dbg3("flushing %s\n", sl->id);
list_del(&sl->list);
record_mmap_file(dirname, sl->id, bufsize);
free(sl);
return;
}
}
}
static int shmem_lost_count;
struct tid_list {
struct list_head list;
int pid;
int tid;
bool exited;
};
static LIST_HEAD(tid_list_head);
static void add_tid_list(int pid, int tid)
{
struct tid_list *tl;
tl = xmalloc(sizeof(*tl));
tl->pid = pid;
tl->tid = tid;
tl->exited = false;
/* link to tid_list */
list_add(&tl->list, &tid_list_head);
}
static void free_tid_list(void)
{
struct tid_list *tl, *tmp;
list_for_each_entry_safe(tl, tmp, &tid_list_head, list) {
list_del(&tl->list);
free(tl);
}
}
static bool check_tid_list(void)
{
struct tid_list *tl;
char buf[128];
list_for_each_entry(tl, &tid_list_head, list) {
int fd, len;
char state;
char line[4096];
if (tl->exited || tl->tid < 0)
continue;
snprintf(buf, sizeof(buf), "/proc/%d/stat", tl->tid);
fd = open(buf, O_RDONLY);
if (fd < 0) {
tl->exited = true;
continue;
}
len = read(fd, line, sizeof(line) - 1);
if (len < 0) {
tl->exited = true;
close(fd);
continue;
}
line[len] = '\0';
sscanf(line, "%*d %*s %c", &state);
if (state == 'Z')
tl->exited = true;
close(fd);
}
list_for_each_entry(tl, &tid_list_head, list) {
if (!tl->exited)
return false;
}
pr_dbg2("all process/thread exited\n");
return true;
}
static void read_record_mmap(int pfd, const char *dirname, int bufsize)
{
char buf[128];
struct shmem_list *sl, *tmp;
struct tid_list *tl, *pos;
struct ftrace_msg msg;
struct ftrace_msg_task tmsg;
struct ftrace_msg_sess sess;
struct ftrace_msg_dlopen dmsg;
char *exename;
int lost;
if (read_all(pfd, &msg, sizeof(msg)) < 0)
pr_err("reading pipe failed:");
if (msg.magic != FTRACE_MSG_MAGIC)
pr_err_ns("invalid message received: %x\n", msg.magic);
switch (msg.type) {
case FTRACE_MSG_REC_START:
if (msg.len > SHMEM_NAME_SIZE)
pr_err_ns("invalid message length\n");
sl = xmalloc(sizeof(*sl));
if (read_all(pfd, sl->id, msg.len) < 0)
pr_err("reading pipe failed");
sl->id[msg.len] = '\0';
pr_dbg2("MSG START: %s\n", sl->id);
/* link to shmem_list */
list_add_tail(&sl->list, &shmem_list_head);
break;
case FTRACE_MSG_REC_END:
if (msg.len > SHMEM_NAME_SIZE)
pr_err_ns("invalid message length\n");
if (read_all(pfd, buf, msg.len) < 0)
pr_err("reading pipe failed");
buf[msg.len] = '\0';
pr_dbg2("MSG END : %s\n", buf);
/* remove from shmem_list */
list_for_each_entry_safe(sl, tmp, &shmem_list_head, list) {
if (!strncmp(sl->id, buf, SHMEM_NAME_SIZE)) {
list_del(&sl->list);
free(sl);
break;
}
}
record_mmap_file(dirname, buf, bufsize);
break;
case FTRACE_MSG_TID:
if (msg.len != sizeof(tmsg))
pr_err_ns("invalid message length\n");
if (read_all(pfd, &tmsg, sizeof(tmsg)) < 0)
pr_err("reading pipe failed");
pr_dbg2("MSG TID : %d/%d\n", tmsg.pid, tmsg.tid);
/* check existing tid (due to exec) */
list_for_each_entry(pos, &tid_list_head, list) {
if (pos->tid == tmsg.tid) {
flush_old_shmem(dirname, tmsg.tid, bufsize);
break;
}
}
if (list_no_entry(pos, &tid_list_head, list))
add_tid_list(tmsg.pid, tmsg.tid);
write_task_info(dirname, &tmsg);
break;
case FTRACE_MSG_FORK_START:
if (msg.len != sizeof(tmsg))
pr_err_ns("invalid message length\n");
if (read_all(pfd, &tmsg, sizeof(tmsg)) < 0)
pr_err("reading pipe failed");
pr_dbg2("MSG FORK1: %d/%d\n", tmsg.pid, -1);
add_tid_list(tmsg.pid, -1);
break;
case FTRACE_MSG_FORK_END:
if (msg.len != sizeof(tmsg))
pr_err_ns("invalid message length\n");
if (read_all(pfd, &tmsg, sizeof(tmsg)) < 0)
pr_err("reading pipe failed");
list_for_each_entry(tl, &tid_list_head, list) {
if (tl->pid == tmsg.pid && tl->tid == -1)
break;
}
if (list_no_entry(tl, &tid_list_head, list)) {
/*
* daemon process has no guarantee that having parent
* pid of 1 anymore due to the systemd, just pick a
* first task which has tid of -1.
*/
list_for_each_entry(tl, &tid_list_head, list) {
if (tl->tid == -1) {
pr_dbg3("override parent of daemon to %d\n",
tl->pid);
tmsg.pid = tl->pid;
break;
}
}
}
if (list_no_entry(tl, &tid_list_head, list))
pr_err("cannot find fork pid: %d\n", tmsg.pid);
tl->tid = tmsg.tid;
pr_dbg2("MSG FORK2: %d/%d\n", tl->pid, tl->tid);
write_fork_info(dirname, &tmsg);
break;
case FTRACE_MSG_SESSION:
if (msg.len < sizeof(sess))
pr_err_ns("invalid message length\n");
if (read_all(pfd, &sess, sizeof(sess)) < 0)
pr_err("reading pipe failed");
exename = xmalloc(sess.namelen + 1);
if (read_all(pfd, exename, sess.namelen) < 0)
pr_err("reading pipe failed");
exename[sess.namelen] = '\0';
memcpy(buf, sess.sid, 16);
buf[16] = '\0';
pr_dbg2("MSG SESSION: %d: %s (%s)\n", sess.task.tid, exename, buf);
write_session_info(dirname, &sess, exename);
break;
case FTRACE_MSG_LOST:
if (msg.len < sizeof(lost))
pr_err_ns("invalid message length\n");
if (read_all(pfd, &lost, sizeof(lost)) < 0)
pr_err("reading pipe failed");
shmem_lost_count += lost;
break;
case FTRACE_MSG_DLOPEN:
if (msg.len < sizeof(dmsg))
pr_err_ns("invalid message length\n");
if (read_all(pfd, &dmsg, sizeof(dmsg)) < 0)
pr_err("reading pipe failed");
exename = xmalloc(dmsg.namelen + 1);
if (read_all(pfd, exename, dmsg.namelen) < 0)
pr_err("reading pipe failed");
exename[dmsg.namelen] = '\0';
pr_dbg2("MSG DLOPEN: %d: %#lx %s\n", dmsg.task.tid, dmsg.base_addr, exename);
write_dlopen_info(dirname, &dmsg, exename);
break;
default:
pr_log("Unknown message type: %u\n", msg.type);
break;
}
}
static void send_task_file(int sock, const char *dirname, struct symtabs *symtabs)
{
FILE *fp;
char *filename = NULL;
struct ftrace_msg msg;
struct ftrace_msg_task tmsg;
struct ftrace_msg_sess smsg;
int namelen;
char *exename;
xasprintf(&filename, "%s/task", dirname);
fp = fopen(filename, "r");