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 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 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 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 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 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 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 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 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 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 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 204 struct comm *thread__comm(struct thread *thread) 205 { 206 struct comm *res = NULL; 207 208 down_read(thread__comm_lock(thread)); 209 if (!list_empty(thread__comm_list(thread))) 210 res = list_first_entry(thread__comm_list(thread), struct comm, list); 211 up_read(thread__comm_lock(thread)); 212 return res; 213 } 214 215 struct comm *thread__exec_comm(struct thread *thread) 216 { 217 struct comm *comm, *last = NULL, *second_last = NULL; 218 219 down_read(thread__comm_lock(thread)); 220 list_for_each_entry(comm, thread__comm_list(thread), list) { 221 if (comm->exec) { 222 up_read(thread__comm_lock(thread)); 223 return comm; 224 } 225 second_last = last; 226 last = comm; 227 } 228 up_read(thread__comm_lock(thread)); 229 230 /* 231 * 'last' with no start time might be the parent's comm of a synthesized 232 * thread (created by processing a synthesized fork event). For a main 233 * thread, that is very probably wrong. Prefer a later comm to avoid 234 * that case. 235 */ 236 if (second_last && !last->start && thread__pid(thread) == thread__tid(thread)) 237 return second_last; 238 239 return last; 240 } 241 242 static int ____thread__set_comm(struct thread *thread, const char *str, 243 u64 timestamp, bool exec) 244 EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread)) 245 { 246 struct comm *new, *curr = thread__comm(thread); 247 248 /* Override the default :tid entry */ 249 if (!thread__comm_set(thread)) { 250 int err = comm__override(curr, str, timestamp, exec); 251 if (err) 252 return err; 253 } else { 254 new = comm__new(str, timestamp, exec); 255 if (!new) 256 return -ENOMEM; 257 list_add(&new->list, thread__comm_list(thread)); 258 259 if (exec) 260 unwind__flush_access(thread__maps(thread)); 261 } 262 263 thread__set_comm_set(thread, true); 264 265 return 0; 266 } 267 268 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, 269 bool exec) 270 { 271 int ret; 272 273 down_write(thread__comm_lock(thread)); 274 ret = ____thread__set_comm(thread, str, timestamp, exec); 275 up_write(thread__comm_lock(thread)); 276 return ret; 277 } 278 279 int thread__set_comm_from_proc(struct thread *thread) 280 { 281 char path[64]; 282 char *comm = NULL; 283 size_t sz; 284 int err = -1; 285 286 if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", 287 thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) && 288 procfs__read_str(path, &comm, &sz) == 0) { 289 comm[sz - 1] = '\0'; 290 err = thread__set_comm(thread, comm, 0); 291 } 292 293 return err; 294 } 295 296 static const char *__thread__comm_str(struct thread *thread) 297 { 298 const struct comm *comm = thread__comm(thread); 299 300 if (!comm) 301 return NULL; 302 303 return comm__str(comm); 304 } 305 306 const char *thread__comm_str(struct thread *thread) 307 { 308 const char *str; 309 310 down_read(thread__comm_lock(thread)); 311 str = __thread__comm_str(thread); 312 up_read(thread__comm_lock(thread)); 313 314 return str; 315 } 316 317 static int __thread__comm_len(struct thread *thread, const char *comm) 318 { 319 if (!comm) 320 return 0; 321 thread__set_comm_len(thread, strlen(comm)); 322 323 return thread__var_comm_len(thread); 324 } 325 326 /* CHECKME: it should probably better return the max comm len from its comm list */ 327 int thread__comm_len(struct thread *thread) 328 { 329 int comm_len = thread__var_comm_len(thread); 330 331 if (!comm_len) { 332 const char *comm; 333 334 down_read(thread__comm_lock(thread)); 335 comm = __thread__comm_str(thread); 336 comm_len = __thread__comm_len(thread, comm); 337 up_read(thread__comm_lock(thread)); 338 } 339 340 return comm_len; 341 } 342 343 size_t thread__fprintf(struct thread *thread, FILE *fp) 344 { 345 return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_str(thread)) + 346 maps__fprintf(thread__maps(thread), fp); 347 } 348 349 int thread__insert_map(struct thread *thread, struct map *map) 350 { 351 int ret; 352 353 ret = unwind__prepare_access(thread__maps(thread), map, NULL); 354 if (ret) 355 return ret; 356 357 return maps__fixup_overlap_and_insert(thread__maps(thread), map); 358 } 359 360 struct thread__prepare_access_maps_cb_args { 361 int err; 362 struct maps *maps; 363 }; 364 365 static int thread__prepare_access_maps_cb(struct map *map, void *data) 366 { 367 bool initialized = false; 368 struct thread__prepare_access_maps_cb_args *args = data; 369 370 args->err = unwind__prepare_access(args->maps, map, &initialized); 371 372 return (args->err || initialized) ? 1 : 0; 373 } 374 375 static int thread__prepare_access(struct thread *thread) 376 { 377 struct thread__prepare_access_maps_cb_args args = { 378 .err = 0, 379 }; 380 381 if (dwarf_callchain_users) { 382 args.maps = thread__maps(thread); 383 maps__for_each_map(thread__maps(thread), thread__prepare_access_maps_cb, &args); 384 } 385 386 return args.err; 387 } 388 389 static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone) 390 { 391 /* This is new thread, we share map groups for process. */ 392 if (thread__pid(thread) == thread__pid(parent)) 393 return thread__prepare_access(thread); 394 395 if (maps__equal(thread__maps(thread), thread__maps(parent))) { 396 pr_debug("broken map groups on thread %d/%d parent %d/%d\n", 397 thread__pid(thread), thread__tid(thread), 398 thread__pid(parent), thread__tid(parent)); 399 return 0; 400 } 401 /* But this one is new process, copy maps. */ 402 return do_maps_clone ? maps__copy_from(thread__maps(thread), thread__maps(parent)) : 0; 403 } 404 405 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone) 406 { 407 if (thread__comm_set(parent)) { 408 const char *comm = thread__comm_str(parent); 409 int err; 410 if (!comm) 411 return -ENOMEM; 412 err = thread__set_comm(thread, comm, timestamp); 413 if (err) 414 return err; 415 } 416 417 thread__set_ppid(thread, thread__tid(parent)); 418 return thread__clone_maps(thread, parent, do_maps_clone); 419 } 420 421 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, 422 struct addr_location *al) 423 { 424 size_t i; 425 const u8 cpumodes[] = { 426 PERF_RECORD_MISC_USER, 427 PERF_RECORD_MISC_KERNEL, 428 PERF_RECORD_MISC_GUEST_USER, 429 PERF_RECORD_MISC_GUEST_KERNEL 430 }; 431 432 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { 433 thread__find_symbol(thread, cpumodes[i], addr, al); 434 if (al->map) 435 break; 436 } 437 } 438 439 static uint16_t read_proc_e_machine_for_pid(pid_t pid) 440 { 441 char path[6 /* "/proc/" */ + 11 /* max length of pid */ + 5 /* "/exe\0" */]; 442 int fd; 443 uint16_t e_machine = EM_NONE; 444 445 snprintf(path, sizeof(path), "/proc/%d/exe", pid); 446 fd = open(path, O_RDONLY); 447 if (fd >= 0) { 448 _Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset"); 449 _Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset"); 450 if (pread(fd, &e_machine, sizeof(e_machine), 18) != sizeof(e_machine)) 451 e_machine = EM_NONE; 452 close(fd); 453 } 454 return e_machine; 455 } 456 457 static int thread__e_machine_callback(struct map *map, void *machine) 458 { 459 struct dso *dso = map__dso(map); 460 461 _Static_assert(0 == EM_NONE, "Unexpected EM_NONE"); 462 if (!dso) 463 return EM_NONE; 464 465 return dso__e_machine(dso, machine); 466 } 467 468 uint16_t thread__e_machine(struct thread *thread, struct machine *machine) 469 { 470 pid_t tid, pid; 471 uint16_t e_machine = RC_CHK_ACCESS(thread)->e_machine; 472 473 if (e_machine != EM_NONE) 474 return e_machine; 475 476 tid = thread__tid(thread); 477 pid = thread__pid(thread); 478 if (pid != tid) { 479 struct thread *parent = machine__findnew_thread(machine, pid, pid); 480 481 if (parent) { 482 e_machine = thread__e_machine(parent, machine); 483 thread__put(parent); 484 thread__set_e_machine(thread, e_machine); 485 return e_machine; 486 } 487 /* Something went wrong, fallback. */ 488 } 489 /* Reading on the PID thread. First try to find from the maps. */ 490 e_machine = maps__for_each_map(thread__maps(thread), 491 thread__e_machine_callback, 492 machine); 493 if (e_machine == EM_NONE) { 494 /* Maps failed, perhaps we're live with map events disabled. */ 495 bool is_live = machine->machines == NULL; 496 497 if (!is_live) { 498 /* Check if the session has a data file. */ 499 struct perf_session *session = container_of(machine->machines, 500 struct perf_session, 501 machines); 502 503 is_live = !!session->data; 504 } 505 /* Read from /proc/pid/exe if live. */ 506 if (is_live) 507 e_machine = read_proc_e_machine_for_pid(pid); 508 } 509 if (e_machine != EM_NONE) 510 thread__set_e_machine(thread, e_machine); 511 else 512 e_machine = EM_HOST; 513 return e_machine; 514 } 515 516 struct thread *thread__main_thread(struct machine *machine, struct thread *thread) 517 { 518 if (thread__pid(thread) == thread__tid(thread)) 519 return thread__get(thread); 520 521 if (thread__pid(thread) == -1) 522 return NULL; 523 524 return machine__find_thread(machine, thread__pid(thread), thread__pid(thread)); 525 } 526 527 int thread__memcpy(struct thread *thread, struct machine *machine, 528 void *buf, u64 ip, int len, bool *is64bit) 529 { 530 u8 cpumode = PERF_RECORD_MISC_USER; 531 struct addr_location al; 532 struct dso *dso; 533 long offset; 534 535 if (machine__kernel_ip(machine, ip)) 536 cpumode = PERF_RECORD_MISC_KERNEL; 537 538 addr_location__init(&al); 539 if (!thread__find_map(thread, cpumode, ip, &al)) { 540 addr_location__exit(&al); 541 return -1; 542 } 543 544 dso = map__dso(al.map); 545 546 if (!dso || dso__data(dso)->status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0) { 547 addr_location__exit(&al); 548 return -1; 549 } 550 551 offset = map__map_ip(al.map, ip); 552 if (is64bit) 553 *is64bit = dso__is_64_bit(dso); 554 555 addr_location__exit(&al); 556 557 return dso__data_read_offset(dso, machine, offset, buf, len); 558 } 559 560 void thread__free_stitch_list(struct thread *thread) 561 { 562 struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread); 563 struct stitch_list *pos, *tmp; 564 565 if (!lbr_stitch) 566 return; 567 568 list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) { 569 map_symbol__exit(&pos->cursor.ms); 570 list_del_init(&pos->node); 571 free(pos); 572 } 573 574 list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) { 575 list_del_init(&pos->node); 576 free(pos); 577 } 578 579 for (unsigned int i = 0 ; i < lbr_stitch->prev_lbr_cursor_size; i++) 580 map_symbol__exit(&lbr_stitch->prev_lbr_cursor[i].ms); 581 582 zfree(&lbr_stitch->prev_lbr_cursor); 583 free(thread__lbr_stitch(thread)); 584 thread__set_lbr_stitch(thread, NULL); 585 } 586