1 /* Copyright (c) 2015-2019. 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/types.h>
18 #include <sys/ucontext.h>
20 typedef register_t greg_t;
23 #include <libunwind.h>
25 #include "src/mc/Frame.hpp"
26 #include "src/mc/mc_unw.hpp"
27 #include "src/mc/remote/RemoteClient.hpp"
29 using simgrid::mc::remote;
34 // ***** Implementation
36 /** Get frame unwind information (libunwind method)
38 * Delegates to the local/ptrace implementation.
40 int UnwindContext::find_proc_info(unw_addr_space_t /*as*/, unw_word_t ip, unw_proc_info_t* pip, int need_unwind_info,
43 simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*) arg;
44 return unw_get_accessors(context->process_->unw_underlying_addr_space)->find_proc_info(
45 context->process_->unw_underlying_addr_space, ip, pip,
46 need_unwind_info, context->process_->unw_underlying_context
50 /** Release frame unwind information (libunwind method)
52 * Delegates to the local/ptrace implementation.
54 void UnwindContext::put_unwind_info(unw_addr_space_t /*as*/, unw_proc_info_t* pip, void* arg) noexcept
56 simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*) arg;
57 return unw_get_accessors(context->process_->unw_underlying_addr_space)->put_unwind_info(
58 context->process_->unw_underlying_addr_space, pip,
59 context->process_->unw_underlying_context
63 /** (libunwind method)
67 int UnwindContext::get_dyn_info_list_addr(unw_addr_space_t /*as*/, unw_word_t* dilap, void* arg) noexcept
69 simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*) arg;
70 return unw_get_accessors(context->process_->unw_underlying_addr_space)->get_dyn_info_list_addr(
71 context->process_->unw_underlying_addr_space,
73 context->process_->unw_underlying_context
77 /** Read from the target address space memory (libunwind method)
79 * Delegates to the `simgrid::mc::Process*`.
81 int UnwindContext::access_mem(unw_addr_space_t /*as*/, unw_word_t addr, unw_word_t* valp, int write, void* arg) noexcept
83 simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*) arg;
85 return - UNW_EREADONLYREG;
86 context->addressSpace_->read_bytes(valp, sizeof(unw_word_t), remote(addr));
90 void* UnwindContext::get_reg(unw_context_t* context, unw_regnum_t regnum) noexcept
93 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 # elif defined __FreeBSD__
114 case UNW_X86_64_RAX: return &mcontext->mc_rax;
115 case UNW_X86_64_RDX: return &mcontext->mc_rdx;
116 case UNW_X86_64_RCX: return &mcontext->mc_rcx;
117 case UNW_X86_64_RBX: return &mcontext->mc_rbx;
118 case UNW_X86_64_RSI: return &mcontext->mc_rsi;
119 case UNW_X86_64_RDI: return &mcontext->mc_rdi;
120 case UNW_X86_64_RBP: return &mcontext->mc_rbp;
121 case UNW_X86_64_RSP: return &mcontext->mc_rsp;
122 case UNW_X86_64_R8: return &mcontext->mc_r8;
123 case UNW_X86_64_R9: return &mcontext->mc_r9;
124 case UNW_X86_64_R10: return &mcontext->mc_r10;
125 case UNW_X86_64_R11: return &mcontext->mc_r11;
126 case UNW_X86_64_R12: return &mcontext->mc_r12;
127 case UNW_X86_64_R13: return &mcontext->mc_r13;
128 case UNW_X86_64_R14: return &mcontext->mc_r14;
129 case UNW_X86_64_R15: return &mcontext->mc_r15;
130 case UNW_X86_64_RIP: return &mcontext->mc_rip;
132 # error "Unable to get register from ucontext, please add your case"
134 default: return nullptr;
141 /** Read a standard register (libunwind method)
143 int UnwindContext::access_reg(unw_addr_space_t /*as*/, unw_regnum_t regnum, unw_word_t* valp, int write,
146 simgrid::mc::UnwindContext* as_context = (simgrid::mc::UnwindContext*) arg;
147 unw_context_t* context = &as_context->unwindContext_;
149 return -UNW_EREADONLYREG;
150 greg_t* preg = (greg_t*) get_reg(context, regnum);
157 /** Read a floating-point register (libunwind method)
159 * FP registers are caller-saved. The values saved by functions such as
160 * `getcontext()` is not relevant for the caller. It is not really necessary
161 * to save and handle them.
163 int UnwindContext::access_fpreg(unw_addr_space_t /*as*/, unw_regnum_t /*regnum*/, unw_fpreg_t* /*fpvalp*/,
164 int /*write*/, void* /*arg*/) noexcept
169 /** Resume the execution of the context (libunwind method)
173 int UnwindContext::resume(unw_addr_space_t /*as*/, unw_cursor_t* /*cp*/, void* /*arg*/) noexcept
178 /** Find informations about a function (libunwind method)
180 int UnwindContext::get_proc_name(unw_addr_space_t /*as*/, unw_word_t addr, char* bufp, size_t buf_len, unw_word_t* offp,
183 simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*) arg;
184 simgrid::mc::Frame* frame = context->process_->find_function(remote(addr));
186 return - UNW_ENOINFO;
187 *offp = (unw_word_t) frame->range.begin() - addr;
189 strncpy(bufp, frame->name.c_str(), buf_len);
190 if (bufp[buf_len - 1]) {
191 bufp[buf_len - 1] = 0;
200 /** Virtual table for our `libunwind` implementation
202 * Stack unwinding on a `simgrid::mc::Process*` (for memory, unwinding information)
203 * and `ucontext_t` (for processor registers).
205 * It works with the `simgrid::mc::UnwindContext` context.
207 unw_accessors_t UnwindContext::accessors = {
210 &get_dyn_info_list_addr,
218 unw_addr_space_t UnwindContext::createUnwindAddressSpace()
220 return unw_create_addr_space(&UnwindContext::accessors, BYTE_ORDER);
223 void UnwindContext::initialize(simgrid::mc::RemoteClient* process, unw_context_t* c)
225 this->addressSpace_ = process;
226 this->process_ = process;
228 // Take a copy of the context for our own purpose:
229 this->unwindContext_ = *c;
230 #if SIMGRID_PROCESSOR_x86_64 || SIMGRID_PROCESSOR_i686
232 // On x86_64, ucontext_t contains a pointer to itself for FP registers.
233 // We don't really need support for FR registers as they are caller saved
234 // and probably never use those fields as libunwind-x86_64 does not read
235 // FP registers from the unw_context_t
236 // Let's ignore this and see what happens:
237 this->unwindContext_.uc_mcontext.fpregs = nullptr;
240 // Do we need to do any fixup like this?
241 # error Target CPU type is not handled.
245 unw_cursor_t UnwindContext::cursor()
248 if (process_ == nullptr
249 || addressSpace_ == nullptr
250 || unw_init_remote(&cursor, process_->unw_addr_space, this) != 0)
251 xbt_die("UnwindContext not initialized");