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; 50771fd155SKan 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); 114*9c6c3f47SKan Liang thread__free_stitch_list(thread); 115c824c433SArnaldo Carvalho de Melo free(thread); 116591765fdSArnaldo Carvalho de Melo } 117591765fdSArnaldo Carvalho de Melo 118f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread) 119f3b623b8SArnaldo Carvalho de Melo { 120b91fc39fSArnaldo Carvalho de Melo if (thread) 121e34f5b11SElena Reshetova refcount_inc(&thread->refcnt); 122f3b623b8SArnaldo Carvalho de Melo return thread; 123f3b623b8SArnaldo Carvalho de Melo } 124f3b623b8SArnaldo Carvalho de Melo 125f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread) 126f3b623b8SArnaldo Carvalho de Melo { 127e34f5b11SElena Reshetova if (thread && refcount_dec_and_test(&thread->refcnt)) { 128abd82868SArnaldo Carvalho de Melo /* 1294c00af0eSArnaldo Carvalho de Melo * Remove it from the dead threads list, as last reference is 1304c00af0eSArnaldo Carvalho de Melo * gone, if it is in a dead threads list. 1314c00af0eSArnaldo Carvalho de Melo * 1324c00af0eSArnaldo Carvalho de Melo * We may not be there anymore if say, the machine where it was 1334c00af0eSArnaldo Carvalho de Melo * stored was already deleted, so we already removed it from 1344c00af0eSArnaldo Carvalho de Melo * the dead threads and some other piece of code still keeps a 1354c00af0eSArnaldo Carvalho de Melo * reference. 1364c00af0eSArnaldo Carvalho de Melo * 1374c00af0eSArnaldo Carvalho de Melo * This is what 'perf sched' does and finally drops it in 1384c00af0eSArnaldo Carvalho de Melo * perf_sched__lat(), where it calls perf_sched__read_events(), 1394c00af0eSArnaldo Carvalho de Melo * that processes the events by creating a session and deleting 1404c00af0eSArnaldo Carvalho de Melo * it, which ends up destroying the list heads for the dead 1414c00af0eSArnaldo Carvalho de Melo * threads, but before it does that it removes all threads from 1424c00af0eSArnaldo Carvalho de Melo * it using list_del_init(). 1434c00af0eSArnaldo Carvalho de Melo * 1444c00af0eSArnaldo Carvalho de Melo * So we need to check here if it is in a dead threads list and 1454c00af0eSArnaldo Carvalho de Melo * if so, remove it before finally deleting the thread, to avoid 1464c00af0eSArnaldo Carvalho de Melo * an use after free situation. 147abd82868SArnaldo Carvalho de Melo */ 1484c00af0eSArnaldo Carvalho de Melo if (!list_empty(&thread->node)) 149f3b623b8SArnaldo Carvalho de Melo list_del_init(&thread->node); 150f3b623b8SArnaldo Carvalho de Melo thread__delete(thread); 151f3b623b8SArnaldo Carvalho de Melo } 152f3b623b8SArnaldo Carvalho de Melo } 153f3b623b8SArnaldo Carvalho de Melo 1546584140bSNamhyung Kim static struct namespaces *__thread__namespaces(const struct thread *thread) 155f3b3614aSHari Bathini { 156f3b3614aSHari Bathini if (list_empty(&thread->namespaces_list)) 157f3b3614aSHari Bathini return NULL; 158f3b3614aSHari Bathini 159f3b3614aSHari Bathini return list_first_entry(&thread->namespaces_list, struct namespaces, list); 160f3b3614aSHari Bathini } 161f3b3614aSHari Bathini 1627cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread) 1636584140bSNamhyung Kim { 1646584140bSNamhyung Kim struct namespaces *ns; 1656584140bSNamhyung Kim 1667cb10a08SNamhyung Kim down_read(&thread->namespaces_lock); 1676584140bSNamhyung Kim ns = __thread__namespaces(thread); 1687cb10a08SNamhyung Kim up_read(&thread->namespaces_lock); 1696584140bSNamhyung Kim 1706584140bSNamhyung Kim return ns; 1716584140bSNamhyung Kim } 1726584140bSNamhyung Kim 173b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp, 17469d81f09SArnaldo Carvalho de Melo struct perf_record_namespaces *event) 175f3b3614aSHari Bathini { 1766584140bSNamhyung Kim struct namespaces *new, *curr = __thread__namespaces(thread); 177f3b3614aSHari Bathini 178f3b3614aSHari Bathini new = namespaces__new(event); 179f3b3614aSHari Bathini if (!new) 180f3b3614aSHari Bathini return -ENOMEM; 181f3b3614aSHari Bathini 182f3b3614aSHari Bathini list_add(&new->list, &thread->namespaces_list); 183f3b3614aSHari Bathini 184f3b3614aSHari Bathini if (timestamp && curr) { 185f3b3614aSHari Bathini /* 186f3b3614aSHari Bathini * setns syscall must have changed few or all the namespaces 187f3b3614aSHari Bathini * of this thread. Update end time for the namespaces 188f3b3614aSHari Bathini * previously used. 189f3b3614aSHari Bathini */ 190f3b3614aSHari Bathini curr = list_next_entry(new, list); 191f3b3614aSHari Bathini curr->end_time = timestamp; 192f3b3614aSHari Bathini } 193f3b3614aSHari Bathini 194f3b3614aSHari Bathini return 0; 195f3b3614aSHari Bathini } 196f3b3614aSHari Bathini 197b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp, 19869d81f09SArnaldo Carvalho de Melo struct perf_record_namespaces *event) 199b32ee9e5SKan Liang { 200b32ee9e5SKan Liang int ret; 201b32ee9e5SKan Liang 202b32ee9e5SKan Liang down_write(&thread->namespaces_lock); 203b32ee9e5SKan Liang ret = __thread__set_namespaces(thread, timestamp, event); 204b32ee9e5SKan Liang up_write(&thread->namespaces_lock); 205b32ee9e5SKan Liang return ret; 206b32ee9e5SKan Liang } 207b32ee9e5SKan Liang 2084dfced35SNamhyung Kim struct comm *thread__comm(const struct thread *thread) 2096baa0a5aSFrederic Weisbecker { 2101902efe7SFrederic Weisbecker if (list_empty(&thread->comm_list)) 2111902efe7SFrederic Weisbecker return NULL; 2124385d580SDavid S. Miller 2131902efe7SFrederic Weisbecker return list_first_entry(&thread->comm_list, struct comm, list); 2144385d580SDavid S. Miller } 2151902efe7SFrederic Weisbecker 21665de51f9SAdrian Hunter struct comm *thread__exec_comm(const struct thread *thread) 21765de51f9SAdrian Hunter { 2183de7ae0bSAdrian Hunter struct comm *comm, *last = NULL, *second_last = NULL; 21965de51f9SAdrian Hunter 22065de51f9SAdrian Hunter list_for_each_entry(comm, &thread->comm_list, list) { 22165de51f9SAdrian Hunter if (comm->exec) 22265de51f9SAdrian Hunter return comm; 2233de7ae0bSAdrian Hunter second_last = last; 22465de51f9SAdrian Hunter last = comm; 22565de51f9SAdrian Hunter } 22665de51f9SAdrian Hunter 2273de7ae0bSAdrian Hunter /* 2283de7ae0bSAdrian Hunter * 'last' with no start time might be the parent's comm of a synthesized 2293de7ae0bSAdrian Hunter * thread (created by processing a synthesized fork event). For a main 2303de7ae0bSAdrian Hunter * thread, that is very probably wrong. Prefer a later comm to avoid 2313de7ae0bSAdrian Hunter * that case. 2323de7ae0bSAdrian Hunter */ 2333de7ae0bSAdrian Hunter if (second_last && !last->start && thread->pid_ == thread->tid) 2343de7ae0bSAdrian Hunter return second_last; 2353de7ae0bSAdrian Hunter 23665de51f9SAdrian Hunter return last; 23765de51f9SAdrian Hunter } 23865de51f9SAdrian Hunter 239b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str, 240b32ee9e5SKan Liang u64 timestamp, bool exec) 2411902efe7SFrederic Weisbecker { 2421902efe7SFrederic Weisbecker struct comm *new, *curr = thread__comm(thread); 2431902efe7SFrederic Weisbecker 244a8480808SAdrian Hunter /* Override the default :tid entry */ 245a8480808SAdrian Hunter if (!thread->comm_set) { 24618ef15c6SArnaldo Carvalho de Melo int err = comm__override(curr, str, timestamp, exec); 2473178f58bSFrederic Weisbecker if (err) 2483178f58bSFrederic Weisbecker return err; 249a5285ad9SFrederic Weisbecker } else { 25065de51f9SAdrian Hunter new = comm__new(str, timestamp, exec); 2511902efe7SFrederic Weisbecker if (!new) 2521902efe7SFrederic Weisbecker return -ENOMEM; 2531902efe7SFrederic Weisbecker list_add(&new->list, &thread->comm_list); 254380b5143SNamhyung Kim 255380b5143SNamhyung Kim if (exec) 256fe87797dSArnaldo Carvalho de Melo unwind__flush_access(thread->maps); 257a5285ad9SFrederic Weisbecker } 258a5285ad9SFrederic Weisbecker 2591902efe7SFrederic Weisbecker thread->comm_set = true; 2601902efe7SFrederic Weisbecker 2611902efe7SFrederic Weisbecker return 0; 2626baa0a5aSFrederic Weisbecker } 2636baa0a5aSFrederic Weisbecker 264b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, 265b32ee9e5SKan Liang bool exec) 266b32ee9e5SKan Liang { 267b32ee9e5SKan Liang int ret; 268b32ee9e5SKan Liang 269b32ee9e5SKan Liang down_write(&thread->comm_lock); 270b32ee9e5SKan Liang ret = ____thread__set_comm(thread, str, timestamp, exec); 271b32ee9e5SKan Liang up_write(&thread->comm_lock); 272b32ee9e5SKan Liang return ret; 273b32ee9e5SKan Liang } 274b32ee9e5SKan Liang 2752f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread) 2762f3027acSArnaldo Carvalho de Melo { 2772f3027acSArnaldo Carvalho de Melo char path[64]; 2782f3027acSArnaldo Carvalho de Melo char *comm = NULL; 2792f3027acSArnaldo Carvalho de Melo size_t sz; 2802f3027acSArnaldo Carvalho de Melo int err = -1; 2812f3027acSArnaldo Carvalho de Melo 2822f3027acSArnaldo Carvalho de Melo if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", 2832f3027acSArnaldo Carvalho de Melo thread->pid_, thread->tid) >= (int)sizeof(path)) && 2842f3027acSArnaldo Carvalho de Melo procfs__read_str(path, &comm, &sz) == 0) { 2852f3027acSArnaldo Carvalho de Melo comm[sz - 1] = '\0'; 2862f3027acSArnaldo Carvalho de Melo err = thread__set_comm(thread, comm, 0); 2872f3027acSArnaldo Carvalho de Melo } 2882f3027acSArnaldo Carvalho de Melo 2892f3027acSArnaldo Carvalho de Melo return err; 2902f3027acSArnaldo Carvalho de Melo } 2912f3027acSArnaldo Carvalho de Melo 292b32ee9e5SKan Liang static const char *__thread__comm_str(const struct thread *thread) 293b9c5143aSFrederic Weisbecker { 2941902efe7SFrederic Weisbecker const struct comm *comm = thread__comm(thread); 2951902efe7SFrederic Weisbecker 2961902efe7SFrederic Weisbecker if (!comm) 2971902efe7SFrederic Weisbecker return NULL; 2981902efe7SFrederic Weisbecker 2991902efe7SFrederic Weisbecker return comm__str(comm); 300b9c5143aSFrederic Weisbecker } 301b9c5143aSFrederic Weisbecker 3027cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread) 303b32ee9e5SKan Liang { 304b32ee9e5SKan Liang const char *str; 305b32ee9e5SKan Liang 3067cb10a08SNamhyung Kim down_read(&thread->comm_lock); 307b32ee9e5SKan Liang str = __thread__comm_str(thread); 3087cb10a08SNamhyung Kim up_read(&thread->comm_lock); 309b32ee9e5SKan Liang 310b32ee9e5SKan Liang return str; 311b32ee9e5SKan Liang } 312b32ee9e5SKan Liang 3131902efe7SFrederic Weisbecker /* CHECKME: it should probably better return the max comm len from its comm list */ 314c824c433SArnaldo Carvalho de Melo int thread__comm_len(struct thread *thread) 315a4fb581bSFrederic Weisbecker { 316c824c433SArnaldo Carvalho de Melo if (!thread->comm_len) { 3171902efe7SFrederic Weisbecker const char *comm = thread__comm_str(thread); 3181902efe7SFrederic Weisbecker if (!comm) 319a4fb581bSFrederic Weisbecker return 0; 3201902efe7SFrederic Weisbecker thread->comm_len = strlen(comm); 321a4fb581bSFrederic Weisbecker } 322a4fb581bSFrederic Weisbecker 323c824c433SArnaldo Carvalho de Melo return thread->comm_len; 324a4fb581bSFrederic Weisbecker } 325a4fb581bSFrederic Weisbecker 3263f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp) 32795011c60SArnaldo Carvalho de Melo { 328b9c5143aSFrederic Weisbecker return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + 329fe87797dSArnaldo Carvalho de Melo maps__fprintf(thread->maps, fp); 3306baa0a5aSFrederic Weisbecker } 3316baa0a5aSFrederic Weisbecker 3328132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map) 3331b46cddfSArnaldo Carvalho de Melo { 3348132a2a8SHe Kuang int ret; 3358132a2a8SHe Kuang 336fe87797dSArnaldo Carvalho de Melo ret = unwind__prepare_access(thread->maps, map, NULL); 3378132a2a8SHe Kuang if (ret) 3388132a2a8SHe Kuang return ret; 3398132a2a8SHe Kuang 340fe87797dSArnaldo Carvalho de Melo maps__fixup_overlappings(thread->maps, map, stderr); 341fe87797dSArnaldo Carvalho de Melo maps__insert(thread->maps, map); 3428132a2a8SHe Kuang 3438132a2a8SHe Kuang return 0; 34495011c60SArnaldo Carvalho de Melo } 34595011c60SArnaldo Carvalho de Melo 3466c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread) 3476c502584SJiri Olsa { 3486c502584SJiri Olsa bool initialized = false; 3493183f8caSArnaldo Carvalho de Melo int err = 0; 350fe87797dSArnaldo Carvalho de Melo struct maps *maps = thread->maps; 3516c502584SJiri Olsa struct map *map; 3526c502584SJiri Olsa 3530a7c74eaSArnaldo Carvalho de Melo down_read(&maps->lock); 3546c502584SJiri Olsa 3558efc4f05SArnaldo Carvalho de Melo maps__for_each_entry(maps, map) { 356fe87797dSArnaldo Carvalho de Melo err = unwind__prepare_access(thread->maps, map, &initialized); 3576c502584SJiri Olsa if (err || initialized) 3586c502584SJiri Olsa break; 3596c502584SJiri Olsa } 3606c502584SJiri Olsa 3610a7c74eaSArnaldo Carvalho de Melo up_read(&maps->lock); 3626c502584SJiri Olsa 3636c502584SJiri Olsa return err; 3646c502584SJiri Olsa } 3656c502584SJiri Olsa 3666c502584SJiri Olsa static int thread__prepare_access(struct thread *thread) 3676c502584SJiri Olsa { 3686c502584SJiri Olsa int err = 0; 3696c502584SJiri Olsa 370382619c0SJiri Olsa if (dwarf_callchain_users) 3716c502584SJiri Olsa err = __thread__prepare_access(thread); 3726c502584SJiri Olsa 3736c502584SJiri Olsa return err; 3746c502584SJiri Olsa } 3756c502584SJiri Olsa 37679b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone) 377cddcef60SJiri Olsa { 378cddcef60SJiri Olsa /* This is new thread, we share map groups for process. */ 379cddcef60SJiri Olsa if (thread->pid_ == parent->pid_) 3806c502584SJiri Olsa return thread__prepare_access(thread); 381cddcef60SJiri Olsa 382fe87797dSArnaldo Carvalho de Melo if (thread->maps == parent->maps) { 3830d7e7accSAdrian Hunter pr_debug("broken map groups on thread %d/%d parent %d/%d\n", 3840d7e7accSAdrian Hunter thread->pid_, thread->tid, parent->pid_, parent->tid); 3850d7e7accSAdrian Hunter return 0; 3860d7e7accSAdrian Hunter } 387cddcef60SJiri Olsa /* But this one is new process, copy maps. */ 388fe87797dSArnaldo Carvalho de Melo return do_maps_clone ? maps__clone(thread, parent->maps) : 0; 389cddcef60SJiri Olsa } 390cddcef60SJiri Olsa 3914f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone) 3926baa0a5aSFrederic Weisbecker { 393faa5c5c3SArnaldo Carvalho de Melo if (parent->comm_set) { 3941902efe7SFrederic Weisbecker const char *comm = thread__comm_str(parent); 39518ef15c6SArnaldo Carvalho de Melo int err; 3961902efe7SFrederic Weisbecker if (!comm) 3976baa0a5aSFrederic Weisbecker return -ENOMEM; 3981902efe7SFrederic Weisbecker err = thread__set_comm(thread, comm, timestamp); 3998d00be81SDavid Ahern if (err) 4001902efe7SFrederic Weisbecker return err; 401faa5c5c3SArnaldo Carvalho de Melo } 4026baa0a5aSFrederic Weisbecker 403c824c433SArnaldo Carvalho de Melo thread->ppid = parent->tid; 40479b6bb73SArnaldo Carvalho de Melo return thread__clone_maps(thread, parent, do_maps_clone); 4056baa0a5aSFrederic Weisbecker } 40652a3cb8cSArnaldo Carvalho de Melo 40726bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, 40852a3cb8cSArnaldo Carvalho de Melo struct addr_location *al) 40952a3cb8cSArnaldo Carvalho de Melo { 41052a3cb8cSArnaldo Carvalho de Melo size_t i; 4113b556bceSEric Engestrom const u8 cpumodes[] = { 41252a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_USER, 41352a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_KERNEL, 41452a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_USER, 41552a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_KERNEL 41652a3cb8cSArnaldo Carvalho de Melo }; 41752a3cb8cSArnaldo Carvalho de Melo 41852a3cb8cSArnaldo Carvalho de Melo for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { 41926bd9331SArnaldo Carvalho de Melo thread__find_symbol(thread, cpumodes[i], addr, al); 42052a3cb8cSArnaldo Carvalho de Melo if (al->map) 42152a3cb8cSArnaldo Carvalho de Melo break; 42252a3cb8cSArnaldo Carvalho de Melo } 42352a3cb8cSArnaldo Carvalho de Melo } 424480ca357SAndi Kleen 425480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread) 426480ca357SAndi Kleen { 427480ca357SAndi Kleen if (thread->pid_ == thread->tid) 428480ca357SAndi Kleen return thread__get(thread); 429480ca357SAndi Kleen 430480ca357SAndi Kleen if (thread->pid_ == -1) 431480ca357SAndi Kleen return NULL; 432480ca357SAndi Kleen 433480ca357SAndi Kleen return machine__find_thread(machine, thread->pid_, thread->pid_); 434480ca357SAndi Kleen } 43515325938SAndi Kleen 43615325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine, 43715325938SAndi Kleen void *buf, u64 ip, int len, bool *is64bit) 43815325938SAndi Kleen { 43915325938SAndi Kleen u8 cpumode = PERF_RECORD_MISC_USER; 44015325938SAndi Kleen struct addr_location al; 44115325938SAndi Kleen long offset; 44215325938SAndi Kleen 44315325938SAndi Kleen if (machine__kernel_ip(machine, ip)) 44415325938SAndi Kleen cpumode = PERF_RECORD_MISC_KERNEL; 44515325938SAndi Kleen 44615325938SAndi Kleen if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso || 44715325938SAndi Kleen al.map->dso->data.status == DSO_DATA_STATUS_ERROR || 44815325938SAndi Kleen map__load(al.map) < 0) 44915325938SAndi Kleen return -1; 45015325938SAndi Kleen 45115325938SAndi Kleen offset = al.map->map_ip(al.map, ip); 45215325938SAndi Kleen if (is64bit) 45315325938SAndi Kleen *is64bit = al.map->dso->is_64_bit; 45415325938SAndi Kleen 45515325938SAndi Kleen return dso__data_read_offset(al.map->dso, machine, offset, buf, len); 45615325938SAndi Kleen } 457