Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <string.h>
0003 #include "perf_regs.h"
0004 #include "thread.h"
0005 #include "map.h"
0006 #include "maps.h"
0007 #include "event.h"
0008 #include "debug.h"
0009 #include "tests/tests.h"
0010 
0011 #define STACK_SIZE 8192
0012 
0013 static int sample_ustack(struct perf_sample *sample,
0014              struct thread *thread, u64 *regs)
0015 {
0016     struct stack_dump *stack = &sample->user_stack;
0017     struct map *map;
0018     unsigned long sp;
0019     u64 stack_size, *buf;
0020 
0021     buf = malloc(STACK_SIZE);
0022     if (!buf) {
0023         pr_debug("failed to allocate sample uregs data\n");
0024         return -1;
0025     }
0026 
0027     sp = (unsigned long) regs[PERF_REG_X86_SP];
0028 
0029     map = maps__find(thread->maps, (u64)sp);
0030     if (!map) {
0031         pr_debug("failed to get stack map\n");
0032         free(buf);
0033         return -1;
0034     }
0035 
0036     stack_size = map->end - sp;
0037     stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
0038 
0039     memcpy(buf, (void *) sp, stack_size);
0040 #ifdef MEMORY_SANITIZER
0041     /*
0042      * Copying the stack may copy msan poison, avoid false positives in the
0043      * unwinder by removing the poison here.
0044      */
0045     __msan_unpoison(buf, stack_size);
0046 #endif
0047     stack->data = (char *) buf;
0048     stack->size = stack_size;
0049     return 0;
0050 }
0051 
0052 int test__arch_unwind_sample(struct perf_sample *sample,
0053                  struct thread *thread)
0054 {
0055     struct regs_dump *regs = &sample->user_regs;
0056     u64 *buf;
0057 
0058     buf = malloc(sizeof(u64) * PERF_REGS_MAX);
0059     if (!buf) {
0060         pr_debug("failed to allocate sample uregs data\n");
0061         return -1;
0062     }
0063 
0064 #ifdef MEMORY_SANITIZER
0065     /*
0066      * Assignments to buf in the assembly function perf_regs_load aren't
0067      * seen by memory sanitizer. Zero the memory to convince memory
0068      * sanitizer the memory is initialized.
0069      */
0070     memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);
0071 #endif
0072     perf_regs_load(buf);
0073     regs->abi  = PERF_SAMPLE_REGS_ABI;
0074     regs->regs = buf;
0075     regs->mask = PERF_REGS_MASK;
0076 
0077     return sample_ustack(sample, thread, buf);
0078 }