thread.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "../perf.h"
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <linux/kernel.h>
  8. #include "session.h"
  9. #include "thread.h"
  10. #include "thread-stack.h"
  11. #include "util.h"
  12. #include "debug.h"
  13. #include "namespaces.h"
  14. #include "comm.h"
  15. #include "unwind.h"
  16. #include <api/fs/fs.h>
  17. int thread__init_map_groups(struct thread *thread, struct machine *machine)
  18. {
  19. pid_t pid = thread->pid_;
  20. if (pid == thread->tid || pid == -1) {
  21. thread->mg = map_groups__new(machine);
  22. } else {
  23. struct thread *leader = __machine__findnew_thread(machine, pid, pid);
  24. if (leader) {
  25. thread->mg = map_groups__get(leader->mg);
  26. thread__put(leader);
  27. }
  28. }
  29. return thread->mg ? 0 : -1;
  30. }
  31. struct thread *thread__new(pid_t pid, pid_t tid)
  32. {
  33. char *comm_str;
  34. struct comm *comm;
  35. struct thread *thread = zalloc(sizeof(*thread));
  36. if (thread != NULL) {
  37. thread->pid_ = pid;
  38. thread->tid = tid;
  39. thread->ppid = -1;
  40. thread->cpu = -1;
  41. INIT_LIST_HEAD(&thread->namespaces_list);
  42. INIT_LIST_HEAD(&thread->comm_list);
  43. init_rwsem(&thread->namespaces_lock);
  44. init_rwsem(&thread->comm_lock);
  45. comm_str = malloc(32);
  46. if (!comm_str)
  47. goto err_thread;
  48. snprintf(comm_str, 32, ":%d", tid);
  49. comm = comm__new(comm_str, 0, false);
  50. free(comm_str);
  51. if (!comm)
  52. goto err_thread;
  53. list_add(&comm->list, &thread->comm_list);
  54. refcount_set(&thread->refcnt, 1);
  55. RB_CLEAR_NODE(&thread->rb_node);
  56. /* Thread holds first ref to nsdata. */
  57. thread->nsinfo = nsinfo__new(pid);
  58. }
  59. return thread;
  60. err_thread:
  61. free(thread);
  62. return NULL;
  63. }
  64. void thread__delete(struct thread *thread)
  65. {
  66. struct namespaces *namespaces, *tmp_namespaces;
  67. struct comm *comm, *tmp_comm;
  68. BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
  69. thread_stack__free(thread);
  70. if (thread->mg) {
  71. map_groups__put(thread->mg);
  72. thread->mg = NULL;
  73. }
  74. down_write(&thread->namespaces_lock);
  75. list_for_each_entry_safe(namespaces, tmp_namespaces,
  76. &thread->namespaces_list, list) {
  77. list_del(&namespaces->list);
  78. namespaces__free(namespaces);
  79. }
  80. up_write(&thread->namespaces_lock);
  81. down_write(&thread->comm_lock);
  82. list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
  83. list_del(&comm->list);
  84. comm__free(comm);
  85. }
  86. up_write(&thread->comm_lock);
  87. unwind__finish_access(thread);
  88. nsinfo__zput(thread->nsinfo);
  89. exit_rwsem(&thread->namespaces_lock);
  90. exit_rwsem(&thread->comm_lock);
  91. free(thread);
  92. }
  93. struct thread *thread__get(struct thread *thread)
  94. {
  95. if (thread)
  96. refcount_inc(&thread->refcnt);
  97. return thread;
  98. }
  99. void thread__put(struct thread *thread)
  100. {
  101. if (thread && refcount_dec_and_test(&thread->refcnt)) {
  102. /*
  103. * Remove it from the dead_threads list, as last reference
  104. * is gone.
  105. */
  106. list_del_init(&thread->node);
  107. thread__delete(thread);
  108. }
  109. }
  110. static struct namespaces *__thread__namespaces(const struct thread *thread)
  111. {
  112. if (list_empty(&thread->namespaces_list))
  113. return NULL;
  114. return list_first_entry(&thread->namespaces_list, struct namespaces, list);
  115. }
  116. struct namespaces *thread__namespaces(const struct thread *thread)
  117. {
  118. struct namespaces *ns;
  119. down_read((struct rw_semaphore *)&thread->namespaces_lock);
  120. ns = __thread__namespaces(thread);
  121. up_read((struct rw_semaphore *)&thread->namespaces_lock);
  122. return ns;
  123. }
  124. static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
  125. struct namespaces_event *event)
  126. {
  127. struct namespaces *new, *curr = __thread__namespaces(thread);
  128. new = namespaces__new(event);
  129. if (!new)
  130. return -ENOMEM;
  131. list_add(&new->list, &thread->namespaces_list);
  132. if (timestamp && curr) {
  133. /*
  134. * setns syscall must have changed few or all the namespaces
  135. * of this thread. Update end time for the namespaces
  136. * previously used.
  137. */
  138. curr = list_next_entry(new, list);
  139. curr->end_time = timestamp;
  140. }
  141. return 0;
  142. }
  143. int thread__set_namespaces(struct thread *thread, u64 timestamp,
  144. struct namespaces_event *event)
  145. {
  146. int ret;
  147. down_write(&thread->namespaces_lock);
  148. ret = __thread__set_namespaces(thread, timestamp, event);
  149. up_write(&thread->namespaces_lock);
  150. return ret;
  151. }
  152. struct comm *thread__comm(const struct thread *thread)
  153. {
  154. if (list_empty(&thread->comm_list))
  155. return NULL;
  156. return list_first_entry(&thread->comm_list, struct comm, list);
  157. }
  158. struct comm *thread__exec_comm(const struct thread *thread)
  159. {
  160. struct comm *comm, *last = NULL, *second_last = NULL;
  161. list_for_each_entry(comm, &thread->comm_list, list) {
  162. if (comm->exec)
  163. return comm;
  164. second_last = last;
  165. last = comm;
  166. }
  167. /*
  168. * 'last' with no start time might be the parent's comm of a synthesized
  169. * thread (created by processing a synthesized fork event). For a main
  170. * thread, that is very probably wrong. Prefer a later comm to avoid
  171. * that case.
  172. */
  173. if (second_last && !last->start && thread->pid_ == thread->tid)
  174. return second_last;
  175. return last;
  176. }
  177. static int ____thread__set_comm(struct thread *thread, const char *str,
  178. u64 timestamp, bool exec)
  179. {
  180. struct comm *new, *curr = thread__comm(thread);
  181. /* Override the default :tid entry */
  182. if (!thread->comm_set) {
  183. int err = comm__override(curr, str, timestamp, exec);
  184. if (err)
  185. return err;
  186. } else {
  187. new = comm__new(str, timestamp, exec);
  188. if (!new)
  189. return -ENOMEM;
  190. list_add(&new->list, &thread->comm_list);
  191. if (exec)
  192. unwind__flush_access(thread);
  193. }
  194. thread->comm_set = true;
  195. return 0;
  196. }
  197. int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
  198. bool exec)
  199. {
  200. int ret;
  201. down_write(&thread->comm_lock);
  202. ret = ____thread__set_comm(thread, str, timestamp, exec);
  203. up_write(&thread->comm_lock);
  204. return ret;
  205. }
  206. int thread__set_comm_from_proc(struct thread *thread)
  207. {
  208. char path[64];
  209. char *comm = NULL;
  210. size_t sz;
  211. int err = -1;
  212. if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
  213. thread->pid_, thread->tid) >= (int)sizeof(path)) &&
  214. procfs__read_str(path, &comm, &sz) == 0) {
  215. comm[sz - 1] = '\0';
  216. err = thread__set_comm(thread, comm, 0);
  217. }
  218. return err;
  219. }
  220. static const char *__thread__comm_str(const struct thread *thread)
  221. {
  222. const struct comm *comm = thread__comm(thread);
  223. if (!comm)
  224. return NULL;
  225. return comm__str(comm);
  226. }
  227. const char *thread__comm_str(const struct thread *thread)
  228. {
  229. const char *str;
  230. down_read((struct rw_semaphore *)&thread->comm_lock);
  231. str = __thread__comm_str(thread);
  232. up_read((struct rw_semaphore *)&thread->comm_lock);
  233. return str;
  234. }
  235. /* CHECKME: it should probably better return the max comm len from its comm list */
  236. int thread__comm_len(struct thread *thread)
  237. {
  238. if (!thread->comm_len) {
  239. const char *comm = thread__comm_str(thread);
  240. if (!comm)
  241. return 0;
  242. thread->comm_len = strlen(comm);
  243. }
  244. return thread->comm_len;
  245. }
  246. size_t thread__fprintf(struct thread *thread, FILE *fp)
  247. {
  248. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  249. map_groups__fprintf(thread->mg, fp);
  250. }
  251. int thread__insert_map(struct thread *thread, struct map *map)
  252. {
  253. int ret;
  254. ret = unwind__prepare_access(thread, map, NULL);
  255. if (ret)
  256. return ret;
  257. map_groups__fixup_overlappings(thread->mg, map, stderr);
  258. map_groups__insert(thread->mg, map);
  259. return 0;
  260. }
  261. static int __thread__prepare_access(struct thread *thread)
  262. {
  263. bool initialized = false;
  264. int err = 0;
  265. struct maps *maps = &thread->mg->maps;
  266. struct map *map;
  267. down_read(&maps->lock);
  268. for (map = maps__first(maps); map; map = map__next(map)) {
  269. err = unwind__prepare_access(thread, map, &initialized);
  270. if (err || initialized)
  271. break;
  272. }
  273. up_read(&maps->lock);
  274. return err;
  275. }
  276. static int thread__prepare_access(struct thread *thread)
  277. {
  278. int err = 0;
  279. if (symbol_conf.use_callchain)
  280. err = __thread__prepare_access(thread);
  281. return err;
  282. }
  283. static int thread__clone_map_groups(struct thread *thread,
  284. struct thread *parent)
  285. {
  286. /* This is new thread, we share map groups for process. */
  287. if (thread->pid_ == parent->pid_)
  288. return thread__prepare_access(thread);
  289. if (thread->mg == parent->mg) {
  290. pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
  291. thread->pid_, thread->tid, parent->pid_, parent->tid);
  292. return 0;
  293. }
  294. /* But this one is new process, copy maps. */
  295. if (map_groups__clone(thread, parent->mg) < 0)
  296. return -ENOMEM;
  297. return 0;
  298. }
  299. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
  300. {
  301. if (parent->comm_set) {
  302. const char *comm = thread__comm_str(parent);
  303. int err;
  304. if (!comm)
  305. return -ENOMEM;
  306. err = thread__set_comm(thread, comm, timestamp);
  307. if (err)
  308. return err;
  309. }
  310. thread->ppid = parent->tid;
  311. return thread__clone_map_groups(thread, parent);
  312. }
  313. void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
  314. struct addr_location *al)
  315. {
  316. size_t i;
  317. const u8 cpumodes[] = {
  318. PERF_RECORD_MISC_USER,
  319. PERF_RECORD_MISC_KERNEL,
  320. PERF_RECORD_MISC_GUEST_USER,
  321. PERF_RECORD_MISC_GUEST_KERNEL
  322. };
  323. for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
  324. thread__find_symbol(thread, cpumodes[i], addr, al);
  325. if (al->map)
  326. break;
  327. }
  328. }
  329. struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
  330. {
  331. if (thread->pid_ == thread->tid)
  332. return thread__get(thread);
  333. if (thread->pid_ == -1)
  334. return NULL;
  335. return machine__find_thread(machine, thread->pid_, thread->pid_);
  336. }