1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 26baa0a5aSFrederic Weisbecker #include "../perf.h" 3a43783aeSArnaldo Carvalho de Melo #include <errno.h> 46baa0a5aSFrederic Weisbecker #include <stdlib.h> 56baa0a5aSFrederic Weisbecker #include <stdio.h> 66baa0a5aSFrederic Weisbecker #include <string.h> 7877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h> 8b3165f41SArnaldo Carvalho de Melo #include "session.h" 96baa0a5aSFrederic Weisbecker #include "thread.h" 1000447ccdSAdrian Hunter #include "thread-stack.h" 116baa0a5aSFrederic Weisbecker #include "util.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 22cddcef60SJiri Olsa int thread__init_map_groups(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) { 2711246c70SArnaldo Carvalho de Melo thread->mg = map_groups__new(machine); 28cddcef60SJiri Olsa } else { 2918ef15c6SArnaldo Carvalho de Melo struct thread *leader = __machine__findnew_thread(machine, pid, pid); 30abd82868SArnaldo Carvalho de Melo if (leader) { 31cddcef60SJiri Olsa thread->mg = map_groups__get(leader->mg); 32abd82868SArnaldo Carvalho de Melo thread__put(leader); 33abd82868SArnaldo Carvalho de Melo } 34cddcef60SJiri Olsa } 35cddcef60SJiri Olsa 36cddcef60SJiri Olsa return thread->mg ? 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; 50f3b3614aSHari Bathini INIT_LIST_HEAD(&thread->namespaces_list); 511902efe7SFrederic Weisbecker INIT_LIST_HEAD(&thread->comm_list); 52b32ee9e5SKan Liang init_rwsem(&thread->namespaces_lock); 53b32ee9e5SKan Liang init_rwsem(&thread->comm_lock); 541902efe7SFrederic Weisbecker 551902efe7SFrederic Weisbecker comm_str = malloc(32); 561902efe7SFrederic Weisbecker if (!comm_str) 571902efe7SFrederic Weisbecker goto err_thread; 581902efe7SFrederic Weisbecker 591902efe7SFrederic Weisbecker snprintf(comm_str, 32, ":%d", tid); 6065de51f9SAdrian Hunter comm = comm__new(comm_str, 0, false); 611902efe7SFrederic Weisbecker free(comm_str); 621902efe7SFrederic Weisbecker if (!comm) 631902efe7SFrederic Weisbecker goto err_thread; 641902efe7SFrederic Weisbecker 651902efe7SFrederic Weisbecker list_add(&comm->list, &thread->comm_list); 66e34f5b11SElena Reshetova refcount_set(&thread->refcnt, 1); 67b91fc39fSArnaldo Carvalho de Melo RB_CLEAR_NODE(&thread->rb_node); 68843ff37bSKrister Johansen /* Thread holds first ref to nsdata. */ 69843ff37bSKrister Johansen thread->nsinfo = nsinfo__new(pid); 70dd2e18e9SAndi Kleen srccode_state_init(&thread->srccode_state); 716baa0a5aSFrederic Weisbecker } 726baa0a5aSFrederic Weisbecker 73c824c433SArnaldo Carvalho de Melo return thread; 741902efe7SFrederic Weisbecker 751902efe7SFrederic Weisbecker err_thread: 761902efe7SFrederic Weisbecker free(thread); 771902efe7SFrederic Weisbecker return NULL; 786baa0a5aSFrederic Weisbecker } 796baa0a5aSFrederic Weisbecker 80c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread) 81591765fdSArnaldo Carvalho de Melo { 82f3b3614aSHari Bathini struct namespaces *namespaces, *tmp_namespaces; 83f3b3614aSHari Bathini struct comm *comm, *tmp_comm; 841902efe7SFrederic Weisbecker 85b91fc39fSArnaldo Carvalho de Melo BUG_ON(!RB_EMPTY_NODE(&thread->rb_node)); 86b91fc39fSArnaldo Carvalho de Melo 8700447ccdSAdrian Hunter thread_stack__free(thread); 8800447ccdSAdrian Hunter 899608b84eSAdrian Hunter if (thread->mg) { 90a26ca671SArnaldo Carvalho de Melo map_groups__put(thread->mg); 9193d5731dSArnaldo Carvalho de Melo thread->mg = NULL; 929608b84eSAdrian Hunter } 93b32ee9e5SKan Liang down_write(&thread->namespaces_lock); 94f3b3614aSHari Bathini list_for_each_entry_safe(namespaces, tmp_namespaces, 95f3b3614aSHari Bathini &thread->namespaces_list, list) { 96f3b3614aSHari Bathini list_del(&namespaces->list); 97f3b3614aSHari Bathini namespaces__free(namespaces); 98f3b3614aSHari Bathini } 99b32ee9e5SKan Liang up_write(&thread->namespaces_lock); 100b32ee9e5SKan Liang 101b32ee9e5SKan Liang down_write(&thread->comm_lock); 102f3b3614aSHari Bathini list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) { 1031902efe7SFrederic Weisbecker list_del(&comm->list); 1041902efe7SFrederic Weisbecker comm__free(comm); 1051902efe7SFrederic Weisbecker } 106b32ee9e5SKan Liang up_write(&thread->comm_lock); 107b32ee9e5SKan Liang 10866f066d8SNamhyung Kim unwind__finish_access(thread); 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 /* 128abd82868SArnaldo Carvalho de Melo * Remove it from the dead_threads list, as last reference 129abd82868SArnaldo Carvalho de Melo * is gone. 130abd82868SArnaldo Carvalho de Melo */ 131f3b623b8SArnaldo Carvalho de Melo list_del_init(&thread->node); 132f3b623b8SArnaldo Carvalho de Melo thread__delete(thread); 133f3b623b8SArnaldo Carvalho de Melo } 134f3b623b8SArnaldo Carvalho de Melo } 135f3b623b8SArnaldo Carvalho de Melo 1366584140bSNamhyung Kim static struct namespaces *__thread__namespaces(const struct thread *thread) 137f3b3614aSHari Bathini { 138f3b3614aSHari Bathini if (list_empty(&thread->namespaces_list)) 139f3b3614aSHari Bathini return NULL; 140f3b3614aSHari Bathini 141f3b3614aSHari Bathini return list_first_entry(&thread->namespaces_list, struct namespaces, list); 142f3b3614aSHari Bathini } 143f3b3614aSHari Bathini 144*7cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread) 1456584140bSNamhyung Kim { 1466584140bSNamhyung Kim struct namespaces *ns; 1476584140bSNamhyung Kim 148*7cb10a08SNamhyung Kim down_read(&thread->namespaces_lock); 1496584140bSNamhyung Kim ns = __thread__namespaces(thread); 150*7cb10a08SNamhyung Kim up_read(&thread->namespaces_lock); 1516584140bSNamhyung Kim 1526584140bSNamhyung Kim return ns; 1536584140bSNamhyung Kim } 1546584140bSNamhyung Kim 155b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp, 156f3b3614aSHari Bathini struct namespaces_event *event) 157f3b3614aSHari Bathini { 1586584140bSNamhyung Kim struct namespaces *new, *curr = __thread__namespaces(thread); 159f3b3614aSHari Bathini 160f3b3614aSHari Bathini new = namespaces__new(event); 161f3b3614aSHari Bathini if (!new) 162f3b3614aSHari Bathini return -ENOMEM; 163f3b3614aSHari Bathini 164f3b3614aSHari Bathini list_add(&new->list, &thread->namespaces_list); 165f3b3614aSHari Bathini 166f3b3614aSHari Bathini if (timestamp && curr) { 167f3b3614aSHari Bathini /* 168f3b3614aSHari Bathini * setns syscall must have changed few or all the namespaces 169f3b3614aSHari Bathini * of this thread. Update end time for the namespaces 170f3b3614aSHari Bathini * previously used. 171f3b3614aSHari Bathini */ 172f3b3614aSHari Bathini curr = list_next_entry(new, list); 173f3b3614aSHari Bathini curr->end_time = timestamp; 174f3b3614aSHari Bathini } 175f3b3614aSHari Bathini 176f3b3614aSHari Bathini return 0; 177f3b3614aSHari Bathini } 178f3b3614aSHari Bathini 179b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp, 180b32ee9e5SKan Liang struct namespaces_event *event) 181b32ee9e5SKan Liang { 182b32ee9e5SKan Liang int ret; 183b32ee9e5SKan Liang 184b32ee9e5SKan Liang down_write(&thread->namespaces_lock); 185b32ee9e5SKan Liang ret = __thread__set_namespaces(thread, timestamp, event); 186b32ee9e5SKan Liang up_write(&thread->namespaces_lock); 187b32ee9e5SKan Liang return ret; 188b32ee9e5SKan Liang } 189b32ee9e5SKan Liang 1904dfced35SNamhyung Kim struct comm *thread__comm(const struct thread *thread) 1916baa0a5aSFrederic Weisbecker { 1921902efe7SFrederic Weisbecker if (list_empty(&thread->comm_list)) 1931902efe7SFrederic Weisbecker return NULL; 1944385d580SDavid S. Miller 1951902efe7SFrederic Weisbecker return list_first_entry(&thread->comm_list, struct comm, list); 1964385d580SDavid S. Miller } 1971902efe7SFrederic Weisbecker 19865de51f9SAdrian Hunter struct comm *thread__exec_comm(const struct thread *thread) 19965de51f9SAdrian Hunter { 20065de51f9SAdrian Hunter struct comm *comm, *last = NULL; 20165de51f9SAdrian Hunter 20265de51f9SAdrian Hunter list_for_each_entry(comm, &thread->comm_list, list) { 20365de51f9SAdrian Hunter if (comm->exec) 20465de51f9SAdrian Hunter return comm; 20565de51f9SAdrian Hunter last = comm; 20665de51f9SAdrian Hunter } 20765de51f9SAdrian Hunter 20865de51f9SAdrian Hunter return last; 20965de51f9SAdrian Hunter } 21065de51f9SAdrian Hunter 211b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str, 212b32ee9e5SKan Liang u64 timestamp, bool exec) 2131902efe7SFrederic Weisbecker { 2141902efe7SFrederic Weisbecker struct comm *new, *curr = thread__comm(thread); 2151902efe7SFrederic Weisbecker 216a8480808SAdrian Hunter /* Override the default :tid entry */ 217a8480808SAdrian Hunter if (!thread->comm_set) { 21818ef15c6SArnaldo Carvalho de Melo int err = comm__override(curr, str, timestamp, exec); 2193178f58bSFrederic Weisbecker if (err) 2203178f58bSFrederic Weisbecker return err; 221a5285ad9SFrederic Weisbecker } else { 22265de51f9SAdrian Hunter new = comm__new(str, timestamp, exec); 2231902efe7SFrederic Weisbecker if (!new) 2241902efe7SFrederic Weisbecker return -ENOMEM; 2251902efe7SFrederic Weisbecker list_add(&new->list, &thread->comm_list); 226380b5143SNamhyung Kim 227380b5143SNamhyung Kim if (exec) 228380b5143SNamhyung Kim unwind__flush_access(thread); 229a5285ad9SFrederic Weisbecker } 230a5285ad9SFrederic Weisbecker 2311902efe7SFrederic Weisbecker thread->comm_set = true; 2321902efe7SFrederic Weisbecker 2331902efe7SFrederic Weisbecker return 0; 2346baa0a5aSFrederic Weisbecker } 2356baa0a5aSFrederic Weisbecker 236b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, 237b32ee9e5SKan Liang bool exec) 238b32ee9e5SKan Liang { 239b32ee9e5SKan Liang int ret; 240b32ee9e5SKan Liang 241b32ee9e5SKan Liang down_write(&thread->comm_lock); 242b32ee9e5SKan Liang ret = ____thread__set_comm(thread, str, timestamp, exec); 243b32ee9e5SKan Liang up_write(&thread->comm_lock); 244b32ee9e5SKan Liang return ret; 245b32ee9e5SKan Liang } 246b32ee9e5SKan Liang 2472f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread) 2482f3027acSArnaldo Carvalho de Melo { 2492f3027acSArnaldo Carvalho de Melo char path[64]; 2502f3027acSArnaldo Carvalho de Melo char *comm = NULL; 2512f3027acSArnaldo Carvalho de Melo size_t sz; 2522f3027acSArnaldo Carvalho de Melo int err = -1; 2532f3027acSArnaldo Carvalho de Melo 2542f3027acSArnaldo Carvalho de Melo if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", 2552f3027acSArnaldo Carvalho de Melo thread->pid_, thread->tid) >= (int)sizeof(path)) && 2562f3027acSArnaldo Carvalho de Melo procfs__read_str(path, &comm, &sz) == 0) { 2572f3027acSArnaldo Carvalho de Melo comm[sz - 1] = '\0'; 2582f3027acSArnaldo Carvalho de Melo err = thread__set_comm(thread, comm, 0); 2592f3027acSArnaldo Carvalho de Melo } 2602f3027acSArnaldo Carvalho de Melo 2612f3027acSArnaldo Carvalho de Melo return err; 2622f3027acSArnaldo Carvalho de Melo } 2632f3027acSArnaldo Carvalho de Melo 264b32ee9e5SKan Liang static const char *__thread__comm_str(const struct thread *thread) 265b9c5143aSFrederic Weisbecker { 2661902efe7SFrederic Weisbecker const struct comm *comm = thread__comm(thread); 2671902efe7SFrederic Weisbecker 2681902efe7SFrederic Weisbecker if (!comm) 2691902efe7SFrederic Weisbecker return NULL; 2701902efe7SFrederic Weisbecker 2711902efe7SFrederic Weisbecker return comm__str(comm); 272b9c5143aSFrederic Weisbecker } 273b9c5143aSFrederic Weisbecker 274*7cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread) 275b32ee9e5SKan Liang { 276b32ee9e5SKan Liang const char *str; 277b32ee9e5SKan Liang 278*7cb10a08SNamhyung Kim down_read(&thread->comm_lock); 279b32ee9e5SKan Liang str = __thread__comm_str(thread); 280*7cb10a08SNamhyung Kim up_read(&thread->comm_lock); 281b32ee9e5SKan Liang 282b32ee9e5SKan Liang return str; 283b32ee9e5SKan Liang } 284b32ee9e5SKan Liang 2851902efe7SFrederic Weisbecker /* CHECKME: it should probably better return the max comm len from its comm list */ 286c824c433SArnaldo Carvalho de Melo int thread__comm_len(struct thread *thread) 287a4fb581bSFrederic Weisbecker { 288c824c433SArnaldo Carvalho de Melo if (!thread->comm_len) { 2891902efe7SFrederic Weisbecker const char *comm = thread__comm_str(thread); 2901902efe7SFrederic Weisbecker if (!comm) 291a4fb581bSFrederic Weisbecker return 0; 2921902efe7SFrederic Weisbecker thread->comm_len = strlen(comm); 293a4fb581bSFrederic Weisbecker } 294a4fb581bSFrederic Weisbecker 295c824c433SArnaldo Carvalho de Melo return thread->comm_len; 296a4fb581bSFrederic Weisbecker } 297a4fb581bSFrederic Weisbecker 2983f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp) 29995011c60SArnaldo Carvalho de Melo { 300b9c5143aSFrederic Weisbecker return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + 301acebd408SJiri Olsa map_groups__fprintf(thread->mg, fp); 3026baa0a5aSFrederic Weisbecker } 3036baa0a5aSFrederic Weisbecker 3048132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map) 3051b46cddfSArnaldo Carvalho de Melo { 3068132a2a8SHe Kuang int ret; 3078132a2a8SHe Kuang 308a2873325SJiri Olsa ret = unwind__prepare_access(thread, map, NULL); 3098132a2a8SHe Kuang if (ret) 3108132a2a8SHe Kuang return ret; 3118132a2a8SHe Kuang 312acebd408SJiri Olsa map_groups__fixup_overlappings(thread->mg, map, stderr); 31393d5731dSArnaldo Carvalho de Melo map_groups__insert(thread->mg, map); 3148132a2a8SHe Kuang 3158132a2a8SHe Kuang return 0; 31695011c60SArnaldo Carvalho de Melo } 31795011c60SArnaldo Carvalho de Melo 3186c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread) 3196c502584SJiri Olsa { 3206c502584SJiri Olsa bool initialized = false; 3213183f8caSArnaldo Carvalho de Melo int err = 0; 3223183f8caSArnaldo Carvalho de Melo struct maps *maps = &thread->mg->maps; 3236c502584SJiri Olsa struct map *map; 3246c502584SJiri Olsa 3250a7c74eaSArnaldo Carvalho de Melo down_read(&maps->lock); 3266c502584SJiri Olsa 3276c502584SJiri Olsa for (map = maps__first(maps); map; map = map__next(map)) { 3286c502584SJiri Olsa err = unwind__prepare_access(thread, map, &initialized); 3296c502584SJiri Olsa if (err || initialized) 3306c502584SJiri Olsa break; 3316c502584SJiri Olsa } 3326c502584SJiri Olsa 3330a7c74eaSArnaldo Carvalho de Melo up_read(&maps->lock); 3346c502584SJiri Olsa 3356c502584SJiri Olsa return err; 3366c502584SJiri Olsa } 3376c502584SJiri Olsa 3386c502584SJiri Olsa static int thread__prepare_access(struct thread *thread) 3396c502584SJiri Olsa { 3406c502584SJiri Olsa int err = 0; 3416c502584SJiri Olsa 342382619c0SJiri Olsa if (dwarf_callchain_users) 3436c502584SJiri Olsa err = __thread__prepare_access(thread); 3446c502584SJiri Olsa 3456c502584SJiri Olsa return err; 3466c502584SJiri Olsa } 3476c502584SJiri Olsa 348cddcef60SJiri Olsa static int thread__clone_map_groups(struct thread *thread, 3494f8f382eSDavid Miller struct thread *parent, 3504f8f382eSDavid Miller bool do_maps_clone) 351cddcef60SJiri Olsa { 352cddcef60SJiri Olsa /* This is new thread, we share map groups for process. */ 353cddcef60SJiri Olsa if (thread->pid_ == parent->pid_) 3546c502584SJiri Olsa return thread__prepare_access(thread); 355cddcef60SJiri Olsa 3560d7e7accSAdrian Hunter if (thread->mg == parent->mg) { 3570d7e7accSAdrian Hunter pr_debug("broken map groups on thread %d/%d parent %d/%d\n", 3580d7e7accSAdrian Hunter thread->pid_, thread->tid, parent->pid_, parent->tid); 3590d7e7accSAdrian Hunter return 0; 3600d7e7accSAdrian Hunter } 361cddcef60SJiri Olsa /* But this one is new process, copy maps. */ 3624f8f382eSDavid Miller return do_maps_clone ? map_groups__clone(thread, parent->mg) : 0; 363cddcef60SJiri Olsa } 364cddcef60SJiri Olsa 3654f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone) 3666baa0a5aSFrederic Weisbecker { 367faa5c5c3SArnaldo Carvalho de Melo if (parent->comm_set) { 3681902efe7SFrederic Weisbecker const char *comm = thread__comm_str(parent); 36918ef15c6SArnaldo Carvalho de Melo int err; 3701902efe7SFrederic Weisbecker if (!comm) 3716baa0a5aSFrederic Weisbecker return -ENOMEM; 3721902efe7SFrederic Weisbecker err = thread__set_comm(thread, comm, timestamp); 3738d00be81SDavid Ahern if (err) 3741902efe7SFrederic Weisbecker return err; 375faa5c5c3SArnaldo Carvalho de Melo } 3766baa0a5aSFrederic Weisbecker 377c824c433SArnaldo Carvalho de Melo thread->ppid = parent->tid; 3784f8f382eSDavid Miller return thread__clone_map_groups(thread, parent, do_maps_clone); 3796baa0a5aSFrederic Weisbecker } 38052a3cb8cSArnaldo Carvalho de Melo 38126bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, 38252a3cb8cSArnaldo Carvalho de Melo struct addr_location *al) 38352a3cb8cSArnaldo Carvalho de Melo { 38452a3cb8cSArnaldo Carvalho de Melo size_t i; 3853b556bceSEric Engestrom const u8 cpumodes[] = { 38652a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_USER, 38752a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_KERNEL, 38852a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_USER, 38952a3cb8cSArnaldo Carvalho de Melo PERF_RECORD_MISC_GUEST_KERNEL 39052a3cb8cSArnaldo Carvalho de Melo }; 39152a3cb8cSArnaldo Carvalho de Melo 39252a3cb8cSArnaldo Carvalho de Melo for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { 39326bd9331SArnaldo Carvalho de Melo thread__find_symbol(thread, cpumodes[i], addr, al); 39452a3cb8cSArnaldo Carvalho de Melo if (al->map) 39552a3cb8cSArnaldo Carvalho de Melo break; 39652a3cb8cSArnaldo Carvalho de Melo } 39752a3cb8cSArnaldo Carvalho de Melo } 398480ca357SAndi Kleen 399480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread) 400480ca357SAndi Kleen { 401480ca357SAndi Kleen if (thread->pid_ == thread->tid) 402480ca357SAndi Kleen return thread__get(thread); 403480ca357SAndi Kleen 404480ca357SAndi Kleen if (thread->pid_ == -1) 405480ca357SAndi Kleen return NULL; 406480ca357SAndi Kleen 407480ca357SAndi Kleen return machine__find_thread(machine, thread->pid_, thread->pid_); 408480ca357SAndi Kleen } 40915325938SAndi Kleen 41015325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine, 41115325938SAndi Kleen void *buf, u64 ip, int len, bool *is64bit) 41215325938SAndi Kleen { 41315325938SAndi Kleen u8 cpumode = PERF_RECORD_MISC_USER; 41415325938SAndi Kleen struct addr_location al; 41515325938SAndi Kleen long offset; 41615325938SAndi Kleen 41715325938SAndi Kleen if (machine__kernel_ip(machine, ip)) 41815325938SAndi Kleen cpumode = PERF_RECORD_MISC_KERNEL; 41915325938SAndi Kleen 42015325938SAndi Kleen if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso || 42115325938SAndi Kleen al.map->dso->data.status == DSO_DATA_STATUS_ERROR || 42215325938SAndi Kleen map__load(al.map) < 0) 42315325938SAndi Kleen return -1; 42415325938SAndi Kleen 42515325938SAndi Kleen offset = al.map->map_ip(al.map, ip); 42615325938SAndi Kleen if (is64bit) 42715325938SAndi Kleen *is64bit = al.map->dso->is_64_bit; 42815325938SAndi Kleen 42915325938SAndi Kleen return dso__data_read_offset(al.map->dso, machine, offset, buf, len); 43015325938SAndi Kleen } 431