1 // SPDX-License-Identifier: GPL-2.0
2 #include <elf.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <linux/kernel.h>
9 #include <linux/zalloc.h>
10 #include "dso.h"
11 #include "session.h"
12 #include "thread.h"
13 #include "thread-stack.h"
14 #include "debug.h"
15 #include "namespaces.h"
16 #include "comm.h"
17 #include "map.h"
18 #include "symbol.h"
19 #include "unwind.h"
20 #include "callchain.h"
21 #include "dwarf-regs.h"
22
23 #include <api/fs/fs.h>
24
thread__init_maps(struct thread * thread,struct machine * machine)25 int thread__init_maps(struct thread *thread, struct machine *machine)
26 {
27 pid_t pid = thread__pid(thread);
28
29 if (pid == thread__tid(thread) || pid == -1) {
30 thread__set_maps(thread, maps__new(machine));
31 } else {
32 struct thread *leader = machine__findnew_thread(machine, pid, pid);
33
34 if (leader) {
35 thread__set_maps(thread, maps__get(thread__maps(leader)));
36 thread__put(leader);
37 }
38 }
39
40 return thread__maps(thread) ? 0 : -1;
41 }
42
thread__new(pid_t pid,pid_t tid)43 struct thread *thread__new(pid_t pid, pid_t tid)
44 NO_THREAD_SAFETY_ANALYSIS /* Allocation/creation is inherently single threaded. */
45 {
46 RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
47 struct thread *thread;
48
49 if (ADD_RC_CHK(thread, _thread) != NULL) {
50 struct comm *comm;
51 char comm_str[32];
52
53 thread__set_pid(thread, pid);
54 thread__set_tid(thread, tid);
55 thread__set_ppid(thread, -1);
56 thread__set_cpu(thread, -1);
57 thread__set_guest_cpu(thread, -1);
58 thread__set_e_machine(thread, EM_NONE);
59 thread__set_lbr_stitch_enable(thread, false);
60 INIT_LIST_HEAD(thread__namespaces_list(thread));
61 INIT_LIST_HEAD(thread__comm_list(thread));
62 init_rwsem(thread__namespaces_lock(thread));
63 init_rwsem(thread__comm_lock(thread));
64
65 snprintf(comm_str, sizeof(comm_str), ":%d", tid);
66 comm = comm__new(comm_str, 0, false);
67 if (!comm)
68 goto err_thread;
69
70 list_add(&comm->list, thread__comm_list(thread));
71 refcount_set(thread__refcnt(thread), 1);
72 /* Thread holds first ref to nsdata. */
73 RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new(pid);
74 srccode_state_init(thread__srccode_state(thread));
75 }
76
77 return thread;
78
79 err_thread:
80 thread__delete(thread);
81 return NULL;
82 }
83
84 static void (*thread__priv_destructor)(void *priv);
85
thread__set_priv_destructor(void (* destructor)(void * priv))86 void thread__set_priv_destructor(void (*destructor)(void *priv))
87 {
88 assert(thread__priv_destructor == NULL);
89
90 thread__priv_destructor = destructor;
91 }
92
thread__delete(struct thread * thread)93 void thread__delete(struct thread *thread)
94 {
95 struct namespaces *namespaces, *tmp_namespaces;
96 struct comm *comm, *tmp_comm;
97
98 thread_stack__free(thread);
99
100 if (thread__maps(thread)) {
101 maps__put(thread__maps(thread));
102 thread__set_maps(thread, NULL);
103 }
104 down_write(thread__namespaces_lock(thread));
105 list_for_each_entry_safe(namespaces, tmp_namespaces,
106 thread__namespaces_list(thread), list) {
107 list_del_init(&namespaces->list);
108 namespaces__free(namespaces);
109 }
110 up_write(thread__namespaces_lock(thread));
111
112 down_write(thread__comm_lock(thread));
113 list_for_each_entry_safe(comm, tmp_comm, thread__comm_list(thread), list) {
114 list_del_init(&comm->list);
115 comm__free(comm);
116 }
117 up_write(thread__comm_lock(thread));
118
119 nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
120 srccode_state_free(thread__srccode_state(thread));
121
122 exit_rwsem(thread__namespaces_lock(thread));
123 exit_rwsem(thread__comm_lock(thread));
124 thread__free_stitch_list(thread);
125
126 if (thread__priv_destructor)
127 thread__priv_destructor(thread__priv(thread));
128
129 RC_CHK_FREE(thread);
130 }
131
thread__get(struct thread * thread)132 struct thread *thread__get(struct thread *thread)
133 {
134 struct thread *result;
135
136 if (RC_CHK_GET(result, thread))
137 refcount_inc(thread__refcnt(thread));
138
139 return result;
140 }
141
thread__put(struct thread * thread)142 void thread__put(struct thread *thread)
143 {
144 if (thread && refcount_dec_and_test(thread__refcnt(thread)))
145 thread__delete(thread);
146 else
147 RC_CHK_PUT(thread);
148 }
149
__thread__namespaces(struct thread * thread)150 static struct namespaces *__thread__namespaces(struct thread *thread)
151 {
152 if (list_empty(thread__namespaces_list(thread)))
153 return NULL;
154
155 return list_first_entry(thread__namespaces_list(thread), struct namespaces, list);
156 }
157
thread__namespaces(struct thread * thread)158 struct namespaces *thread__namespaces(struct thread *thread)
159 {
160 struct namespaces *ns;
161
162 down_read(thread__namespaces_lock(thread));
163 ns = __thread__namespaces(thread);
164 up_read(thread__namespaces_lock(thread));
165
166 return ns;
167 }
168
__thread__set_namespaces(struct thread * thread,u64 timestamp,struct perf_record_namespaces * event)169 static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
170 struct perf_record_namespaces *event)
171 {
172 struct namespaces *new, *curr = __thread__namespaces(thread);
173
174 new = namespaces__new(event);
175 if (!new)
176 return -ENOMEM;
177
178 list_add(&new->list, thread__namespaces_list(thread));
179
180 if (timestamp && curr) {
181 /*
182 * setns syscall must have changed few or all the namespaces
183 * of this thread. Update end time for the namespaces
184 * previously used.
185 */
186 curr = list_next_entry(new, list);
187 curr->end_time = timestamp;
188 }
189
190 return 0;
191 }
192
thread__set_namespaces(struct thread * thread,u64 timestamp,struct perf_record_namespaces * event)193 int thread__set_namespaces(struct thread *thread, u64 timestamp,
194 struct perf_record_namespaces *event)
195 {
196 int ret;
197
198 down_write(thread__namespaces_lock(thread));
199 ret = __thread__set_namespaces(thread, timestamp, event);
200 up_write(thread__namespaces_lock(thread));
201 return ret;
202 }
203
__thread__comm(struct thread * thread)204 static struct comm *__thread__comm(struct thread *thread)
205 SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
206 {
207 if (list_empty(thread__comm_list(thread)))
208 return NULL;
209
210 return list_first_entry(thread__comm_list(thread), struct comm, list);
211 }
212
thread__comm(struct thread * thread)213 struct comm *thread__comm(struct thread *thread)
214 {
215 struct comm *res = NULL;
216
217 down_read(thread__comm_lock(thread));
218 res = __thread__comm(thread);
219 up_read(thread__comm_lock(thread));
220 return res;
221 }
222
thread__exec_comm(struct thread * thread)223 struct comm *thread__exec_comm(struct thread *thread)
224 {
225 struct comm *comm, *last = NULL, *second_last = NULL;
226
227 down_read(thread__comm_lock(thread));
228 list_for_each_entry(comm, thread__comm_list(thread), list) {
229 if (comm->exec) {
230 up_read(thread__comm_lock(thread));
231 return comm;
232 }
233 second_last = last;
234 last = comm;
235 }
236 up_read(thread__comm_lock(thread));
237
238 /*
239 * 'last' with no start time might be the parent's comm of a synthesized
240 * thread (created by processing a synthesized fork event). For a main
241 * thread, that is very probably wrong. Prefer a later comm to avoid
242 * that case.
243 */
244 if (second_last && !last->start && thread__pid(thread) == thread__tid(thread))
245 return second_last;
246
247 return last;
248 }
249
____thread__set_comm(struct thread * thread,const char * str,u64 timestamp,bool exec)250 static int ____thread__set_comm(struct thread *thread, const char *str,
251 u64 timestamp, bool exec)
252 EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread))
253 {
254 struct comm *new, *curr = __thread__comm(thread);
255
256 /* Override the default :tid entry */
257 if (!thread__comm_set(thread)) {
258 int err = comm__override(curr, str, timestamp, exec);
259 if (err)
260 return err;
261 } else {
262 new = comm__new(str, timestamp, exec);
263 if (!new)
264 return -ENOMEM;
265 list_add(&new->list, thread__comm_list(thread));
266
267 if (exec)
268 unwind__flush_access(thread__maps(thread));
269 }
270
271 thread__set_comm_set(thread, true);
272
273 return 0;
274 }
275
__thread__set_comm(struct thread * thread,const char * str,u64 timestamp,bool exec)276 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
277 bool exec)
278 {
279 int ret;
280
281 down_write(thread__comm_lock(thread));
282 ret = ____thread__set_comm(thread, str, timestamp, exec);
283 up_write(thread__comm_lock(thread));
284 return ret;
285 }
286
thread__set_comm_from_proc(struct thread * thread)287 int thread__set_comm_from_proc(struct thread *thread)
288 {
289 char path[64];
290 char *comm = NULL;
291 size_t sz;
292 int err = -1;
293
294 if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
295 thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) &&
296 procfs__read_str(path, &comm, &sz) == 0) {
297 comm[sz - 1] = '\0';
298 err = thread__set_comm(thread, comm, 0);
299 }
300
301 return err;
302 }
303
__thread__comm_str(struct thread * thread)304 static const char *__thread__comm_str(struct thread *thread)
305 SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
306 {
307 const struct comm *comm = __thread__comm(thread);
308
309 if (!comm)
310 return NULL;
311
312 return comm__str(comm);
313 }
314
thread__comm_str(struct thread * thread)315 const char *thread__comm_str(struct thread *thread)
316 {
317 const char *str;
318
319 down_read(thread__comm_lock(thread));
320 str = __thread__comm_str(thread);
321 up_read(thread__comm_lock(thread));
322
323 return str;
324 }
325
__thread__comm_len(struct thread * thread,const char * comm)326 static int __thread__comm_len(struct thread *thread, const char *comm)
327 {
328 if (!comm)
329 return 0;
330 thread__set_comm_len(thread, strlen(comm));
331
332 return thread__var_comm_len(thread);
333 }
334
335 /* CHECKME: it should probably better return the max comm len from its comm list */
thread__comm_len(struct thread * thread)336 int thread__comm_len(struct thread *thread)
337 {
338 int comm_len = thread__var_comm_len(thread);
339
340 if (!comm_len) {
341 const char *comm;
342
343 down_read(thread__comm_lock(thread));
344 comm = __thread__comm_str(thread);
345 comm_len = __thread__comm_len(thread, comm);
346 up_read(thread__comm_lock(thread));
347 }
348
349 return comm_len;
350 }
351
thread__fprintf(struct thread * thread,FILE * fp)352 size_t thread__fprintf(struct thread *thread, FILE *fp)
353 {
354 return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_str(thread)) +
355 maps__fprintf(thread__maps(thread), fp);
356 }
357
thread__insert_map(struct thread * thread,struct map * map)358 int thread__insert_map(struct thread *thread, struct map *map)
359 {
360 int ret;
361
362 ret = unwind__prepare_access(thread__maps(thread), map, NULL);
363 if (ret)
364 return ret;
365
366 return maps__fixup_overlap_and_insert(thread__maps(thread), map);
367 }
368
369 struct thread__prepare_access_maps_cb_args {
370 int err;
371 struct maps *maps;
372 };
373
thread__prepare_access_maps_cb(struct map * map,void * data)374 static int thread__prepare_access_maps_cb(struct map *map, void *data)
375 {
376 bool initialized = false;
377 struct thread__prepare_access_maps_cb_args *args = data;
378
379 args->err = unwind__prepare_access(args->maps, map, &initialized);
380
381 return (args->err || initialized) ? 1 : 0;
382 }
383
thread__prepare_access(struct thread * thread)384 static int thread__prepare_access(struct thread *thread)
385 {
386 struct thread__prepare_access_maps_cb_args args = {
387 .err = 0,
388 };
389
390 if (dwarf_callchain_users) {
391 args.maps = thread__maps(thread);
392 maps__for_each_map(thread__maps(thread), thread__prepare_access_maps_cb, &args);
393 }
394
395 return args.err;
396 }
397
thread__clone_maps(struct thread * thread,struct thread * parent,bool do_maps_clone)398 static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
399 {
400 /* This is new thread, we share map groups for process. */
401 if (thread__pid(thread) == thread__pid(parent))
402 return thread__prepare_access(thread);
403
404 if (maps__equal(thread__maps(thread), thread__maps(parent))) {
405 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
406 thread__pid(thread), thread__tid(thread),
407 thread__pid(parent), thread__tid(parent));
408 return 0;
409 }
410 /* But this one is new process, copy maps. */
411 return do_maps_clone ? maps__copy_from(thread__maps(thread), thread__maps(parent)) : 0;
412 }
413
thread__fork(struct thread * thread,struct thread * parent,u64 timestamp,bool do_maps_clone)414 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
415 {
416 if (thread__comm_set(parent)) {
417 const char *comm = thread__comm_str(parent);
418 int err;
419 if (!comm)
420 return -ENOMEM;
421 err = thread__set_comm(thread, comm, timestamp);
422 if (err)
423 return err;
424 }
425
426 thread__set_ppid(thread, thread__tid(parent));
427 return thread__clone_maps(thread, parent, do_maps_clone);
428 }
429
thread__find_cpumode_addr_location(struct thread * thread,u64 addr,bool symbols,struct addr_location * al)430 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
431 bool symbols, struct addr_location *al)
432 {
433 size_t i;
434 const u8 cpumodes[] = {
435 PERF_RECORD_MISC_USER,
436 PERF_RECORD_MISC_KERNEL,
437 PERF_RECORD_MISC_GUEST_USER,
438 PERF_RECORD_MISC_GUEST_KERNEL
439 };
440
441 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
442 if (symbols)
443 thread__find_symbol(thread, cpumodes[i], addr, al);
444 else
445 thread__find_map(thread, cpumodes[i], addr, al);
446
447 if (al->map)
448 break;
449 }
450 }
451
read_proc_e_machine_for_pid(pid_t pid)452 static uint16_t read_proc_e_machine_for_pid(pid_t pid)
453 {
454 char path[6 /* "/proc/" */ + 11 /* max length of pid */ + 5 /* "/exe\0" */];
455 int fd;
456 uint16_t e_machine = EM_NONE;
457
458 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
459 fd = open(path, O_RDONLY);
460 if (fd >= 0) {
461 _Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset");
462 _Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset");
463 if (pread(fd, &e_machine, sizeof(e_machine), 18) != sizeof(e_machine))
464 e_machine = EM_NONE;
465 close(fd);
466 }
467 return e_machine;
468 }
469
thread__e_machine_callback(struct map * map,void * machine)470 static int thread__e_machine_callback(struct map *map, void *machine)
471 {
472 struct dso *dso = map__dso(map);
473
474 _Static_assert(0 == EM_NONE, "Unexpected EM_NONE");
475 if (!dso)
476 return EM_NONE;
477
478 return dso__e_machine(dso, machine);
479 }
480
thread__e_machine(struct thread * thread,struct machine * machine)481 uint16_t thread__e_machine(struct thread *thread, struct machine *machine)
482 {
483 pid_t tid, pid;
484 uint16_t e_machine = RC_CHK_ACCESS(thread)->e_machine;
485
486 if (e_machine != EM_NONE)
487 return e_machine;
488
489 tid = thread__tid(thread);
490 pid = thread__pid(thread);
491 if (pid != tid) {
492 struct thread *parent = machine__findnew_thread(machine, pid, pid);
493
494 if (parent) {
495 e_machine = thread__e_machine(parent, machine);
496 thread__put(parent);
497 thread__set_e_machine(thread, e_machine);
498 return e_machine;
499 }
500 /* Something went wrong, fallback. */
501 }
502 /* Reading on the PID thread. First try to find from the maps. */
503 e_machine = maps__for_each_map(thread__maps(thread),
504 thread__e_machine_callback,
505 machine);
506 if (e_machine == EM_NONE) {
507 /* Maps failed, perhaps we're live with map events disabled. */
508 bool is_live = machine->machines == NULL;
509
510 if (!is_live) {
511 /* Check if the session has a data file. */
512 struct perf_session *session = container_of(machine->machines,
513 struct perf_session,
514 machines);
515
516 is_live = !!session->data;
517 }
518 /* Read from /proc/pid/exe if live. */
519 if (is_live)
520 e_machine = read_proc_e_machine_for_pid(pid);
521 }
522 if (e_machine != EM_NONE)
523 thread__set_e_machine(thread, e_machine);
524 else
525 e_machine = EM_HOST;
526 return e_machine;
527 }
528
thread__main_thread(struct machine * machine,struct thread * thread)529 struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
530 {
531 if (thread__pid(thread) == thread__tid(thread))
532 return thread__get(thread);
533
534 if (thread__pid(thread) == -1)
535 return NULL;
536
537 return machine__find_thread(machine, thread__pid(thread), thread__pid(thread));
538 }
539
thread__memcpy(struct thread * thread,struct machine * machine,void * buf,u64 ip,int len,bool * is64bit)540 int thread__memcpy(struct thread *thread, struct machine *machine,
541 void *buf, u64 ip, int len, bool *is64bit)
542 {
543 u8 cpumode = PERF_RECORD_MISC_USER;
544 struct addr_location al;
545 struct dso *dso;
546 long offset;
547
548 if (machine__kernel_ip(machine, ip))
549 cpumode = PERF_RECORD_MISC_KERNEL;
550
551 addr_location__init(&al);
552 if (!thread__find_map(thread, cpumode, ip, &al)) {
553 addr_location__exit(&al);
554 return -1;
555 }
556
557 dso = map__dso(al.map);
558
559 if (!dso || dso__data(dso)->status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0) {
560 addr_location__exit(&al);
561 return -1;
562 }
563
564 offset = map__map_ip(al.map, ip);
565 if (is64bit)
566 *is64bit = dso__is_64_bit(dso);
567
568 addr_location__exit(&al);
569
570 return dso__data_read_offset(dso, machine, offset, buf, len);
571 }
572
thread__free_stitch_list(struct thread * thread)573 void thread__free_stitch_list(struct thread *thread)
574 {
575 struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
576 struct stitch_list *pos, *tmp;
577
578 if (!lbr_stitch)
579 return;
580
581 list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
582 map_symbol__exit(&pos->cursor.ms);
583 list_del_init(&pos->node);
584 free(pos);
585 }
586
587 list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
588 list_del_init(&pos->node);
589 free(pos);
590 }
591
592 for (unsigned int i = 0 ; i < lbr_stitch->prev_lbr_cursor_size; i++)
593 map_symbol__exit(&lbr_stitch->prev_lbr_cursor[i].ms);
594
595 zfree(&lbr_stitch->prev_lbr_cursor);
596 free(thread__lbr_stitch(thread));
597 thread__set_lbr_stitch(thread, NULL);
598 }
599