1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * Libunwind support for mc_address_space objects.
11 // We need this for the register indices:
12 // #define _GNU_SOURCE
16 // On x86_64, libunwind unw_context_t has the same layout as ucontext_t:
17 #include <sys/ucontext.h>
19 #include <libunwind.h>
21 #include "mc_object_info.h"
22 #include "mc/Process.hpp"
24 #include "mc/Frame.hpp"
26 using simgrid::mc::remote;
30 // ***** Implementation
32 /** Get frame unwind information (libunwind method)
34 * Delegates to the local/ptrace implementation.
36 static int find_proc_info(unw_addr_space_t as,
37 unw_word_t ip, unw_proc_info_t *pip,
38 int need_unwind_info, void* arg)
40 mc_unw_context_t context = (mc_unw_context_t) arg;
41 return unw_get_accessors(context->process->unw_underlying_addr_space)->find_proc_info(
42 context->process->unw_underlying_addr_space, ip, pip,
43 need_unwind_info, context->process->unw_underlying_context
47 /** Release frame unwind information (libunwind method)
49 * Delegates to the local/ptrace implementation.
51 static void put_unwind_info(unw_addr_space_t as,
52 unw_proc_info_t *pip, void* arg)
54 mc_unw_context_t context = (mc_unw_context_t) arg;
55 return unw_get_accessors(context->process->unw_underlying_addr_space)->put_unwind_info(
56 context->process->unw_underlying_addr_space, pip,
57 context->process->unw_underlying_context
61 /** (libunwind method)
65 static int get_dyn_info_list_addr(unw_addr_space_t as,
66 unw_word_t *dilap, void* arg)
68 mc_unw_context_t context = (mc_unw_context_t) arg;
69 return unw_get_accessors(context->process->unw_underlying_addr_space)->get_dyn_info_list_addr(
70 context->process->unw_underlying_addr_space,
72 context->process->unw_underlying_context
76 /** Read from the target address space memory (libunwind method)
78 * Delegates to the `simgrid::mc::Process*`.
80 static int access_mem(unw_addr_space_t as,
81 unw_word_t addr, unw_word_t *valp,
84 mc_unw_context_t context = (mc_unw_context_t) arg;
86 return - UNW_EREADONLYREG;
87 context->address_space->read_bytes(valp, sizeof(unw_word_t), remote(addr));
91 static void* get_reg(unw_context_t* context, unw_regnum_t regnum)
94 mcontext_t* mcontext = &context->uc_mcontext;
96 case UNW_X86_64_RAX: return &mcontext->gregs[REG_RAX];
97 case UNW_X86_64_RDX: return &mcontext->gregs[REG_RDX];
98 case UNW_X86_64_RCX: return &mcontext->gregs[REG_RCX];
99 case UNW_X86_64_RBX: return &mcontext->gregs[REG_RBX];
100 case UNW_X86_64_RSI: return &mcontext->gregs[REG_RSI];
101 case UNW_X86_64_RDI: return &mcontext->gregs[REG_RDI];
102 case UNW_X86_64_RBP: return &mcontext->gregs[REG_RBP];
103 case UNW_X86_64_RSP: return &mcontext->gregs[REG_RSP];
104 case UNW_X86_64_R8: return &mcontext->gregs[REG_R8];
105 case UNW_X86_64_R9: return &mcontext->gregs[REG_R9];
106 case UNW_X86_64_R10: return &mcontext->gregs[REG_R10];
107 case UNW_X86_64_R11: return &mcontext->gregs[REG_R11];
108 case UNW_X86_64_R12: return &mcontext->gregs[REG_R12];
109 case UNW_X86_64_R13: return &mcontext->gregs[REG_R13];
110 case UNW_X86_64_R14: return &mcontext->gregs[REG_R14];
111 case UNW_X86_64_R15: return &mcontext->gregs[REG_R15];
112 case UNW_X86_64_RIP: return &mcontext->gregs[REG_RIP];
113 default: return NULL;
120 /** Read a standard register (libunwind method)
122 static int access_reg(unw_addr_space_t as,
123 unw_regnum_t regnum, unw_word_t *valp,
124 int write, void* arg)
126 mc_unw_context_t as_context = (mc_unw_context_t) arg;
127 unw_context_t* context = &as_context->context;
129 return -UNW_EREADONLYREG;
130 greg_t* preg = (greg_t*) get_reg(context, regnum);
137 /** Read a floating-point register (libunwind method)
139 * FP registers are caller-saved. The values saved by functions such as
140 * `getcontext()` is not relevant for the caller. It is not really necessary
141 * to save and handle them.
143 static int access_fpreg(unw_addr_space_t as,
144 unw_regnum_t regnum, unw_fpreg_t *fpvalp,
145 int write, void* arg)
150 /** Resume the execution of the context (libunwind method)
154 static int resume(unw_addr_space_t as,
155 unw_cursor_t *cp, void* arg)
160 /** Find informations about a function (libunwind method)
162 static int get_proc_name(unw_addr_space_t as,
163 unw_word_t addr, char *bufp,
164 size_t buf_len, unw_word_t *offp,
167 mc_unw_context_t context = (mc_unw_context_t) arg;
168 simgrid::mc::Frame* frame = context->process->find_function(remote(addr));
170 return - UNW_ENOINFO;
171 *offp = (unw_word_t) frame->low_pc - addr;
173 strncpy(bufp, frame->name.c_str(), buf_len);
174 if (bufp[buf_len - 1]) {
175 bufp[buf_len - 1] = 0;
184 unw_accessors_t mc_unw_accessors =
188 &get_dyn_info_list_addr,
196 // ***** Context management
198 int mc_unw_init_context(
199 mc_unw_context_t context, simgrid::mc::Process* process, unw_context_t* c)
201 context->address_space = process;
202 context->process = process;
204 // Take a copy of the context for our own purpose:
205 context->context = *c;
206 #if defined(PROCESSOR_x86_64) || defined(PROCESSOR_i686)
207 // On x86_64, ucontext_t contains a pointer to itself for FP registers.
208 // We don't really need support for FR registers as they are caller saved
209 // and probably never use those fields as libunwind-x86_64 does not read
210 // FP registers from the unw_context_t
211 // but we fix the pointer in order to avoid dangling pointers:
212 context->context.uc_mcontext.fpregs = &(context->context.__fpregs_mem);
214 // Do we need to do any fixup like this?
215 #error Target CPU type is not handled.
221 // ***** Cursor management
223 int mc_unw_init_cursor(unw_cursor_t *cursor, mc_unw_context_t context)
225 if (!context->process || !context->address_space)
227 return unw_init_remote(cursor, context->process->unw_addr_space, context);