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