thread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <linux/kernel.h>
  7. #include <linux/zalloc.h>
  8. #include "dso.h"
  9. #include "session.h"
  10. #include "thread.h"
  11. #include "thread-stack.h"
  12. #include "debug.h"
  13. #include "namespaces.h"
  14. #include "comm.h"
  15. #include "map.h"
  16. #include "symbol.h"
  17. #include "unwind.h"
  18. #include "callchain.h"
  19. #include <api/fs/fs.h>
  20. int thread__init_maps(struct thread *thread, struct machine *machine)
  21. {
  22. pid_t pid = thread->pid_;
  23. if (pid == thread->tid || pid == -1) {
  24. thread->maps = maps__new(machine);
  25. } else {
  26. struct thread *leader = __machine__findnew_thread(machine, pid, pid);
  27. if (leader) {
  28. thread->maps = maps__get(leader->maps);
  29. thread__put(leader);
  30. }
  31. }
  32. return thread->maps ? 0 : -1;
  33. }
  34. struct thread *thread__new(pid_t pid, pid_t tid)
  35. {
  36. char *comm_str;
  37. struct comm *comm;
  38. struct thread *thread = zalloc(sizeof(*thread));
  39. if (thread != NULL) {
  40. thread->pid_ = pid;
  41. thread->tid = tid;
  42. thread->ppid = -1;
  43. thread->cpu = -1;
  44. thread->lbr_stitch_enable = false;
  45. INIT_LIST_HEAD(&thread->namespaces_list);
  46. INIT_LIST_HEAD(&thread->comm_list);
  47. init_rwsem(&thread->namespaces_lock);
  48. init_rwsem(&thread->comm_lock);
  49. comm_str = malloc(32);
  50. if (!comm_str)
  51. goto err_thread;
  52. snprintf(comm_str, 32, ":%d", tid);
  53. comm = comm__new(comm_str, 0, false);
  54. free(comm_str);
  55. if (!comm)
  56. goto err_thread;
  57. list_add(&comm->list, &thread->comm_list);
  58. refcount_set(&thread->refcnt, 1);
  59. RB_CLEAR_NODE(&thread->rb_node);
  60. /* Thread holds first ref to nsdata. */
  61. thread->nsinfo = nsinfo__new(pid);
  62. srccode_state_init(&thread->srccode_state);
  63. }
  64. return thread;
  65. err_thread:
  66. free(thread);
  67. return NULL;
  68. }
  69. void thread__delete(struct thread *thread)
  70. {
  71. struct namespaces *namespaces, *tmp_namespaces;
  72. struct comm *comm, *tmp_comm;
  73. BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
  74. thread_stack__free(thread);
  75. if (thread->maps) {
  76. maps__put(thread->maps);
  77. thread->maps = NULL;
  78. }
  79. down_write(&thread->namespaces_lock);
  80. list_for_each_entry_safe(namespaces, tmp_namespaces,
  81. &thread->namespaces_list, list) {
  82. list_del_init(&namespaces->list);
  83. namespaces__free(namespaces);
  84. }
  85. up_write(&thread->namespaces_lock);
  86. down_write(&thread->comm_lock);
  87. list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
  88. list_del_init(&comm->list);
  89. comm__free(comm);
  90. }
  91. up_write(&thread->comm_lock);
  92. nsinfo__zput(thread->nsinfo);
  93. srccode_state_free(&thread->srccode_state);
  94. exit_rwsem(&thread->namespaces_lock);
  95. exit_rwsem(&thread->comm_lock);
  96. thread__free_stitch_list(thread);
  97. free(thread);
  98. }
  99. struct thread *thread__get(struct thread *thread)
  100. {
  101. if (thread)
  102. refcount_inc(&thread->refcnt);
  103. return thread;
  104. }
  105. void thread__put(struct thread *thread)
  106. {
  107. if (thread && refcount_dec_and_test(&thread->refcnt)) {
  108. /*
  109. * Remove it from the dead threads list, as last reference is
  110. * gone, if it is in a dead threads list.
  111. *
  112. * We may not be there anymore if say, the machine where it was
  113. * stored was already deleted, so we already removed it from
  114. * the dead threads and some other piece of code still keeps a
  115. * reference.
  116. *
  117. * This is what 'perf sched' does and finally drops it in
  118. * perf_sched__lat(), where it calls perf_sched__read_events(),
  119. * that processes the events by creating a session and deleting
  120. * it, which ends up destroying the list heads for the dead
  121. * threads, but before it does that it removes all threads from
  122. * it using list_del_init().
  123. *
  124. * So we need to check here if it is in a dead threads list and
  125. * if so, remove it before finally deleting the thread, to avoid
  126. * an use after free situation.
  127. */
  128. if (!list_empty(&thread->node))
  129. list_del_init(&thread->node);
  130. thread__delete(thread);
  131. }
  132. }
  133. static struct namespaces *__thread__namespaces(const struct thread *thread)
  134. {
  135. if (list_empty(&thread->namespaces_list))
  136. return NULL;
  137. return list_first_entry(&thread->namespaces_list, struct namespaces, list);
  138. }
  139. struct namespaces *thread__namespaces(struct thread *thread)
  140. {
  141. struct namespaces *ns;
  142. down_read(&thread->namespaces_lock);
  143. ns = __thread__namespaces(thread);
  144. up_read(&thread->namespaces_lock);
  145. return ns;
  146. }
  147. static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
  148. struct perf_record_namespaces *event)
  149. {
  150. struct namespaces *new, *curr = __thread__namespaces(thread);
  151. new = namespaces__new(event);
  152. if (!new)
  153. return -ENOMEM;
  154. list_add(&new->list, &thread->namespaces_list);
  155. if (timestamp && curr) {
  156. /*
  157. * setns syscall must have changed few or all the namespaces
  158. * of this thread. Update end time for the namespaces
  159. * previously used.
  160. */
  161. curr = list_next_entry(new, list);
  162. curr->end_time = timestamp;
  163. }
  164. return 0;
  165. }
  166. int thread__set_namespaces(struct thread *thread, u64 timestamp,
  167. struct perf_record_namespaces *event)
  168. {
  169. int ret;
  170. down_write(&thread->namespaces_lock);
  171. ret = __thread__set_namespaces(thread, timestamp, event);
  172. up_write(&thread->namespaces_lock);
  173. return ret;
  174. }
  175. struct comm *thread__comm(const struct thread *thread)
  176. {
  177. if (list_empty(&thread->comm_list))
  178. return NULL;
  179. return list_first_entry(&thread->comm_list, struct comm, list);
  180. }
  181. struct comm *thread__exec_comm(const struct thread *thread)
  182. {
  183. struct comm *comm, *last = NULL, *second_last = NULL;
  184. list_for_each_entry(comm, &thread->comm_list, list) {
  185. if (comm->exec)
  186. return comm;
  187. second_last = last;
  188. last = comm;
  189. }
  190. /*
  191. * 'last' with no start time might be the parent's comm of a synthesized
  192. * thread (created by processing a synthesized fork event). For a main
  193. * thread, that is very probably wrong. Prefer a later comm to avoid
  194. * that case.
  195. */
  196. if (second_last && !last->start && thread->pid_ == thread->tid)
  197. return second_last;
  198. return last;
  199. }
  200. static int ____thread__set_comm(struct thread *thread, const char *str,
  201. u64 timestamp, bool exec)
  202. {
  203. struct comm *new, *curr = thread__comm(thread);
  204. /* Override the default :tid entry */
  205. if (!thread->comm_set) {
  206. int err = comm__override(curr, str, timestamp, exec);
  207. if (err)
  208. return err;
  209. } else {
  210. new = comm__new(str, timestamp, exec);
  211. if (!new)
  212. return -ENOMEM;
  213. list_add(&new->list, &thread->comm_list);
  214. if (exec)
  215. unwind__flush_access(thread->maps);
  216. }
  217. thread->comm_set = true;
  218. return 0;
  219. }
  220. int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
  221. bool exec)
  222. {
  223. int ret;
  224. down_write(&thread->comm_lock);
  225. ret = ____thread__set_comm(thread, str, timestamp, exec);
  226. up_write(&thread->comm_lock);
  227. return ret;
  228. }
  229. int thread__set_comm_from_proc(struct thread *thread)
  230. {
  231. char path[64];
  232. char *comm = NULL;
  233. size_t sz;
  234. int err = -1;
  235. if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
  236. thread->pid_, thread->tid) >= (int)sizeof(path)) &&
  237. procfs__read_str(path, &comm, &sz) == 0) {
  238. comm[sz - 1] = '\0';
  239. err = thread__set_comm(thread, comm, 0);
  240. }
  241. return err;
  242. }
  243. static const char *__thread__comm_str(const struct thread *thread)
  244. {
  245. const struct comm *comm = thread__comm(thread);
  246. if (!comm)
  247. return NULL;
  248. return comm__str(comm);
  249. }
  250. const char *thread__comm_str(struct thread *thread)
  251. {
  252. const char *str;
  253. down_read(&thread->comm_lock);
  254. str = __thread__comm_str(thread);
  255. up_read(&thread->comm_lock);
  256. return str;
  257. }
  258. /* CHECKME: it should probably better return the max comm len from its comm list */
  259. int thread__comm_len(struct thread *thread)
  260. {
  261. if (!thread->comm_len) {
  262. const char *comm = thread__comm_str(thread);
  263. if (!comm)
  264. return 0;
  265. thread->comm_len = strlen(comm);
  266. }
  267. return thread->comm_len;
  268. }
  269. size_t thread__fprintf(struct thread *thread, FILE *fp)
  270. {
  271. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  272. maps__fprintf(thread->maps, fp);
  273. }
  274. int thread__insert_map(struct thread *thread, struct map *map)
  275. {
  276. int ret;
  277. ret = unwind__prepare_access(thread->maps, map, NULL);
  278. if (ret)
  279. return ret;
  280. maps__fixup_overlappings(thread->maps, map, stderr);
  281. maps__insert(thread->maps, map);
  282. return 0;
  283. }
  284. static int __thread__prepare_access(struct thread *thread)
  285. {
  286. bool initialized = false;
  287. int err = 0;
  288. struct maps *maps = thread->maps;
  289. struct map *map;
  290. down_read(&maps->lock);
  291. maps__for_each_entry(maps, map) {
  292. err = unwind__prepare_access(thread->maps, map, &initialized);
  293. if (err || initialized)
  294. break;
  295. }
  296. up_read(&maps->lock);
  297. return err;
  298. }
  299. static int thread__prepare_access(struct thread *thread)
  300. {
  301. int err = 0;
  302. if (dwarf_callchain_users)
  303. err = __thread__prepare_access(thread);
  304. return err;
  305. }
  306. static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
  307. {
  308. /* This is new thread, we share map groups for process. */
  309. if (thread->pid_ == parent->pid_)
  310. return thread__prepare_access(thread);
  311. if (thread->maps == parent->maps) {
  312. pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
  313. thread->pid_, thread->tid, parent->pid_, parent->tid);
  314. return 0;
  315. }
  316. /* But this one is new process, copy maps. */
  317. return do_maps_clone ? maps__clone(thread, parent->maps) : 0;
  318. }
  319. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
  320. {
  321. if (parent->comm_set) {
  322. const char *comm = thread__comm_str(parent);
  323. int err;
  324. if (!comm)
  325. return -ENOMEM;
  326. err = thread__set_comm(thread, comm, timestamp);
  327. if (err)
  328. return err;
  329. }
  330. thread->ppid = parent->tid;
  331. return thread__clone_maps(thread, parent, do_maps_clone);
  332. }
  333. void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
  334. struct addr_location *al)
  335. {
  336. size_t i;
  337. const u8 cpumodes[] = {
  338. PERF_RECORD_MISC_USER,
  339. PERF_RECORD_MISC_KERNEL,
  340. PERF_RECORD_MISC_GUEST_USER,
  341. PERF_RECORD_MISC_GUEST_KERNEL
  342. };
  343. for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
  344. thread__find_symbol(thread, cpumodes[i], addr, al);
  345. if (al->map)
  346. break;
  347. }
  348. }
  349. struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
  350. {
  351. if (thread->pid_ == thread->tid)
  352. return thread__get(thread);
  353. if (thread->pid_ == -1)
  354. return NULL;
  355. return machine__find_thread(machine, thread->pid_, thread->pid_);
  356. }
  357. int thread__memcpy(struct thread *thread, struct machine *machine,
  358. void *buf, u64 ip, int len, bool *is64bit)
  359. {
  360. u8 cpumode = PERF_RECORD_MISC_USER;
  361. struct addr_location al;
  362. long offset;
  363. if (machine__kernel_ip(machine, ip))
  364. cpumode = PERF_RECORD_MISC_KERNEL;
  365. if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso ||
  366. al.map->dso->data.status == DSO_DATA_STATUS_ERROR ||
  367. map__load(al.map) < 0)
  368. return -1;
  369. offset = al.map->map_ip(al.map, ip);
  370. if (is64bit)
  371. *is64bit = al.map->dso->is_64_bit;
  372. return dso__data_read_offset(al.map->dso, machine, offset, buf, len);
  373. }
  374. void thread__free_stitch_list(struct thread *thread)
  375. {
  376. struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
  377. struct stitch_list *pos, *tmp;
  378. if (!lbr_stitch)
  379. return;
  380. list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
  381. list_del_init(&pos->node);
  382. free(pos);
  383. }
  384. list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
  385. list_del_init(&pos->node);
  386. free(pos);
  387. }
  388. zfree(&lbr_stitch->prev_lbr_cursor);
  389. zfree(&thread->lbr_stitch);
  390. }