thread.c 15 KB

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