1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2a43783aeSArnaldo Carvalho de Melo #include <errno.h> 36baa0a5aSFrederic Weisbecker #include <stdlib.h> 46baa0a5aSFrederic Weisbecker #include <stdio.h> 56baa0a5aSFrederic Weisbecker #include <string.h> 6877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h> 77f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 84a3cec84SArnaldo Carvalho de Melo #include "dso.h" 9b3165f41SArnaldo Carvalho de Melo #include "session.h" 106baa0a5aSFrederic Weisbecker #include "thread.h" 1100447ccdSAdrian Hunter #include "thread-stack.h" 126e086437SFrederic Weisbecker #include "debug.h" 13f3b3614aSHari Bathini #include "namespaces.h" 141902efe7SFrederic Weisbecker #include "comm.h" 1515325938SAndi Kleen #include "map.h" 16daecf9e0SArnaldo Carvalho de Melo #include "symbol.h" 1766f066d8SNamhyung Kim #include "unwind.h" 18382619c0SJiri Olsa #include "callchain.h" 196baa0a5aSFrederic Weisbecker 202f3027acSArnaldo Carvalho de Melo #include <api/fs/fs.h> 212f3027acSArnaldo Carvalho de Melo 2279b6bb73SArnaldo Carvalho de Melo int thread__init_maps(struct thread *thread, struct machine *machine) 23cddcef60SJiri Olsa { 24cddcef60SJiri Olsa pid_t pid = thread->pid_; 25cddcef60SJiri Olsa 261fcb8768SAdrian Hunter if (pid == thread->tid || pid == -1) { 27fe87797dSArnaldo Carvalho de Melo thread->maps = maps__new(machine); 28cddcef60SJiri Olsa } else { 2918ef15c6SArnaldo Carvalho de Melo struct thread *leader = __machine__findnew_thread(machine, pid, pid); 30abd82868SArnaldo Carvalho de Melo if (leader) { 31fe87797dSArnaldo Carvalho de Melo thread->maps = maps__get(leader->maps); 32abd82868SArnaldo Carvalho de Melo thread__put(leader); 33abd82868SArnaldo Carvalho de Melo } 34cddcef60SJiri Olsa } 35cddcef60SJiri Olsa 36fe87797dSArnaldo Carvalho de Melo return thread->maps ? 0 : -1; 37cddcef60SJiri Olsa } 38cddcef60SJiri Olsa 3999d725fcSAdrian Hunter struct thread *thread__new(pid_t pid, pid_t tid) 406baa0a5aSFrederic Weisbecker { 411902efe7SFrederic Weisbecker char *comm_str; 421902efe7SFrederic Weisbecker struct comm *comm; 43c824c433SArnaldo Carvalho de Melo struct thread *thread = zalloc(sizeof(*thread)); 446baa0a5aSFrederic Weisbecker 45c824c433SArnaldo Carvalho de Melo if (thread != NULL) { 46c824c433SArnaldo Carvalho de Melo thread->pid_ = pid; 47c824c433SArnaldo Carvalho de Melo thread->tid = tid; 48c824c433SArnaldo Carvalho de Melo thread->ppid = -1; 49bf49c35fSAdrian Hunter thread->cpu = -1; 50*771fd155SKan Liang thread->lbr_stitch_enable = false; 51f3b3614aSHari Bathini INIT_LIST_HEAD(&thread->namespaces_list); 521902efe7SFrederic Weisbecker INIT_LIST_HEAD(&thread->comm_list); 53b32ee9e5SKan Liang init_rwsem(&thread->namespaces_lock); 54b32ee9e5SKan Liang init_rwsem(&thread->comm_lock); 551902efe7SFrederic Weisbecker 561902efe7SFrederic Weisbecker comm_str = malloc(32); 571902efe7SFrederic Weisbecker if (!comm_str) 581902efe7SFrederic Weisbecker goto err_thread; 591902efe7SFrederic Weisbecker 601902efe7SFrederic Weisbecker snprintf(comm_str, 32, ":%d", tid); 6165de51f9SAdrian Hunter comm = comm__new(comm_str, 0, false); 621902efe7SFrederic Weisbecker free(comm_str); 631902efe7SFrederic Weisbecker if (!comm) 641902efe7SFrederic Weisbecker goto err_thread; 651902efe7SFrederic Weisbecker 661902efe7SFrederic Weisbecker list_add(&comm->list, &thread->comm_list); 67e34f5b11SElena Reshetova refcount_set(&thread->refcnt, 1); 68b91fc39fSArnaldo Carvalho de Melo RB_CLEAR_NODE(&thread->rb_node); 69843ff37bSKrister Johansen /* Thread holds first ref to nsdata. */ 70843ff37bSKrister Johansen thread->nsinfo = nsinfo__new(pid); 71dd2e18e9SAndi Kleen srccode_state_init(&thread->srccode_state); 726baa0a5aSFrederic Weisbecker } 736baa0a5aSFrederic Weisbecker 74c824c433SArnaldo Carvalho de Melo return thread; 751902efe7SFrederic Weisbecker 761902efe7SFrederic Weisbecker err_thread: 771902efe7SFrederic Weisbecker free(thread); 781902efe7SFrederic Weisbecker return NULL; 796baa0a5aSFrederic Weisbecker } 806baa0a5aSFrederic Weisbecker 81c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread) 82591765fdSArnaldo Carvalho de Melo { 83f3b3614aSHari Bathini struct namespaces *namespaces, *tmp_namespaces; 84f3b3614aSHari Bathini struct comm *comm, *tmp_comm; 851902efe7SFrederic Weisbecker 86b91fc39fSArnaldo Carvalho de Melo BUG_ON(!RB_EMPTY_NODE(&thread->rb_node)); 87b91fc39fSArnaldo Carvalho de Melo 8800447ccdSAdrian Hunter thread_stack__free(thread); 8900447ccdSAdrian Hunter 90fe87797dSArnaldo Carvalho de Melo if (thread->maps) { 91fe87797dSArnaldo Carvalho de Melo maps__put(thread->maps); 92fe87797dSArnaldo Carvalho de Melo thread->maps = NULL; 939608b84eSAdrian Hunter } 94b32ee9e5SKan Liang down_write(&thread->namespaces_lock); 95f3b3614aSHari Bathini list_for_each_entry_safe(namespaces, tmp_namespaces, 96f3b3614aSHari Bathini &thread->namespaces_list, list) { 97e56fbc9dSArnaldo Carvalho de Melo list_del_init(&namespaces->list); 98f3b3614aSHari Bathini namespaces__free(namespaces); 99f3b3614aSHari Bathini } 100b32ee9e5SKan Liang up_write(&thread->namespaces_lock); 101b32ee9e5SKan Liang 102b32ee9e5SKan Liang down_write(&thread->comm_lock); 103f3b3614aSHari Bathini list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) { 104e56fbc9dSArnaldo Carvalho de Melo list_del_init(&comm->list); 1051902efe7SFrederic Weisbecker comm__free(comm); 1061902efe7SFrederic Weisbecker } 107b32ee9e5SKan Liang up_write(&thread->comm_lock); 108b32ee9e5SKan Liang 109843ff37bSKrister Johansen nsinfo__zput(thread->nsinfo); 110dd2e18e9SAndi Kleen srccode_state_free(&thread->srccode_state); 1111902efe7SFrederic Weisbecker 112b32ee9e5SKan Liang exit_rwsem(&thread->namespaces_lock); 113b32ee9e5SKan Liang exit_rwsem(&thread->comm_lock); 114c824c433SArnaldo Carvalho de Melo free(thread); 115591765fdSArnaldo Carvalho de Melo } 116591765fdSArnaldo Carvalho de Melo 117f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread) 118f3b623b8SArnaldo Carvalho de Melo { 119b91fc39fSArnaldo Carvalho de Melo if (thread) 120e34f5b11SElena Reshetova refcount_inc(&thread->refcnt); 121f3b623b8SArnaldo Carvalho de Melo return thread; 122f3b623b8SArnaldo Carvalho de Melo } 123f3b623b8SArnaldo Carvalho de Melo 124f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread) 125f3b623b8SArnaldo Carvalho de Melo { 126e34f5b11SElena Reshetova if (thread && refcount_dec_and_test(&thread->refcnt)) { 127abd82868SArnaldo Carvalho de Melo /* 1284c00af0eSArnaldo Carvalho de Melo * Remove it from the dead threads list, as last reference is 1294c00af0eSArnaldo Carvalho de Melo * gone, if it is in a dead threads list. 1304c00af0eSArnaldo Carvalho de Melo * 1314c00af0eSArnaldo Carvalho de Melo * We may not be there anymore if say, the machine where it was 1324c00af0eSArnaldo Carvalho de Melo * stored was already deleted, so we already removed it from 1334c00af0eSArnaldo Carvalho de Melo * the dead threads and some other piece of code still keeps a 1344c00af0eSArnaldo Carvalho de Melo * reference. 1354c00af0eSArnaldo Carvalho de Melo * 1364c00af0eSArnaldo Carvalho de Melo * This is what 'perf sched' does and finally drops it in 1374c00af0eSArnaldo Carvalho de Melo * perf_sched__lat(), where it calls perf_sched__read_events(), 1384c00af0eSArnaldo Carvalho de Melo * that processes the events by creating a session and deleting 1394c00af0eSArnaldo Carvalho de Melo * it, which ends up destroying the list heads for the dead 1404c00af0eSArnaldo Carvalho de Melo * threads, but before it does that it removes all threads from 1414c00af0eSArnaldo Carvalho de Melo * it using list_del_init(). 1424c00af0eSArnaldo Carvalho de Melo * 1434c00af0eSArnaldo Carvalho de Melo * So we need to check here if it is in a dead threads list and 1444c00af0eSArnaldo Carvalho de Melo * if so, remove it before finally deleting the thread, to avoid 1454c00af0eSArnaldo Carvalho de Melo * an use after free situation. 146abd82868SArnaldo Carvalho de Melo */ 1474c00af0eSArnaldo Carvalho de Melo if (!list_empty(&thread->node)) 148f3b623b8SArnaldo Carvalho de Melo list_del_init(&thread->node); 149f3b623b8SArnaldo Carvalho de Melo thread__delete(thread); 150f3b623b8SArnaldo Carvalho de Melo } 151f3b623b8SArnaldo Carvalho de Melo } 152f3b623b8SArnaldo Carvalho de Melo 1536584140bSNamhyung Kim static struct namespaces *__thread__namespaces(const struct thread *thread) 154f3b3614aSHari Bathini { 155f3b3614aSHari Bathini if (list_empty(&thread->namespaces_list)) 156f3b3614aSHari Bathini return NULL; 157f3b3614aSHari Bathini 158f3b3614aSHari Bathini return list_first_entry(&thread->namespaces_list, struct namespaces, list); 159f3b3614aSHari Bathini } 160f3b3614aSHari Bathini 1617cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread) 1626584140bSNamhyung Kim { 1636584140bSNamhyung Kim struct namespaces *ns; 1646584140bSNamhyung Kim 1657cb10a08SNamhyung Kim down_read(&thread->namespaces_lock); 1666584140bSNamhyung Kim ns = __thread__namespaces(thread); 1677cb10a08SNamhyung Kim up_read(&thread->namespaces_lock); 1686584140bSNamhyung Kim 1696584140bSNamhyung Kim return ns; 1706584140bSNamhyung Kim } 1716584140bSNamhyung Kim 172b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp, 17369d81f09SArnaldo Carvalho de Melo struct perf_record_namespaces *event) 174f3b3614aSHari Bathini { 1756584140bSNamhyung Kim struct namespaces *new, *curr = __thread__namespaces(thread); 176f3b3614aSHari Bathini 177f3b3614aSHari Bathini new = namespaces__new(event); 178f3b3614aSHari Bathini if (!new) 179f3b3614aSHari Bathini return -ENOMEM; 180f3b3614aSHari Bathini 181f3b3614aSHari Bathini list_add(&new->list, &thread->namespaces_list); 182f3b3614aSHari Bathini 183f3b3614aSHari Bathini if (timestamp && curr) { 184f3b3614aSHari Bathini /* 185f3b3614aSHari Bathini * setns syscall must have changed few or all the namespaces 186f3b3614aSHari Bathini * of this thread. Update end time for the namespaces 187f3b3614aSHari Bathini * previously used. 188f3b3614aSHari Bathini */ 189f3b3614aSHari Bathini curr = list_next_entry(new, list); 190f3b3614aSHari Bathini curr->end_time = timestamp; 191f3b3614aSHari Bathini } 192f3b3614aSHari Bathini 193f3b3614aSHari Bathini return 0; 194f3b3614aSHari Bathini } 195f3b3614aSHari Bathini 196b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp, 19769d81f09SArnaldo Carvalho de Melo struct perf_record_namespaces *event) 198b32ee9e5SKan Liang { 199b32ee9e5SKan Liang int ret; 200b32ee9e5SKan Liang 201b32ee9e5SKan Liang down_write(&thread->namespaces_lock); 202b32ee9e5SKan Liang ret = __thread__set_namespaces(thread, timestamp, event); 203b32ee9e5SKan Liang up_write(&thread->namespaces_lock); 204b32ee9e5SKan Liang return ret; 205b32ee9e5SKan Liang } 206b32ee9e5SKan Liang 2074dfced35SNamhyung Kim struct comm *thread__comm(const struct thread *thread) 2086baa0a5aSFrederic Weisbecker { 2091902efe7SFrederic Weisbecker if (list_empty(&thread->comm_list)) 2101902efe7SFrederic Weisbecker return NULL; 2114385d580SDavid S. Miller 2121902efe7SFrederic Weisbecker return list_first_entry(&thread->comm_list, struct comm, list); 2134385d580SDavid S. Miller } 2141902efe7SFrederic Weisbecker 21565de51f9SAdrian Hunter struct comm *thread__exec_comm(const struct thread *thread) 21665de51f9SAdrian Hunter { 2173de7ae0bSAdrian Hunter struct comm *comm, *last = NULL, *second_last = NULL; 21865de51f9SAdrian Hunter 21965de51f9SAdrian Hunter list_for_each_entry(comm, &thread->comm_list, list) { 22065de51f9SAdrian Hunter if (comm->exec) 22165de51f9SAdrian Hunter return comm; 2223de7ae0bSAdrian Hunter second_last = last; 22365de51f9SAdrian Hunter last = comm; 22465de51f9SAdrian Hunter } 22565de51f9SAdrian Hunter 2263de7ae0bSAdrian Hunter /* 2273de7ae0bSAdrian Hunter * 'last' with no start time might be the parent's comm of a synthesized 2283de7ae0bSAdrian Hunter * thread (created by processing a synthesized fork event). For a main 2293de7ae0bSAdrian Hunter * thread, that is very probably wrong. Prefer a later comm to avoid 2303de7ae0bSAdrian Hunter * that case. 2313de7ae0bSAdrian Hunter */ 2323de7ae0bSAdrian Hunter if (second_last && !last->start && thread->pid_ == thread->tid) 2333de7ae0bSAdrian Hunter return second_last; 2343de7ae0bSAdrian Hunter 23565de51f9SAdrian Hunter return last; 23665de51f9SAdrian Hunter } 23765de51f9SAdrian Hunter 238b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str, 239b32ee9e5SKan Liang u64 timestamp, bool exec) 2401902efe7SFrederic Weisbecker { 2411902efe7SFrederic Weisbecker struct comm *new, *curr = thread__comm(thread); 2421902efe7SFrederic Weisbecker 243a8480808SAdrian Hunter /* Override the default :tid entry */ 244a8480808SAdrian Hunter if (!thread->comm_set) { 24518ef15c6SArnaldo Carvalho de Melo int err = comm__override(curr, str, timestamp, exec); 2463178f58bSFrederic Weisbecker if (err) 2473178f58bSFrederic Weisbecker return err; 248a5285ad9SFrederic Weisbecker } else { 24965de51f9SAdrian Hunter new = comm__new(str, timestamp, exec); 2501902efe7SFrederic Weisbecker if (!new) 2511902efe7SFrederic Weisbecker return -ENOMEM; 2521902efe7SFrederic Weisbecker list_add(&new->list, &thread->comm_list); 253380b5143SNamhyung Kim 254380b5143SNamhyung Kim if (exec) 255fe87797dSArnaldo Carvalho de Melo unwind__flush_access(thread->maps); 256a5285ad9SFrederic Weisbecker } 257a5285ad9SFrederic Weisbecker 2581902efe7SFrederic Weisbecker thread->comm_set = true; 2591902efe7SFrederic Weisbecker 2601902efe7SFrederic Weisbecker return 0; 2616baa0a5aSFrederic Weisbecker } 2626baa0a5aSFrederic Weisbecker 263b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, 264b32ee9e5SKan Liang bool exec) 265b32ee9e5SKan Liang { 266b32ee9e5SKan Liang int ret; 267b32ee9e5SKan Liang 268b32ee9e5SKan Liang down_write(&thread->comm_lock); 269b32ee9e5SKan Liang ret = ____thread__set_comm(thread, str, timestamp, exec); 270b32ee9e5SKan Liang up_write(&thread->comm_lock); 271b32ee9e5SKan Liang return ret; 272b32ee9e5SKan Liang } 273b32ee9e5SKan Liang 2742f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread) 2752f3027acSArnaldo Carvalho de Melo { 2762f3027acSArnaldo Carvalho de Melo char path[64]; 2772f3027acSArnaldo Carvalho de Melo char *comm = NULL; 2782f3027acSArnaldo Carvalho de Melo size_t sz; 2792f3027acSArnaldo Carvalho de Melo int err = -1; 2802f3027acSArnaldo Carvalho de Melo 2812f3027acSArnaldo Carvalho de Melo if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", 2822f3027acSArnaldo Carvalho de Melo thread->pid_, thread->tid) >= (int)sizeof(path)) && 2832f3027acSArnaldo Carvalho de Melo procfs__read_str(path, &comm, &sz) == 0) { 2842f3027acSArnaldo Carvalho de Melo comm[sz - 1] = '\0'; 2852f3027acSArnaldo Carvalho de Melo err = thread__set_comm(thread, comm, 0); 2862f3027acSArnaldo Carvalho de Melo } 2872f3027acSArnaldo Carvalho de Melo 2882f3027acSArnaldo Carvalho de Melo return err; 2892f3027acSArnaldo Carvalho de Melo } 2902f3027acSArnaldo Carvalho de Melo 291b32ee9e5SKan Liang static const char *__thread__comm_str(const struct thread *thread) 292b9c5143aSFrederic Weisbecker { 2931902efe7SFrederic Weisbecker const struct comm *comm = thread__comm(thread); 2941902efe7SFrederic Weisbecker 2951902efe7SFrederic Weisbecker if (!comm) 2961902efe7SFrederic Weisbecker return NULL; 2971902efe7SFrederic Weisbecker 2981902efe7SFrederic Weisbecker return comm__str(comm); 299b9c5143aSFrederic Weisbecker } 300b9c5143aSFrederic Weisbecker 3017cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread) 302b32ee9e5SKan Liang { 303b32ee9e5SKan Liang const char *str; 304b32ee9e5SKan Liang 3057cb10a08SNamhyung Kim down_read(&thread->comm_lock); 306b32ee9e5SKan Liang str = __thread__comm_str(thread); 3077cb10a08SNamhyung Kim up_read(&thread->comm_lock); 308b32ee9e5SKan Liang 309b32ee9e5SKan Liang return str; 310b32ee9e5SKan Liang } 311b32ee9e5SKan Liang 3121902efe7SFrederic Weisbecker /* CHECKME: it should probably better return the max comm len from its comm list */ 313c824c433SArnaldo Carvalho de Melo int thread__comm_len(struct thread *thread) 314a4fb581bSFrederic Weisbecker { 315c824c433SArnaldo Carvalho de Melo if (!thread->comm_len) { 3161902efe7SFrederic Weisbecker const char *comm = thread__comm_str(thread); 3171902efe7SFrederic Weisbecker if (!comm) 318a4fb581bSFrederic Weisbecker return 0; 3191902efe7SFrederic Weisbecker thread->comm_len = strlen(comm); 320a4fb581bSFrederic Weisbecker } 321a4fb581bSFrederic Weisbecker 322c824c433SArnaldo Carvalho de Melo return thread->comm_len; 323a4fb581bSFrederic Weisbecker } 324a4fb581bSFrederic Weisbecker 3253f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp) 32695011c60SArnaldo Carvalho de Melo { 327b9c5143aSFrederic Weisbecker return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + 328fe87797dSArnaldo Carvalho de Melo maps__fprintf(thread->maps, fp); 3296baa0a5aSFrederic Weisbecker } 3306baa0a5aSFrederic Weisbecker 3318132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map) 3321b46cddfSArnaldo Carvalho de Melo { 3338132a2a8SHe Kuang int ret; 3348132a2a8SHe Kuang 335fe87797dSArnaldo Carvalho de Melo ret = unwind__prepare_access(thread->maps, map, NULL); 3368132a2a8SHe Kuang if (ret) 3378132a2a8SHe Kuang return ret; 3388132a2a8SHe Kuang 339fe87797dSArnaldo Carvalho de Melo maps__fixup_overlappings(thread->maps, map, stderr); 340fe87797dSArnaldo Carvalho de Melo maps__insert(thread->maps, map); 3418132a2a8SHe Kuang 3428132a2a8SHe Kuang return 0; 34395011c60SArnaldo Carvalho de Melo } 34495011c60SArnaldo Carvalho de Melo 3456c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread) 3466c502584SJiri Olsa { 3476c502584SJiri Olsa bool initialized = false; 3483183f8caSArnaldo Carvalho de Melo int err = 0; 349fe87797dSArnaldo Carvalho de Melo struct maps *maps = thread->maps; 3506c502584SJiri Olsa struct map *map; 3516c502584SJiri Olsa 3520a7c74eaSArnaldo Carvalho de Melo down_read(&maps->lock); 3536c502584SJiri Olsa 3548efc4f05SArnaldo Carvalho de Melo maps__for_each_entry(maps, map) { 355fe87797dSArnaldo Carvalho de Melo err = unwind__prepare_access(thread->maps, map, &initialized); 3566c502584SJiri Olsa if (err || initialized) 3576c502584SJiri Olsa break; 3586c502584SJiri Olsa } 3596c502584SJiri Olsa 3600a7c74eaSArnaldo Carvalho de Melo up_read(&maps->lock); 3616c502584SJiri Olsa 3626c502584SJiri Olsa return err; 3636c502584SJiri Olsa } 3646c502584SJiri Olsa 3656c502584SJiri Olsa static int thread__prepare_access(struct thread *thread) 3666c502584SJiri Olsa { 3676c502584SJiri Olsa int err = 0; 3686c502584SJiri Olsa 369382619c0SJiri Olsa if (dwarf_callchain_users) 3706c502584SJiri Olsa err = __thread__prepare_access(thread); 3716c502584SJiri Olsa 3726c502584SJiri Olsa return err; 3736c502584SJiri Olsa } 3746c502584SJiri Olsa 37579b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone) 376cddcef60SJiri Olsa { 377cddcef60SJiri Olsa /* This is new thread, we share map groups for process. */ 378cddcef60SJiri Olsa if (thread->pid_ == parent->pid_) 3796c502584SJiri Olsa return thread__prepare_access(thread); 380cddcef60SJiri Olsa 381fe87797dSArnaldo Carvalho de Melo if (thread->maps == parent->maps) { 3820d7e7accSAdrian Hunter pr_debug("broken map groups on thread %d/%d parent %d/%d\n", 3830d7e7accSAdrian Hunter thread->pid_, thread->tid, parent->pid_, parent->tid); 3840d7e7accSAdrian Hunter return 0; 3850d7e7accSAdrian Hunter } 386cddcef60SJiri Olsa /* But this one is new process, copy maps. */ 387fe87797dSArnaldo Carvalho de Melo return do_maps_clone ? maps__clone(thread, parent->maps) : 0; 388cddcef60SJiri Olsa } 389cddcef60SJiri Olsa 3904f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone) 3916baa0a5aSFrederic Weisbecker { 392faa5c5c3SArnaldo Carvalho de Melo if (parent->comm_set) { 3931902efe7SFrederic Weisbecker const char *comm = thread__comm_str(parent); 39418ef15c6SArnaldo Carvalho de Melo int err; 3951902efe7SFrederic Weisbecker if (!comm) 3966baa0a5aSFrederic Weisbecker return -ENOMEM; 3971902efe7SFrederic Weisbecker err = thread__set_comm(thread, comm, timestamp); 3988d00be81SDavid Ahern if (err) 3991902efe7SFrederic Weisbecker return err; 400faa5c5c3SArnaldo Carvalho de Melo } 4016baa0a5aSFrederic Weisbecker 402c824c433SArnaldo Carvalho de Melo thread->ppid = parent->tid; 40379b6bb73SArnaldo Carvalho de Melo return thread__clone_maps(thread, parent, do_maps_clone); 4046baa0a5aSFrederic Weisbecker } 40552a3cb8cSArnaldo Carvalho de Melo 40626bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, 40752a3cb8cSArnaldo Carvalho de Melo struct addr_location *al) 40852a3cb8cSArnaldo Carvalho de Melo { 40952a3cb8cSArnaldo Carvalho de Melo size_t i; 4103b556bceSEric Engestrom const u8 cpumodes[] = { 41152a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_USER, 41252a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_KERNEL, 41352a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_USER, 41452a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_KERNEL 41552a3cb8cSArnaldo Carvalho de Melo }; 41652a3cb8cSArnaldo Carvalho de Melo 41752a3cb8cSArnaldo Carvalho de Melo for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { 41826bd9331SArnaldo Carvalho de Melo thread__find_symbol(thread, cpumodes[i], addr, al); 41952a3cb8cSArnaldo Carvalho de Melo if (al->map) 42052a3cb8cSArnaldo Carvalho de Melo break; 42152a3cb8cSArnaldo Carvalho de Melo } 42252a3cb8cSArnaldo Carvalho de Melo } 423480ca357SAndi Kleen 424480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread) 425480ca357SAndi Kleen { 426480ca357SAndi Kleen if (thread->pid_ == thread->tid) 427480ca357SAndi Kleen return thread__get(thread); 428480ca357SAndi Kleen 429480ca357SAndi Kleen if (thread->pid_ == -1) 430480ca357SAndi Kleen return NULL; 431480ca357SAndi Kleen 432480ca357SAndi Kleen return machine__find_thread(machine, thread->pid_, thread->pid_); 433480ca357SAndi Kleen } 43415325938SAndi Kleen 43515325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine, 43615325938SAndi Kleen void *buf, u64 ip, int len, bool *is64bit) 43715325938SAndi Kleen { 43815325938SAndi Kleen u8 cpumode = PERF_RECORD_MISC_USER; 43915325938SAndi Kleen struct addr_location al; 44015325938SAndi Kleen long offset; 44115325938SAndi Kleen 44215325938SAndi Kleen if (machine__kernel_ip(machine, ip)) 44315325938SAndi Kleen cpumode = PERF_RECORD_MISC_KERNEL; 44415325938SAndi Kleen 44515325938SAndi Kleen if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso || 44615325938SAndi Kleen al.map->dso->data.status == DSO_DATA_STATUS_ERROR || 44715325938SAndi Kleen map__load(al.map) < 0) 44815325938SAndi Kleen return -1; 44915325938SAndi Kleen 45015325938SAndi Kleen offset = al.map->map_ip(al.map, ip); 45115325938SAndi Kleen if (is64bit) 45215325938SAndi Kleen *is64bit = al.map->dso->is_64_bit; 45315325938SAndi Kleen 45415325938SAndi Kleen return dso__data_read_offset(al.map->dso, machine, offset, buf, len); 45515325938SAndi Kleen } 456