thread.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. 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. static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
  117. struct namespaces_event *event)
  118. {
  119. struct namespaces *new, *curr = thread__namespaces(thread);
  120. new = namespaces__new(event);
  121. if (!new)
  122. return -ENOMEM;
  123. list_add(&new->list, &thread->namespaces_list);
  124. if (timestamp && curr) {
  125. /*
  126. * setns syscall must have changed few or all the namespaces
  127. * of this thread. Update end time for the namespaces
  128. * previously used.
  129. */
  130. curr = list_next_entry(new, list);
  131. curr->end_time = timestamp;
  132. }
  133. return 0;
  134. }
  135. int thread__set_namespaces(struct thread *thread, u64 timestamp,
  136. struct namespaces_event *event)
  137. {
  138. int ret;
  139. down_write(&thread->namespaces_lock);
  140. ret = __thread__set_namespaces(thread, timestamp, event);
  141. up_write(&thread->namespaces_lock);
  142. return ret;
  143. }
  144. struct comm *thread__comm(const struct thread *thread)
  145. {
  146. if (list_empty(&thread->comm_list))
  147. return NULL;
  148. return list_first_entry(&thread->comm_list, struct comm, list);
  149. }
  150. struct comm *thread__exec_comm(const struct thread *thread)
  151. {
  152. struct comm *comm, *last = NULL;
  153. list_for_each_entry(comm, &thread->comm_list, list) {
  154. if (comm->exec)
  155. return comm;
  156. last = comm;
  157. }
  158. return last;
  159. }
  160. static int ____thread__set_comm(struct thread *thread, const char *str,
  161. u64 timestamp, bool exec)
  162. {
  163. struct comm *new, *curr = thread__comm(thread);
  164. /* Override the default :tid entry */
  165. if (!thread->comm_set) {
  166. int err = comm__override(curr, str, timestamp, exec);
  167. if (err)
  168. return err;
  169. } else {
  170. new = comm__new(str, timestamp, exec);
  171. if (!new)
  172. return -ENOMEM;
  173. list_add(&new->list, &thread->comm_list);
  174. if (exec)
  175. unwind__flush_access(thread);
  176. }
  177. thread->comm_set = true;
  178. return 0;
  179. }
  180. int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
  181. bool exec)
  182. {
  183. int ret;
  184. down_write(&thread->comm_lock);
  185. ret = ____thread__set_comm(thread, str, timestamp, exec);
  186. up_write(&thread->comm_lock);
  187. return ret;
  188. }
  189. int thread__set_comm_from_proc(struct thread *thread)
  190. {
  191. char path[64];
  192. char *comm = NULL;
  193. size_t sz;
  194. int err = -1;
  195. if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
  196. thread->pid_, thread->tid) >= (int)sizeof(path)) &&
  197. procfs__read_str(path, &comm, &sz) == 0) {
  198. comm[sz - 1] = '\0';
  199. err = thread__set_comm(thread, comm, 0);
  200. }
  201. return err;
  202. }
  203. static const char *__thread__comm_str(const struct thread *thread)
  204. {
  205. const struct comm *comm = thread__comm(thread);
  206. if (!comm)
  207. return NULL;
  208. return comm__str(comm);
  209. }
  210. const char *thread__comm_str(const struct thread *thread)
  211. {
  212. const char *str;
  213. down_read((struct rw_semaphore *)&thread->comm_lock);
  214. str = __thread__comm_str(thread);
  215. up_read((struct rw_semaphore *)&thread->comm_lock);
  216. return str;
  217. }
  218. /* CHECKME: it should probably better return the max comm len from its comm list */
  219. int thread__comm_len(struct thread *thread)
  220. {
  221. if (!thread->comm_len) {
  222. const char *comm = thread__comm_str(thread);
  223. if (!comm)
  224. return 0;
  225. thread->comm_len = strlen(comm);
  226. }
  227. return thread->comm_len;
  228. }
  229. size_t thread__fprintf(struct thread *thread, FILE *fp)
  230. {
  231. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  232. map_groups__fprintf(thread->mg, fp);
  233. }
  234. int thread__insert_map(struct thread *thread, struct map *map)
  235. {
  236. int ret;
  237. ret = unwind__prepare_access(thread, map, NULL);
  238. if (ret)
  239. return ret;
  240. map_groups__fixup_overlappings(thread->mg, map, stderr);
  241. map_groups__insert(thread->mg, map);
  242. return 0;
  243. }
  244. static int __thread__prepare_access(struct thread *thread)
  245. {
  246. bool initialized = false;
  247. int err = 0;
  248. struct maps *maps = &thread->mg->maps;
  249. struct map *map;
  250. down_read(&maps->lock);
  251. for (map = maps__first(maps); map; map = map__next(map)) {
  252. err = unwind__prepare_access(thread, map, &initialized);
  253. if (err || initialized)
  254. break;
  255. }
  256. up_read(&maps->lock);
  257. return err;
  258. }
  259. static int thread__prepare_access(struct thread *thread)
  260. {
  261. int err = 0;
  262. if (symbol_conf.use_callchain)
  263. err = __thread__prepare_access(thread);
  264. return err;
  265. }
  266. static int thread__clone_map_groups(struct thread *thread,
  267. struct thread *parent,
  268. bool do_maps_clone)
  269. {
  270. /* This is new thread, we share map groups for process. */
  271. if (thread->pid_ == parent->pid_)
  272. return thread__prepare_access(thread);
  273. if (thread->mg == parent->mg) {
  274. pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
  275. thread->pid_, thread->tid, parent->pid_, parent->tid);
  276. return 0;
  277. }
  278. /* But this one is new process, copy maps. */
  279. return do_maps_clone ? map_groups__clone(thread, parent->mg) : 0;
  280. }
  281. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
  282. {
  283. if (parent->comm_set) {
  284. const char *comm = thread__comm_str(parent);
  285. int err;
  286. if (!comm)
  287. return -ENOMEM;
  288. err = thread__set_comm(thread, comm, timestamp);
  289. if (err)
  290. return err;
  291. }
  292. thread->ppid = parent->tid;
  293. return thread__clone_map_groups(thread, parent, do_maps_clone);
  294. }
  295. void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
  296. struct addr_location *al)
  297. {
  298. size_t i;
  299. const u8 cpumodes[] = {
  300. PERF_RECORD_MISC_USER,
  301. PERF_RECORD_MISC_KERNEL,
  302. PERF_RECORD_MISC_GUEST_USER,
  303. PERF_RECORD_MISC_GUEST_KERNEL
  304. };
  305. for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
  306. thread__find_symbol(thread, cpumodes[i], addr, al);
  307. if (al->map)
  308. break;
  309. }
  310. }
  311. struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
  312. {
  313. if (thread->pid_ == thread->tid)
  314. return thread__get(thread);
  315. if (thread->pid_ == -1)
  316. return NULL;
  317. return machine__find_thread(machine, thread->pid_, thread->pid_);
  318. }