Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <errno.h>
0003 #include <stdlib.h>
0004 #include <stdio.h>
0005 #include <string.h>
0006 #include <linux/kernel.h>
0007 #include <linux/zalloc.h>
0008 #include "dso.h"
0009 #include "session.h"
0010 #include "thread.h"
0011 #include "thread-stack.h"
0012 #include "debug.h"
0013 #include "namespaces.h"
0014 #include "comm.h"
0015 #include "map.h"
0016 #include "symbol.h"
0017 #include "unwind.h"
0018 #include "callchain.h"
0019 
0020 #include <api/fs/fs.h>
0021 
0022 int thread__init_maps(struct thread *thread, struct machine *machine)
0023 {
0024     pid_t pid = thread->pid_;
0025 
0026     if (pid == thread->tid || pid == -1) {
0027         thread->maps = maps__new(machine);
0028     } else {
0029         struct thread *leader = __machine__findnew_thread(machine, pid, pid);
0030         if (leader) {
0031             thread->maps = maps__get(leader->maps);
0032             thread__put(leader);
0033         }
0034     }
0035 
0036     return thread->maps ? 0 : -1;
0037 }
0038 
0039 struct thread *thread__new(pid_t pid, pid_t tid)
0040 {
0041     char *comm_str;
0042     struct comm *comm;
0043     struct thread *thread = zalloc(sizeof(*thread));
0044 
0045     if (thread != NULL) {
0046         thread->pid_ = pid;
0047         thread->tid = tid;
0048         thread->ppid = -1;
0049         thread->cpu = -1;
0050         thread->guest_cpu = -1;
0051         thread->lbr_stitch_enable = false;
0052         INIT_LIST_HEAD(&thread->namespaces_list);
0053         INIT_LIST_HEAD(&thread->comm_list);
0054         init_rwsem(&thread->namespaces_lock);
0055         init_rwsem(&thread->comm_lock);
0056 
0057         comm_str = malloc(32);
0058         if (!comm_str)
0059             goto err_thread;
0060 
0061         snprintf(comm_str, 32, ":%d", tid);
0062         comm = comm__new(comm_str, 0, false);
0063         free(comm_str);
0064         if (!comm)
0065             goto err_thread;
0066 
0067         list_add(&comm->list, &thread->comm_list);
0068         refcount_set(&thread->refcnt, 1);
0069         RB_CLEAR_NODE(&thread->rb_node);
0070         /* Thread holds first ref to nsdata. */
0071         thread->nsinfo = nsinfo__new(pid);
0072         srccode_state_init(&thread->srccode_state);
0073     }
0074 
0075     return thread;
0076 
0077 err_thread:
0078     free(thread);
0079     return NULL;
0080 }
0081 
0082 void thread__delete(struct thread *thread)
0083 {
0084     struct namespaces *namespaces, *tmp_namespaces;
0085     struct comm *comm, *tmp_comm;
0086 
0087     BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
0088 
0089     thread_stack__free(thread);
0090 
0091     if (thread->maps) {
0092         maps__put(thread->maps);
0093         thread->maps = NULL;
0094     }
0095     down_write(&thread->namespaces_lock);
0096     list_for_each_entry_safe(namespaces, tmp_namespaces,
0097                  &thread->namespaces_list, list) {
0098         list_del_init(&namespaces->list);
0099         namespaces__free(namespaces);
0100     }
0101     up_write(&thread->namespaces_lock);
0102 
0103     down_write(&thread->comm_lock);
0104     list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
0105         list_del_init(&comm->list);
0106         comm__free(comm);
0107     }
0108     up_write(&thread->comm_lock);
0109 
0110     nsinfo__zput(thread->nsinfo);
0111     srccode_state_free(&thread->srccode_state);
0112 
0113     exit_rwsem(&thread->namespaces_lock);
0114     exit_rwsem(&thread->comm_lock);
0115     thread__free_stitch_list(thread);
0116     free(thread);
0117 }
0118 
0119 struct thread *thread__get(struct thread *thread)
0120 {
0121     if (thread)
0122         refcount_inc(&thread->refcnt);
0123     return thread;
0124 }
0125 
0126 void thread__put(struct thread *thread)
0127 {
0128     if (thread && refcount_dec_and_test(&thread->refcnt)) {
0129         /*
0130          * Remove it from the dead threads list, as last reference is
0131          * gone, if it is in a dead threads list.
0132          *
0133          * We may not be there anymore if say, the machine where it was
0134          * stored was already deleted, so we already removed it from
0135          * the dead threads and some other piece of code still keeps a
0136          * reference.
0137          *
0138          * This is what 'perf sched' does and finally drops it in
0139          * perf_sched__lat(), where it calls perf_sched__read_events(),
0140          * that processes the events by creating a session and deleting
0141          * it, which ends up destroying the list heads for the dead
0142          * threads, but before it does that it removes all threads from
0143          * it using list_del_init().
0144          *
0145          * So we need to check here if it is in a dead threads list and
0146          * if so, remove it before finally deleting the thread, to avoid
0147          * an use after free situation.
0148          */
0149         if (!list_empty(&thread->node))
0150             list_del_init(&thread->node);
0151         thread__delete(thread);
0152     }
0153 }
0154 
0155 static struct namespaces *__thread__namespaces(const struct thread *thread)
0156 {
0157     if (list_empty(&thread->namespaces_list))
0158         return NULL;
0159 
0160     return list_first_entry(&thread->namespaces_list, struct namespaces, list);
0161 }
0162 
0163 struct namespaces *thread__namespaces(struct thread *thread)
0164 {
0165     struct namespaces *ns;
0166 
0167     down_read(&thread->namespaces_lock);
0168     ns = __thread__namespaces(thread);
0169     up_read(&thread->namespaces_lock);
0170 
0171     return ns;
0172 }
0173 
0174 static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
0175                     struct perf_record_namespaces *event)
0176 {
0177     struct namespaces *new, *curr = __thread__namespaces(thread);
0178 
0179     new = namespaces__new(event);
0180     if (!new)
0181         return -ENOMEM;
0182 
0183     list_add(&new->list, &thread->namespaces_list);
0184 
0185     if (timestamp && curr) {
0186         /*
0187          * setns syscall must have changed few or all the namespaces
0188          * of this thread. Update end time for the namespaces
0189          * previously used.
0190          */
0191         curr = list_next_entry(new, list);
0192         curr->end_time = timestamp;
0193     }
0194 
0195     return 0;
0196 }
0197 
0198 int thread__set_namespaces(struct thread *thread, u64 timestamp,
0199                struct perf_record_namespaces *event)
0200 {
0201     int ret;
0202 
0203     down_write(&thread->namespaces_lock);
0204     ret = __thread__set_namespaces(thread, timestamp, event);
0205     up_write(&thread->namespaces_lock);
0206     return ret;
0207 }
0208 
0209 struct comm *thread__comm(const struct thread *thread)
0210 {
0211     if (list_empty(&thread->comm_list))
0212         return NULL;
0213 
0214     return list_first_entry(&thread->comm_list, struct comm, list);
0215 }
0216 
0217 struct comm *thread__exec_comm(const struct thread *thread)
0218 {
0219     struct comm *comm, *last = NULL, *second_last = NULL;
0220 
0221     list_for_each_entry(comm, &thread->comm_list, list) {
0222         if (comm->exec)
0223             return comm;
0224         second_last = last;
0225         last = comm;
0226     }
0227 
0228     /*
0229      * 'last' with no start time might be the parent's comm of a synthesized
0230      * thread (created by processing a synthesized fork event). For a main
0231      * thread, that is very probably wrong. Prefer a later comm to avoid
0232      * that case.
0233      */
0234     if (second_last && !last->start && thread->pid_ == thread->tid)
0235         return second_last;
0236 
0237     return last;
0238 }
0239 
0240 static int ____thread__set_comm(struct thread *thread, const char *str,
0241                 u64 timestamp, bool exec)
0242 {
0243     struct comm *new, *curr = thread__comm(thread);
0244 
0245     /* Override the default :tid entry */
0246     if (!thread->comm_set) {
0247         int err = comm__override(curr, str, timestamp, exec);
0248         if (err)
0249             return err;
0250     } else {
0251         new = comm__new(str, timestamp, exec);
0252         if (!new)
0253             return -ENOMEM;
0254         list_add(&new->list, &thread->comm_list);
0255 
0256         if (exec)
0257             unwind__flush_access(thread->maps);
0258     }
0259 
0260     thread->comm_set = true;
0261 
0262     return 0;
0263 }
0264 
0265 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
0266                bool exec)
0267 {
0268     int ret;
0269 
0270     down_write(&thread->comm_lock);
0271     ret = ____thread__set_comm(thread, str, timestamp, exec);
0272     up_write(&thread->comm_lock);
0273     return ret;
0274 }
0275 
0276 int thread__set_comm_from_proc(struct thread *thread)
0277 {
0278     char path[64];
0279     char *comm = NULL;
0280     size_t sz;
0281     int err = -1;
0282 
0283     if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
0284                thread->pid_, thread->tid) >= (int)sizeof(path)) &&
0285         procfs__read_str(path, &comm, &sz) == 0) {
0286         comm[sz - 1] = '\0';
0287         err = thread__set_comm(thread, comm, 0);
0288     }
0289 
0290     return err;
0291 }
0292 
0293 static const char *__thread__comm_str(const struct thread *thread)
0294 {
0295     const struct comm *comm = thread__comm(thread);
0296 
0297     if (!comm)
0298         return NULL;
0299 
0300     return comm__str(comm);
0301 }
0302 
0303 const char *thread__comm_str(struct thread *thread)
0304 {
0305     const char *str;
0306 
0307     down_read(&thread->comm_lock);
0308     str = __thread__comm_str(thread);
0309     up_read(&thread->comm_lock);
0310 
0311     return str;
0312 }
0313 
0314 /* CHECKME: it should probably better return the max comm len from its comm list */
0315 int thread__comm_len(struct thread *thread)
0316 {
0317     if (!thread->comm_len) {
0318         const char *comm = thread__comm_str(thread);
0319         if (!comm)
0320             return 0;
0321         thread->comm_len = strlen(comm);
0322     }
0323 
0324     return thread->comm_len;
0325 }
0326 
0327 size_t thread__fprintf(struct thread *thread, FILE *fp)
0328 {
0329     return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
0330            maps__fprintf(thread->maps, fp);
0331 }
0332 
0333 int thread__insert_map(struct thread *thread, struct map *map)
0334 {
0335     int ret;
0336 
0337     ret = unwind__prepare_access(thread->maps, map, NULL);
0338     if (ret)
0339         return ret;
0340 
0341     maps__fixup_overlappings(thread->maps, map, stderr);
0342     maps__insert(thread->maps, map);
0343 
0344     return 0;
0345 }
0346 
0347 static int __thread__prepare_access(struct thread *thread)
0348 {
0349     bool initialized = false;
0350     int err = 0;
0351     struct maps *maps = thread->maps;
0352     struct map *map;
0353 
0354     down_read(&maps->lock);
0355 
0356     maps__for_each_entry(maps, map) {
0357         err = unwind__prepare_access(thread->maps, map, &initialized);
0358         if (err || initialized)
0359             break;
0360     }
0361 
0362     up_read(&maps->lock);
0363 
0364     return err;
0365 }
0366 
0367 static int thread__prepare_access(struct thread *thread)
0368 {
0369     int err = 0;
0370 
0371     if (dwarf_callchain_users)
0372         err = __thread__prepare_access(thread);
0373 
0374     return err;
0375 }
0376 
0377 static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
0378 {
0379     /* This is new thread, we share map groups for process. */
0380     if (thread->pid_ == parent->pid_)
0381         return thread__prepare_access(thread);
0382 
0383     if (thread->maps == parent->maps) {
0384         pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
0385              thread->pid_, thread->tid, parent->pid_, parent->tid);
0386         return 0;
0387     }
0388     /* But this one is new process, copy maps. */
0389     return do_maps_clone ? maps__clone(thread, parent->maps) : 0;
0390 }
0391 
0392 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
0393 {
0394     if (parent->comm_set) {
0395         const char *comm = thread__comm_str(parent);
0396         int err;
0397         if (!comm)
0398             return -ENOMEM;
0399         err = thread__set_comm(thread, comm, timestamp);
0400         if (err)
0401             return err;
0402     }
0403 
0404     thread->ppid = parent->tid;
0405     return thread__clone_maps(thread, parent, do_maps_clone);
0406 }
0407 
0408 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
0409                     struct addr_location *al)
0410 {
0411     size_t i;
0412     const u8 cpumodes[] = {
0413         PERF_RECORD_MISC_USER,
0414         PERF_RECORD_MISC_KERNEL,
0415         PERF_RECORD_MISC_GUEST_USER,
0416         PERF_RECORD_MISC_GUEST_KERNEL
0417     };
0418 
0419     for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
0420         thread__find_symbol(thread, cpumodes[i], addr, al);
0421         if (al->map)
0422             break;
0423     }
0424 }
0425 
0426 struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
0427 {
0428     if (thread->pid_ == thread->tid)
0429         return thread__get(thread);
0430 
0431     if (thread->pid_ == -1)
0432         return NULL;
0433 
0434     return machine__find_thread(machine, thread->pid_, thread->pid_);
0435 }
0436 
0437 int thread__memcpy(struct thread *thread, struct machine *machine,
0438            void *buf, u64 ip, int len, bool *is64bit)
0439 {
0440        u8 cpumode = PERF_RECORD_MISC_USER;
0441        struct addr_location al;
0442        long offset;
0443 
0444        if (machine__kernel_ip(machine, ip))
0445                cpumode = PERF_RECORD_MISC_KERNEL;
0446 
0447        if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso ||
0448        al.map->dso->data.status == DSO_DATA_STATUS_ERROR ||
0449        map__load(al.map) < 0)
0450                return -1;
0451 
0452        offset = al.map->map_ip(al.map, ip);
0453        if (is64bit)
0454                *is64bit = al.map->dso->is_64_bit;
0455 
0456        return dso__data_read_offset(al.map->dso, machine, offset, buf, len);
0457 }
0458 
0459 void thread__free_stitch_list(struct thread *thread)
0460 {
0461     struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
0462     struct stitch_list *pos, *tmp;
0463 
0464     if (!lbr_stitch)
0465         return;
0466 
0467     list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
0468         list_del_init(&pos->node);
0469         free(pos);
0470     }
0471 
0472     list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
0473         list_del_init(&pos->node);
0474         free(pos);
0475     }
0476 
0477     zfree(&lbr_stitch->prev_lbr_cursor);
0478     zfree(&thread->lbr_stitch);
0479 }