Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 1 | #include "../perf.h" |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 6 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 7 | #include "session.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 8 | #include "thread.h" |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 9 | #include "thread-stack.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 10 | #include "util.h" |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 11 | #include "debug.h" |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 12 | #include "namespaces.h" |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 13 | #include "comm.h" |
Namhyung Kim | 66f066d8 | 2014-10-06 09:46:00 +0900 | [diff] [blame] | 14 | #include "unwind.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 2f3027a | 2016-04-26 12:32:50 -0300 | [diff] [blame] | 16 | #include <api/fs/fs.h> |
| 17 | |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 18 | int thread__init_map_groups(struct thread *thread, struct machine *machine) |
| 19 | { |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 20 | pid_t pid = thread->pid_; |
| 21 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 22 | if (pid == thread->tid || pid == -1) { |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 23 | thread->mg = map_groups__new(machine); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 24 | } else { |
Arnaldo Carvalho de Melo | 18ef15c | 2016-10-03 11:07:24 -0300 | [diff] [blame] | 25 | struct thread *leader = __machine__findnew_thread(machine, pid, pid); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 26 | if (leader) { |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 27 | thread->mg = map_groups__get(leader->mg); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 28 | thread__put(leader); |
| 29 | } |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | return thread->mg ? 0 : -1; |
| 33 | } |
| 34 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 35 | struct thread *thread__new(pid_t pid, pid_t tid) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 36 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 37 | char *comm_str; |
| 38 | struct comm *comm; |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 39 | struct thread *thread = zalloc(sizeof(*thread)); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 40 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 41 | if (thread != NULL) { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 42 | thread->pid_ = pid; |
| 43 | thread->tid = tid; |
| 44 | thread->ppid = -1; |
Adrian Hunter | bf49c35 | 2014-07-22 16:17:24 +0300 | [diff] [blame] | 45 | thread->cpu = -1; |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 46 | INIT_LIST_HEAD(&thread->namespaces_list); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 47 | INIT_LIST_HEAD(&thread->comm_list); |
| 48 | |
| 49 | comm_str = malloc(32); |
| 50 | if (!comm_str) |
| 51 | goto err_thread; |
| 52 | |
| 53 | snprintf(comm_str, 32, ":%d", tid); |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 54 | comm = comm__new(comm_str, 0, false); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 55 | free(comm_str); |
| 56 | if (!comm) |
| 57 | goto err_thread; |
| 58 | |
| 59 | list_add(&comm->list, &thread->comm_list); |
Elena Reshetova | e34f5b1 | 2017-02-21 17:35:02 +0200 | [diff] [blame] | 60 | refcount_set(&thread->refcnt, 1); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 61 | RB_CLEAR_NODE(&thread->rb_node); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 64 | return thread; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 65 | |
| 66 | err_thread: |
| 67 | free(thread); |
| 68 | return NULL; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 71 | void thread__delete(struct thread *thread) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 72 | { |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 73 | struct namespaces *namespaces, *tmp_namespaces; |
| 74 | struct comm *comm, *tmp_comm; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 75 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 76 | BUG_ON(!RB_EMPTY_NODE(&thread->rb_node)); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 77 | |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 78 | thread_stack__free(thread); |
| 79 | |
Adrian Hunter | 9608b84 | 2014-07-16 10:19:43 +0300 | [diff] [blame] | 80 | if (thread->mg) { |
| 81 | map_groups__put(thread->mg); |
| 82 | thread->mg = NULL; |
| 83 | } |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 84 | list_for_each_entry_safe(namespaces, tmp_namespaces, |
| 85 | &thread->namespaces_list, list) { |
| 86 | list_del(&namespaces->list); |
| 87 | namespaces__free(namespaces); |
| 88 | } |
| 89 | list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 90 | list_del(&comm->list); |
| 91 | comm__free(comm); |
| 92 | } |
Namhyung Kim | 66f066d8 | 2014-10-06 09:46:00 +0900 | [diff] [blame] | 93 | unwind__finish_access(thread); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 94 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 95 | free(thread); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 96 | } |
| 97 | |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 98 | struct thread *thread__get(struct thread *thread) |
| 99 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 100 | if (thread) |
Elena Reshetova | e34f5b1 | 2017-02-21 17:35:02 +0200 | [diff] [blame] | 101 | refcount_inc(&thread->refcnt); |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 102 | return thread; |
| 103 | } |
| 104 | |
| 105 | void thread__put(struct thread *thread) |
| 106 | { |
Elena Reshetova | e34f5b1 | 2017-02-21 17:35:02 +0200 | [diff] [blame] | 107 | if (thread && refcount_dec_and_test(&thread->refcnt)) { |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 108 | /* |
| 109 | * Remove it from the dead_threads list, as last reference |
| 110 | * is gone. |
| 111 | */ |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 112 | list_del_init(&thread->node); |
| 113 | thread__delete(thread); |
| 114 | } |
| 115 | } |
| 116 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 117 | struct namespaces *thread__namespaces(const struct thread *thread) |
| 118 | { |
| 119 | if (list_empty(&thread->namespaces_list)) |
| 120 | return NULL; |
| 121 | |
| 122 | return list_first_entry(&thread->namespaces_list, struct namespaces, list); |
| 123 | } |
| 124 | |
| 125 | int thread__set_namespaces(struct thread *thread, u64 timestamp, |
| 126 | struct namespaces_event *event) |
| 127 | { |
| 128 | struct namespaces *new, *curr = thread__namespaces(thread); |
| 129 | |
| 130 | new = namespaces__new(event); |
| 131 | if (!new) |
| 132 | return -ENOMEM; |
| 133 | |
| 134 | list_add(&new->list, &thread->namespaces_list); |
| 135 | |
| 136 | if (timestamp && curr) { |
| 137 | /* |
| 138 | * setns syscall must have changed few or all the namespaces |
| 139 | * of this thread. Update end time for the namespaces |
| 140 | * previously used. |
| 141 | */ |
| 142 | curr = list_next_entry(new, list); |
| 143 | curr->end_time = timestamp; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 149 | struct comm *thread__comm(const struct thread *thread) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 150 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 151 | if (list_empty(&thread->comm_list)) |
| 152 | return NULL; |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 153 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 154 | return list_first_entry(&thread->comm_list, struct comm, list); |
| 155 | } |
| 156 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 157 | struct comm *thread__exec_comm(const struct thread *thread) |
| 158 | { |
| 159 | struct comm *comm, *last = NULL; |
| 160 | |
| 161 | list_for_each_entry(comm, &thread->comm_list, list) { |
| 162 | if (comm->exec) |
| 163 | return comm; |
| 164 | last = comm; |
| 165 | } |
| 166 | |
| 167 | return last; |
| 168 | } |
| 169 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 170 | int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, |
| 171 | bool exec) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 172 | { |
| 173 | struct comm *new, *curr = thread__comm(thread); |
| 174 | |
Adrian Hunter | a848080 | 2014-11-11 16:16:41 +0200 | [diff] [blame] | 175 | /* Override the default :tid entry */ |
| 176 | if (!thread->comm_set) { |
Arnaldo Carvalho de Melo | 18ef15c | 2016-10-03 11:07:24 -0300 | [diff] [blame] | 177 | int err = comm__override(curr, str, timestamp, exec); |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 178 | if (err) |
| 179 | return err; |
Frederic Weisbecker | a5285ad | 2013-11-16 02:02:09 +0100 | [diff] [blame] | 180 | } else { |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 181 | new = comm__new(str, timestamp, exec); |
Frederic Weisbecker | a5285ad | 2013-11-16 02:02:09 +0100 | [diff] [blame] | 182 | if (!new) |
| 183 | return -ENOMEM; |
| 184 | list_add(&new->list, &thread->comm_list); |
Namhyung Kim | 380b514 | 2014-10-06 09:46:01 +0900 | [diff] [blame] | 185 | |
| 186 | if (exec) |
| 187 | unwind__flush_access(thread); |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 188 | } |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 189 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 190 | thread->comm_set = true; |
| 191 | |
| 192 | return 0; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Arnaldo Carvalho de Melo | 2f3027a | 2016-04-26 12:32:50 -0300 | [diff] [blame] | 195 | int thread__set_comm_from_proc(struct thread *thread) |
| 196 | { |
| 197 | char path[64]; |
| 198 | char *comm = NULL; |
| 199 | size_t sz; |
| 200 | int err = -1; |
| 201 | |
| 202 | if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", |
| 203 | thread->pid_, thread->tid) >= (int)sizeof(path)) && |
| 204 | procfs__read_str(path, &comm, &sz) == 0) { |
| 205 | comm[sz - 1] = '\0'; |
| 206 | err = thread__set_comm(thread, comm, 0); |
| 207 | } |
| 208 | |
| 209 | return err; |
| 210 | } |
| 211 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 212 | const char *thread__comm_str(const struct thread *thread) |
| 213 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 214 | const struct comm *comm = thread__comm(thread); |
| 215 | |
| 216 | if (!comm) |
| 217 | return NULL; |
| 218 | |
| 219 | return comm__str(comm); |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 222 | /* CHECKME: it should probably better return the max comm len from its comm list */ |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 223 | int thread__comm_len(struct thread *thread) |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 224 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 225 | if (!thread->comm_len) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 226 | const char *comm = thread__comm_str(thread); |
| 227 | if (!comm) |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 228 | return 0; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 229 | thread->comm_len = strlen(comm); |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 232 | return thread->comm_len; |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 235 | size_t thread__fprintf(struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 236 | { |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 237 | return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + |
Jiri Olsa | acebd40 | 2014-07-14 23:46:47 +0200 | [diff] [blame] | 238 | map_groups__fprintf(thread->mg, fp); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 239 | } |
| 240 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 241 | int thread__insert_map(struct thread *thread, struct map *map) |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 242 | { |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 243 | int ret; |
| 244 | |
Jiri Olsa | a287332 | 2016-07-04 14:16:22 +0200 | [diff] [blame] | 245 | ret = unwind__prepare_access(thread, map, NULL); |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 246 | if (ret) |
| 247 | return ret; |
| 248 | |
Jiri Olsa | acebd40 | 2014-07-14 23:46:47 +0200 | [diff] [blame] | 249 | map_groups__fixup_overlappings(thread->mg, map, stderr); |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 250 | map_groups__insert(thread->mg, map); |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 251 | |
| 252 | return 0; |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 253 | } |
| 254 | |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 255 | static int __thread__prepare_access(struct thread *thread) |
| 256 | { |
| 257 | bool initialized = false; |
| 258 | int i, err = 0; |
| 259 | |
| 260 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
| 261 | struct maps *maps = &thread->mg->maps[i]; |
| 262 | struct map *map; |
| 263 | |
| 264 | pthread_rwlock_rdlock(&maps->lock); |
| 265 | |
| 266 | for (map = maps__first(maps); map; map = map__next(map)) { |
| 267 | err = unwind__prepare_access(thread, map, &initialized); |
| 268 | if (err || initialized) |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | pthread_rwlock_unlock(&maps->lock); |
| 273 | } |
| 274 | |
| 275 | return err; |
| 276 | } |
| 277 | |
| 278 | static int thread__prepare_access(struct thread *thread) |
| 279 | { |
| 280 | int err = 0; |
| 281 | |
| 282 | if (symbol_conf.use_callchain) |
| 283 | err = __thread__prepare_access(thread); |
| 284 | |
| 285 | return err; |
| 286 | } |
| 287 | |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 288 | static int thread__clone_map_groups(struct thread *thread, |
| 289 | struct thread *parent) |
| 290 | { |
| 291 | int i; |
| 292 | |
| 293 | /* This is new thread, we share map groups for process. */ |
| 294 | if (thread->pid_ == parent->pid_) |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 295 | return thread__prepare_access(thread); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 296 | |
Adrian Hunter | 0d7e7ac | 2015-08-19 17:29:19 +0300 | [diff] [blame] | 297 | if (thread->mg == parent->mg) { |
| 298 | pr_debug("broken map groups on thread %d/%d parent %d/%d\n", |
| 299 | thread->pid_, thread->tid, parent->pid_, parent->tid); |
| 300 | return 0; |
| 301 | } |
| 302 | |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 303 | /* But this one is new process, copy maps. */ |
| 304 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 305 | if (map_groups__clone(thread, parent->mg, i) < 0) |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 306 | return -ENOMEM; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 311 | int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 312 | { |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 313 | if (parent->comm_set) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 314 | const char *comm = thread__comm_str(parent); |
Arnaldo Carvalho de Melo | 18ef15c | 2016-10-03 11:07:24 -0300 | [diff] [blame] | 315 | int err; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 316 | if (!comm) |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 317 | return -ENOMEM; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 318 | err = thread__set_comm(thread, comm, timestamp); |
David Ahern | 8d00be8 | 2013-12-10 21:35:38 -0700 | [diff] [blame] | 319 | if (err) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 320 | return err; |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 321 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 322 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 323 | thread->ppid = parent->tid; |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 324 | return thread__clone_map_groups(thread, parent); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 325 | } |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 326 | |
| 327 | void thread__find_cpumode_addr_location(struct thread *thread, |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 328 | enum map_type type, u64 addr, |
| 329 | struct addr_location *al) |
| 330 | { |
| 331 | size_t i; |
Eric Engestrom | 3b556bc | 2016-04-25 10:47:54 +0100 | [diff] [blame] | 332 | const u8 cpumodes[] = { |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 333 | PERF_RECORD_MISC_USER, |
| 334 | PERF_RECORD_MISC_KERNEL, |
| 335 | PERF_RECORD_MISC_GUEST_USER, |
| 336 | PERF_RECORD_MISC_GUEST_KERNEL |
| 337 | }; |
| 338 | |
| 339 | for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 340 | thread__find_addr_location(thread, cpumodes[i], type, addr, al); |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 341 | if (al->map) |
| 342 | break; |
| 343 | } |
| 344 | } |
Andi Kleen | 480ca35 | 2016-05-23 17:52:24 -0700 | [diff] [blame] | 345 | |
| 346 | struct thread *thread__main_thread(struct machine *machine, struct thread *thread) |
| 347 | { |
| 348 | if (thread->pid_ == thread->tid) |
| 349 | return thread__get(thread); |
| 350 | |
| 351 | if (thread->pid_ == -1) |
| 352 | return NULL; |
| 353 | |
| 354 | return machine__find_thread(machine, thread->pid_, thread->pid_); |
| 355 | } |