thread.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 i, err = 0;
  248. for (i = 0; i < MAP__NR_TYPES; ++i) {
  249. struct maps *maps = &thread->mg->maps[i];
  250. struct map *map;
  251. down_read(&maps->lock);
  252. for (map = maps__first(maps); map; map = map__next(map)) {
  253. err = unwind__prepare_access(thread, map, &initialized);
  254. if (err || initialized)
  255. break;
  256. }
  257. up_read(&maps->lock);
  258. }
  259. return err;
  260. }
  261. static int thread__prepare_access(struct thread *thread)
  262. {
  263. int err = 0;
  264. if (symbol_conf.use_callchain)
  265. err = __thread__prepare_access(thread);
  266. return err;
  267. }
  268. static int thread__clone_map_groups(struct thread *thread,
  269. struct thread *parent)
  270. {
  271. int i;
  272. /* This is new thread, we share map groups for process. */
  273. if (thread->pid_ == parent->pid_)
  274. return thread__prepare_access(thread);
  275. if (thread->mg == parent->mg) {
  276. pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
  277. thread->pid_, thread->tid, parent->pid_, parent->tid);
  278. return 0;
  279. }
  280. /* But this one is new process, copy maps. */
  281. for (i = 0; i < MAP__NR_TYPES; ++i)
  282. if (map_groups__clone(thread, parent->mg, i) < 0)
  283. return -ENOMEM;
  284. return 0;
  285. }
  286. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
  287. {
  288. if (parent->comm_set) {
  289. const char *comm = thread__comm_str(parent);
  290. int err;
  291. if (!comm)
  292. return -ENOMEM;
  293. err = thread__set_comm(thread, comm, timestamp);
  294. if (err)
  295. return err;
  296. }
  297. thread->ppid = parent->tid;
  298. return thread__clone_map_groups(thread, parent);
  299. }
  300. void thread__find_cpumode_addr_location(struct thread *thread,
  301. enum map_type type, u64 addr,
  302. struct addr_location *al)
  303. {
  304. size_t i;
  305. const u8 cpumodes[] = {
  306. PERF_RECORD_MISC_USER,
  307. PERF_RECORD_MISC_KERNEL,
  308. PERF_RECORD_MISC_GUEST_USER,
  309. PERF_RECORD_MISC_GUEST_KERNEL
  310. };
  311. for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
  312. __thread__find_symbol(thread, cpumodes[i], type, addr, al);
  313. if (al->map)
  314. break;
  315. }
  316. }
  317. struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
  318. {
  319. if (thread->pid_ == thread->tid)
  320. return thread__get(thread);
  321. if (thread->pid_ == -1)
  322. return NULL;
  323. return machine__find_thread(machine, thread->pid_, thread->pid_);
  324. }