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 { 24ee84a303SIan Rogers pid_t pid = thread__pid(thread); 25cddcef60SJiri Olsa 26ee84a303SIan Rogers if (pid == thread__tid(thread) || pid == -1) { 27ee84a303SIan Rogers thread__set_maps(thread, maps__new(machine)); 28cddcef60SJiri Olsa } else { 29d436f90aSIan Rogers struct thread *leader = machine__findnew_thread(machine, pid, pid); 30ee84a303SIan Rogers 31abd82868SArnaldo Carvalho de Melo if (leader) { 32ee84a303SIan Rogers thread__set_maps(thread, maps__get(thread__maps(leader))); 33abd82868SArnaldo Carvalho de Melo thread__put(leader); 34abd82868SArnaldo Carvalho de Melo } 35cddcef60SJiri Olsa } 36cddcef60SJiri Olsa 37ee84a303SIan Rogers return thread__maps(thread) ? 0 : -1; 38cddcef60SJiri Olsa } 39cddcef60SJiri Olsa 4099d725fcSAdrian Hunter struct thread *thread__new(pid_t pid, pid_t tid) 416baa0a5aSFrederic Weisbecker { 42f6005cafSIan Rogers RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread)); 43f6005cafSIan Rogers struct thread *thread; 446baa0a5aSFrederic Weisbecker 45f6005cafSIan Rogers if (ADD_RC_CHK(thread, _thread) != NULL) { 463536c257SIan Rogers struct comm *comm; 473536c257SIan Rogers char comm_str[32]; 483536c257SIan Rogers 49ee84a303SIan Rogers thread__set_pid(thread, pid); 50ee84a303SIan Rogers thread__set_tid(thread, tid); 51ee84a303SIan Rogers thread__set_ppid(thread, -1); 52ee84a303SIan Rogers thread__set_cpu(thread, -1); 53ee84a303SIan Rogers thread__set_guest_cpu(thread, -1); 54ee84a303SIan Rogers thread__set_lbr_stitch_enable(thread, false); 55ee84a303SIan Rogers INIT_LIST_HEAD(thread__namespaces_list(thread)); 56ee84a303SIan Rogers INIT_LIST_HEAD(thread__comm_list(thread)); 57ee84a303SIan Rogers init_rwsem(thread__namespaces_lock(thread)); 58ee84a303SIan Rogers init_rwsem(thread__comm_lock(thread)); 591902efe7SFrederic Weisbecker 603536c257SIan Rogers snprintf(comm_str, sizeof(comm_str), ":%d", tid); 6165de51f9SAdrian Hunter comm = comm__new(comm_str, 0, false); 621902efe7SFrederic Weisbecker if (!comm) 631902efe7SFrederic Weisbecker goto err_thread; 641902efe7SFrederic Weisbecker 65ee84a303SIan Rogers list_add(&comm->list, thread__comm_list(thread)); 66ee84a303SIan Rogers refcount_set(thread__refcnt(thread), 1); 67843ff37bSKrister Johansen /* Thread holds first ref to nsdata. */ 68f6005cafSIan Rogers RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new(pid); 69ee84a303SIan Rogers srccode_state_init(thread__srccode_state(thread)); 706baa0a5aSFrederic Weisbecker } 716baa0a5aSFrederic Weisbecker 72c824c433SArnaldo Carvalho de Melo return thread; 731902efe7SFrederic Weisbecker 741902efe7SFrederic Weisbecker err_thread: 753536c257SIan Rogers thread__delete(thread); 761902efe7SFrederic Weisbecker return NULL; 776baa0a5aSFrederic Weisbecker } 786baa0a5aSFrederic Weisbecker 7904cb4fc4SArnaldo Carvalho de Melo static void (*thread__priv_destructor)(void *priv); 8004cb4fc4SArnaldo Carvalho de Melo 8104cb4fc4SArnaldo Carvalho de Melo void thread__set_priv_destructor(void (*destructor)(void *priv)) 8204cb4fc4SArnaldo Carvalho de Melo { 8304cb4fc4SArnaldo Carvalho de Melo assert(thread__priv_destructor == NULL); 8404cb4fc4SArnaldo Carvalho de Melo 8504cb4fc4SArnaldo Carvalho de Melo thread__priv_destructor = destructor; 8604cb4fc4SArnaldo Carvalho de Melo } 8704cb4fc4SArnaldo Carvalho de Melo 88c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread) 89591765fdSArnaldo Carvalho de Melo { 90f3b3614aSHari Bathini struct namespaces *namespaces, *tmp_namespaces; 91f3b3614aSHari Bathini struct comm *comm, *tmp_comm; 921902efe7SFrederic Weisbecker 9300447ccdSAdrian Hunter thread_stack__free(thread); 9400447ccdSAdrian Hunter 95ee84a303SIan Rogers if (thread__maps(thread)) { 96ee84a303SIan Rogers maps__put(thread__maps(thread)); 97ee84a303SIan Rogers thread__set_maps(thread, NULL); 989608b84eSAdrian Hunter } 99ee84a303SIan Rogers down_write(thread__namespaces_lock(thread)); 100f3b3614aSHari Bathini list_for_each_entry_safe(namespaces, tmp_namespaces, 101ee84a303SIan Rogers thread__namespaces_list(thread), list) { 102e56fbc9dSArnaldo Carvalho de Melo list_del_init(&namespaces->list); 103f3b3614aSHari Bathini namespaces__free(namespaces); 104f3b3614aSHari Bathini } 105ee84a303SIan Rogers up_write(thread__namespaces_lock(thread)); 106b32ee9e5SKan Liang 107ee84a303SIan Rogers down_write(thread__comm_lock(thread)); 108ee84a303SIan Rogers list_for_each_entry_safe(comm, tmp_comm, thread__comm_list(thread), list) { 109e56fbc9dSArnaldo Carvalho de Melo list_del_init(&comm->list); 1101902efe7SFrederic Weisbecker comm__free(comm); 1111902efe7SFrederic Weisbecker } 112ee84a303SIan Rogers up_write(thread__comm_lock(thread)); 113b32ee9e5SKan Liang 114f6005cafSIan Rogers nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo); 115ee84a303SIan Rogers srccode_state_free(thread__srccode_state(thread)); 1161902efe7SFrederic Weisbecker 117ee84a303SIan Rogers exit_rwsem(thread__namespaces_lock(thread)); 118ee84a303SIan Rogers exit_rwsem(thread__comm_lock(thread)); 1199c6c3f47SKan Liang thread__free_stitch_list(thread); 12004cb4fc4SArnaldo Carvalho de Melo 12104cb4fc4SArnaldo Carvalho de Melo if (thread__priv_destructor) 12204cb4fc4SArnaldo Carvalho de Melo thread__priv_destructor(thread__priv(thread)); 12304cb4fc4SArnaldo Carvalho de Melo 124f6005cafSIan Rogers RC_CHK_FREE(thread); 125591765fdSArnaldo Carvalho de Melo } 126591765fdSArnaldo Carvalho de Melo 127f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread) 128f3b623b8SArnaldo Carvalho de Melo { 129f6005cafSIan Rogers struct thread *result; 130f6005cafSIan Rogers 131f6005cafSIan Rogers if (RC_CHK_GET(result, thread)) 132ee84a303SIan Rogers refcount_inc(thread__refcnt(thread)); 133f6005cafSIan Rogers 134f6005cafSIan Rogers return result; 135f3b623b8SArnaldo Carvalho de Melo } 136f3b623b8SArnaldo Carvalho de Melo 137f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread) 138f3b623b8SArnaldo Carvalho de Melo { 139ee84a303SIan Rogers if (thread && refcount_dec_and_test(thread__refcnt(thread))) 140f3b623b8SArnaldo Carvalho de Melo thread__delete(thread); 141f6005cafSIan Rogers else 142f6005cafSIan Rogers RC_CHK_PUT(thread); 143f3b623b8SArnaldo Carvalho de Melo } 144f3b623b8SArnaldo Carvalho de Melo 145ee84a303SIan Rogers static struct namespaces *__thread__namespaces(struct thread *thread) 146f3b3614aSHari Bathini { 147ee84a303SIan Rogers if (list_empty(thread__namespaces_list(thread))) 148f3b3614aSHari Bathini return NULL; 149f3b3614aSHari Bathini 150ee84a303SIan Rogers return list_first_entry(thread__namespaces_list(thread), struct namespaces, list); 151f3b3614aSHari Bathini } 152f3b3614aSHari Bathini 1537cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread) 1546584140bSNamhyung Kim { 1556584140bSNamhyung Kim struct namespaces *ns; 1566584140bSNamhyung Kim 157ee84a303SIan Rogers down_read(thread__namespaces_lock(thread)); 1586584140bSNamhyung Kim ns = __thread__namespaces(thread); 159ee84a303SIan Rogers up_read(thread__namespaces_lock(thread)); 1606584140bSNamhyung Kim 1616584140bSNamhyung Kim return ns; 1626584140bSNamhyung Kim } 1636584140bSNamhyung Kim 164b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp, 16569d81f09SArnaldo Carvalho de Melo struct perf_record_namespaces *event) 166f3b3614aSHari Bathini { 1676584140bSNamhyung Kim struct namespaces *new, *curr = __thread__namespaces(thread); 168f3b3614aSHari Bathini 169f3b3614aSHari Bathini new = namespaces__new(event); 170f3b3614aSHari Bathini if (!new) 171f3b3614aSHari Bathini return -ENOMEM; 172f3b3614aSHari Bathini 173ee84a303SIan Rogers list_add(&new->list, thread__namespaces_list(thread)); 174f3b3614aSHari Bathini 175f3b3614aSHari Bathini if (timestamp && curr) { 176f3b3614aSHari Bathini /* 177f3b3614aSHari Bathini * setns syscall must have changed few or all the namespaces 178f3b3614aSHari Bathini * of this thread. Update end time for the namespaces 179f3b3614aSHari Bathini * previously used. 180f3b3614aSHari Bathini */ 181f3b3614aSHari Bathini curr = list_next_entry(new, list); 182f3b3614aSHari Bathini curr->end_time = timestamp; 183f3b3614aSHari Bathini } 184f3b3614aSHari Bathini 185f3b3614aSHari Bathini return 0; 186f3b3614aSHari Bathini } 187f3b3614aSHari Bathini 188b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp, 18969d81f09SArnaldo Carvalho de Melo struct perf_record_namespaces *event) 190b32ee9e5SKan Liang { 191b32ee9e5SKan Liang int ret; 192b32ee9e5SKan Liang 193ee84a303SIan Rogers down_write(thread__namespaces_lock(thread)); 194b32ee9e5SKan Liang ret = __thread__set_namespaces(thread, timestamp, event); 195ee84a303SIan Rogers up_write(thread__namespaces_lock(thread)); 196b32ee9e5SKan Liang return ret; 197b32ee9e5SKan Liang } 198b32ee9e5SKan Liang 199ee84a303SIan Rogers struct comm *thread__comm(struct thread *thread) 2006baa0a5aSFrederic Weisbecker { 201ee84a303SIan Rogers if (list_empty(thread__comm_list(thread))) 2021902efe7SFrederic Weisbecker return NULL; 2034385d580SDavid S. Miller 204ee84a303SIan Rogers return list_first_entry(thread__comm_list(thread), struct comm, list); 2054385d580SDavid S. Miller } 2061902efe7SFrederic Weisbecker 207ee84a303SIan Rogers struct comm *thread__exec_comm(struct thread *thread) 20865de51f9SAdrian Hunter { 2093de7ae0bSAdrian Hunter struct comm *comm, *last = NULL, *second_last = NULL; 21065de51f9SAdrian Hunter 211ee84a303SIan Rogers list_for_each_entry(comm, thread__comm_list(thread), list) { 21265de51f9SAdrian Hunter if (comm->exec) 21365de51f9SAdrian Hunter return comm; 2143de7ae0bSAdrian Hunter second_last = last; 21565de51f9SAdrian Hunter last = comm; 21665de51f9SAdrian Hunter } 21765de51f9SAdrian Hunter 2183de7ae0bSAdrian Hunter /* 2193de7ae0bSAdrian Hunter * 'last' with no start time might be the parent's comm of a synthesized 2203de7ae0bSAdrian Hunter * thread (created by processing a synthesized fork event). For a main 2213de7ae0bSAdrian Hunter * thread, that is very probably wrong. Prefer a later comm to avoid 2223de7ae0bSAdrian Hunter * that case. 2233de7ae0bSAdrian Hunter */ 224ee84a303SIan Rogers if (second_last && !last->start && thread__pid(thread) == thread__tid(thread)) 2253de7ae0bSAdrian Hunter return second_last; 2263de7ae0bSAdrian Hunter 22765de51f9SAdrian Hunter return last; 22865de51f9SAdrian Hunter } 22965de51f9SAdrian Hunter 230b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str, 231b32ee9e5SKan Liang u64 timestamp, bool exec) 2321902efe7SFrederic Weisbecker { 2331902efe7SFrederic Weisbecker struct comm *new, *curr = thread__comm(thread); 2341902efe7SFrederic Weisbecker 235a8480808SAdrian Hunter /* Override the default :tid entry */ 236ee84a303SIan Rogers if (!thread__comm_set(thread)) { 23718ef15c6SArnaldo Carvalho de Melo int err = comm__override(curr, str, timestamp, exec); 2383178f58bSFrederic Weisbecker if (err) 2393178f58bSFrederic Weisbecker return err; 240a5285ad9SFrederic Weisbecker } else { 24165de51f9SAdrian Hunter new = comm__new(str, timestamp, exec); 2421902efe7SFrederic Weisbecker if (!new) 2431902efe7SFrederic Weisbecker return -ENOMEM; 244ee84a303SIan Rogers list_add(&new->list, thread__comm_list(thread)); 245380b5143SNamhyung Kim 246380b5143SNamhyung Kim if (exec) 247ee84a303SIan Rogers unwind__flush_access(thread__maps(thread)); 248a5285ad9SFrederic Weisbecker } 249a5285ad9SFrederic Weisbecker 250ee84a303SIan Rogers thread__set_comm_set(thread, true); 2511902efe7SFrederic Weisbecker 2521902efe7SFrederic Weisbecker return 0; 2536baa0a5aSFrederic Weisbecker } 2546baa0a5aSFrederic Weisbecker 255b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, 256b32ee9e5SKan Liang bool exec) 257b32ee9e5SKan Liang { 258b32ee9e5SKan Liang int ret; 259b32ee9e5SKan Liang 260ee84a303SIan Rogers down_write(thread__comm_lock(thread)); 261b32ee9e5SKan Liang ret = ____thread__set_comm(thread, str, timestamp, exec); 262ee84a303SIan Rogers up_write(thread__comm_lock(thread)); 263b32ee9e5SKan Liang return ret; 264b32ee9e5SKan Liang } 265b32ee9e5SKan Liang 2662f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread) 2672f3027acSArnaldo Carvalho de Melo { 2682f3027acSArnaldo Carvalho de Melo char path[64]; 2692f3027acSArnaldo Carvalho de Melo char *comm = NULL; 2702f3027acSArnaldo Carvalho de Melo size_t sz; 2712f3027acSArnaldo Carvalho de Melo int err = -1; 2722f3027acSArnaldo Carvalho de Melo 2732f3027acSArnaldo Carvalho de Melo if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", 274ee84a303SIan Rogers thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) && 2752f3027acSArnaldo Carvalho de Melo procfs__read_str(path, &comm, &sz) == 0) { 2762f3027acSArnaldo Carvalho de Melo comm[sz - 1] = '\0'; 2772f3027acSArnaldo Carvalho de Melo err = thread__set_comm(thread, comm, 0); 2782f3027acSArnaldo Carvalho de Melo } 2792f3027acSArnaldo Carvalho de Melo 2802f3027acSArnaldo Carvalho de Melo return err; 2812f3027acSArnaldo Carvalho de Melo } 2822f3027acSArnaldo Carvalho de Melo 283ee84a303SIan Rogers static const char *__thread__comm_str(struct thread *thread) 284b9c5143aSFrederic Weisbecker { 2851902efe7SFrederic Weisbecker const struct comm *comm = thread__comm(thread); 2861902efe7SFrederic Weisbecker 2871902efe7SFrederic Weisbecker if (!comm) 2881902efe7SFrederic Weisbecker return NULL; 2891902efe7SFrederic Weisbecker 2901902efe7SFrederic Weisbecker return comm__str(comm); 291b9c5143aSFrederic Weisbecker } 292b9c5143aSFrederic Weisbecker 2937cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread) 294b32ee9e5SKan Liang { 295b32ee9e5SKan Liang const char *str; 296b32ee9e5SKan Liang 297ee84a303SIan Rogers down_read(thread__comm_lock(thread)); 298b32ee9e5SKan Liang str = __thread__comm_str(thread); 299ee84a303SIan Rogers up_read(thread__comm_lock(thread)); 300b32ee9e5SKan Liang 301b32ee9e5SKan Liang return str; 302b32ee9e5SKan Liang } 303b32ee9e5SKan Liang 3046e57f69fSliuwenyu static int __thread__comm_len(struct thread *thread, const char *comm) 305a4fb581bSFrederic Weisbecker { 3061902efe7SFrederic Weisbecker if (!comm) 307a4fb581bSFrederic Weisbecker return 0; 308ee84a303SIan Rogers thread__set_comm_len(thread, strlen(comm)); 309a4fb581bSFrederic Weisbecker 310ee84a303SIan Rogers return thread__var_comm_len(thread); 311a4fb581bSFrederic Weisbecker } 312a4fb581bSFrederic Weisbecker 3136e57f69fSliuwenyu /* CHECKME: it should probably better return the max comm len from its comm list */ 3146e57f69fSliuwenyu int thread__comm_len(struct thread *thread) 3156e57f69fSliuwenyu { 316ee84a303SIan Rogers int comm_len = thread__var_comm_len(thread); 3176e57f69fSliuwenyu 3186e57f69fSliuwenyu if (!comm_len) { 3196e57f69fSliuwenyu const char *comm; 3206e57f69fSliuwenyu 321ee84a303SIan Rogers down_read(thread__comm_lock(thread)); 3226e57f69fSliuwenyu comm = __thread__comm_str(thread); 3236e57f69fSliuwenyu comm_len = __thread__comm_len(thread, comm); 324ee84a303SIan Rogers up_read(thread__comm_lock(thread)); 3256e57f69fSliuwenyu } 3266e57f69fSliuwenyu 3276e57f69fSliuwenyu return comm_len; 3286e57f69fSliuwenyu } 3296e57f69fSliuwenyu 3303f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp) 33195011c60SArnaldo Carvalho de Melo { 332ee84a303SIan Rogers return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_str(thread)) + 333ee84a303SIan Rogers maps__fprintf(thread__maps(thread), fp); 3346baa0a5aSFrederic Weisbecker } 3356baa0a5aSFrederic Weisbecker 3368132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map) 3371b46cddfSArnaldo Carvalho de Melo { 3388132a2a8SHe Kuang int ret; 3398132a2a8SHe Kuang 340ee84a303SIan Rogers ret = unwind__prepare_access(thread__maps(thread), map, NULL); 3418132a2a8SHe Kuang if (ret) 3428132a2a8SHe Kuang return ret; 3438132a2a8SHe Kuang 34407ef14d5SIan Rogers return maps__fixup_overlap_and_insert(thread__maps(thread), map); 34595011c60SArnaldo Carvalho de Melo } 34695011c60SArnaldo Carvalho de Melo 34771225af1SIan Rogers struct thread__prepare_access_maps_cb_args { 34871225af1SIan Rogers int err; 34971225af1SIan Rogers struct maps *maps; 35071225af1SIan Rogers }; 35171225af1SIan Rogers 35271225af1SIan Rogers static int thread__prepare_access_maps_cb(struct map *map, void *data) 3536c502584SJiri Olsa { 3546c502584SJiri Olsa bool initialized = false; 35571225af1SIan Rogers struct thread__prepare_access_maps_cb_args *args = data; 3566c502584SJiri Olsa 35771225af1SIan Rogers args->err = unwind__prepare_access(args->maps, map, &initialized); 3586c502584SJiri Olsa 35971225af1SIan Rogers return (args->err || initialized) ? 1 : 0; 3606c502584SJiri Olsa } 3616c502584SJiri Olsa 3626c502584SJiri Olsa static int thread__prepare_access(struct thread *thread) 3636c502584SJiri Olsa { 36471225af1SIan Rogers struct thread__prepare_access_maps_cb_args args = { 36571225af1SIan Rogers .err = 0, 36671225af1SIan Rogers }; 3676c502584SJiri Olsa 36871225af1SIan Rogers if (dwarf_callchain_users) { 36971225af1SIan Rogers args.maps = thread__maps(thread); 37071225af1SIan Rogers maps__for_each_map(thread__maps(thread), thread__prepare_access_maps_cb, &args); 37171225af1SIan Rogers } 3726c502584SJiri Olsa 37371225af1SIan Rogers return args.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. */ 379ee84a303SIan Rogers if (thread__pid(thread) == thread__pid(parent)) 3806c502584SJiri Olsa return thread__prepare_access(thread); 381cddcef60SJiri Olsa 382ff0bd799SIan Rogers if (maps__equal(thread__maps(thread), thread__maps(parent))) { 3830d7e7accSAdrian Hunter pr_debug("broken map groups on thread %d/%d parent %d/%d\n", 384ee84a303SIan Rogers thread__pid(thread), thread__tid(thread), 385ee84a303SIan Rogers thread__pid(parent), thread__tid(parent)); 3860d7e7accSAdrian Hunter return 0; 3870d7e7accSAdrian Hunter } 388cddcef60SJiri Olsa /* But this one is new process, copy maps. */ 38990849527SIan Rogers return do_maps_clone ? maps__copy_from(thread__maps(thread), thread__maps(parent)) : 0; 390cddcef60SJiri Olsa } 391cddcef60SJiri Olsa 3924f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone) 3936baa0a5aSFrederic Weisbecker { 394ee84a303SIan Rogers if (thread__comm_set(parent)) { 3951902efe7SFrederic Weisbecker const char *comm = thread__comm_str(parent); 39618ef15c6SArnaldo Carvalho de Melo int err; 3971902efe7SFrederic Weisbecker if (!comm) 3986baa0a5aSFrederic Weisbecker return -ENOMEM; 3991902efe7SFrederic Weisbecker err = thread__set_comm(thread, comm, timestamp); 4008d00be81SDavid Ahern if (err) 4011902efe7SFrederic Weisbecker return err; 402faa5c5c3SArnaldo Carvalho de Melo } 4036baa0a5aSFrederic Weisbecker 404ee84a303SIan Rogers thread__set_ppid(thread, thread__tid(parent)); 40579b6bb73SArnaldo Carvalho de Melo return thread__clone_maps(thread, parent, do_maps_clone); 4066baa0a5aSFrederic Weisbecker } 40752a3cb8cSArnaldo Carvalho de Melo 40826bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, 40952a3cb8cSArnaldo Carvalho de Melo struct addr_location *al) 41052a3cb8cSArnaldo Carvalho de Melo { 41152a3cb8cSArnaldo Carvalho de Melo size_t i; 4123b556bceSEric Engestrom const u8 cpumodes[] = { 41352a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_USER, 41452a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_KERNEL, 41552a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_USER, 41652a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_KERNEL 41752a3cb8cSArnaldo Carvalho de Melo }; 41852a3cb8cSArnaldo Carvalho de Melo 41952a3cb8cSArnaldo Carvalho de Melo for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { 42026bd9331SArnaldo Carvalho de Melo thread__find_symbol(thread, cpumodes[i], addr, al); 42152a3cb8cSArnaldo Carvalho de Melo if (al->map) 42252a3cb8cSArnaldo Carvalho de Melo break; 42352a3cb8cSArnaldo Carvalho de Melo } 42452a3cb8cSArnaldo Carvalho de Melo } 425480ca357SAndi Kleen 426480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread) 427480ca357SAndi Kleen { 428ee84a303SIan Rogers if (thread__pid(thread) == thread__tid(thread)) 429480ca357SAndi Kleen return thread__get(thread); 430480ca357SAndi Kleen 431ee84a303SIan Rogers if (thread__pid(thread) == -1) 432480ca357SAndi Kleen return NULL; 433480ca357SAndi Kleen 434ee84a303SIan Rogers return machine__find_thread(machine, thread__pid(thread), thread__pid(thread)); 435480ca357SAndi Kleen } 43615325938SAndi Kleen 43715325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine, 43815325938SAndi Kleen void *buf, u64 ip, int len, bool *is64bit) 43915325938SAndi Kleen { 44015325938SAndi Kleen u8 cpumode = PERF_RECORD_MISC_USER; 44115325938SAndi Kleen struct addr_location al; 44263df0e4bSIan Rogers struct dso *dso; 44315325938SAndi Kleen long offset; 44415325938SAndi Kleen 44515325938SAndi Kleen if (machine__kernel_ip(machine, ip)) 44615325938SAndi Kleen cpumode = PERF_RECORD_MISC_KERNEL; 44715325938SAndi Kleen 4480dd5041cSIan Rogers addr_location__init(&al); 4490dd5041cSIan Rogers if (!thread__find_map(thread, cpumode, ip, &al)) { 4500dd5041cSIan Rogers addr_location__exit(&al); 45163df0e4bSIan Rogers return -1; 4520dd5041cSIan Rogers } 45363df0e4bSIan Rogers 45463df0e4bSIan Rogers dso = map__dso(al.map); 45563df0e4bSIan Rogers 456ee756ef7SIan Rogers if (!dso || dso__data(dso)->status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0) { 4570dd5041cSIan Rogers addr_location__exit(&al); 45815325938SAndi Kleen return -1; 4590dd5041cSIan Rogers } 46015325938SAndi Kleen 46178a1f7cdSIan Rogers offset = map__map_ip(al.map, ip); 46215325938SAndi Kleen if (is64bit) 463ee756ef7SIan Rogers *is64bit = dso__is_64_bit(dso); 46415325938SAndi Kleen 4650dd5041cSIan Rogers addr_location__exit(&al); 4660dd5041cSIan Rogers 46763df0e4bSIan Rogers return dso__data_read_offset(dso, machine, offset, buf, len); 46815325938SAndi Kleen } 469ff165628SKan Liang 470ff165628SKan Liang void thread__free_stitch_list(struct thread *thread) 471ff165628SKan Liang { 472ee84a303SIan Rogers struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread); 473ff165628SKan Liang struct stitch_list *pos, *tmp; 474ff165628SKan Liang 475ff165628SKan Liang if (!lbr_stitch) 476ff165628SKan Liang return; 477ff165628SKan Liang 478ff165628SKan Liang list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) { 479*599c1939SIan Rogers map_symbol__exit(&pos->cursor.ms); 480ff165628SKan Liang list_del_init(&pos->node); 481ff165628SKan Liang free(pos); 482ff165628SKan Liang } 483ff165628SKan Liang 484ff165628SKan Liang list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) { 485ff165628SKan Liang list_del_init(&pos->node); 486ff165628SKan Liang free(pos); 487ff165628SKan Liang } 488ff165628SKan Liang 489*599c1939SIan Rogers for (unsigned int i = 0 ; i < lbr_stitch->prev_lbr_cursor_size; i++) 490*599c1939SIan Rogers map_symbol__exit(&lbr_stitch->prev_lbr_cursor[i].ms); 491*599c1939SIan Rogers 492ff165628SKan Liang zfree(&lbr_stitch->prev_lbr_cursor); 493ee84a303SIan Rogers free(thread__lbr_stitch(thread)); 494ee84a303SIan Rogers thread__set_lbr_stitch(thread, NULL); 495ff165628SKan Liang } 496