1 /* Copyright (c) 2015-2020. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 * Libunwind support for mc_address_space objects.
10 // We need this for the register indices:
11 // #define _GNU_SOURCE
13 #include "src/mc/inspect/mc_unw.hpp"
14 #include "src/mc/inspect/Frame.hpp"
15 #include "src/mc/remote/RemoteSimulation.hpp"
19 // On x86_64, libunwind unw_context_t has the same layout as ucontext_t:
20 #include <sys/types.h>
21 #include <sys/ucontext.h>
23 typedef register_t greg_t;
26 #include <libunwind.h>
28 using simgrid::mc::remote;
33 // ***** Implementation
35 /** Get frame unwind information (libunwind method)
37 * Delegates to the local/ptrace implementation.
39 int UnwindContext::find_proc_info(unw_addr_space_t /*as*/, unw_word_t ip, unw_proc_info_t* pip, int need_unwind_info,
42 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
43 return unw_get_accessors(context->process_->unw_underlying_addr_space)
44 ->find_proc_info(context->process_->unw_underlying_addr_space, ip, pip, need_unwind_info,
45 context->process_->unw_underlying_context);
48 /** Release frame unwind information (libunwind method)
50 * Delegates to the local/ptrace implementation.
52 void UnwindContext::put_unwind_info(unw_addr_space_t /*as*/, unw_proc_info_t* pip, void* arg) noexcept
54 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
55 return unw_get_accessors(context->process_->unw_underlying_addr_space)
56 ->put_unwind_info(context->process_->unw_underlying_addr_space, pip, context->process_->unw_underlying_context);
59 /** (libunwind method)
63 int UnwindContext::get_dyn_info_list_addr(unw_addr_space_t /*as*/, unw_word_t* dilap, void* arg) noexcept
65 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
66 return unw_get_accessors(context->process_->unw_underlying_addr_space)
67 ->get_dyn_info_list_addr(context->process_->unw_underlying_addr_space, dilap,
68 context->process_->unw_underlying_context);
71 /** Read from the target address space memory (libunwind method)
73 * Delegates to the `simgrid::mc::Process*`.
75 int UnwindContext::access_mem(unw_addr_space_t /*as*/, unw_word_t addr, unw_word_t* valp, int write, void* arg) noexcept
77 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
79 return -UNW_EREADONLYREG;
80 context->address_space_->read_bytes(valp, sizeof(unw_word_t), remote(addr));
84 void* UnwindContext::get_reg(unw_context_t* context, unw_regnum_t regnum) noexcept
87 mcontext_t* mcontext = &context->uc_mcontext;
91 return &mcontext->gregs[REG_RAX];
93 return &mcontext->gregs[REG_RDX];
95 return &mcontext->gregs[REG_RCX];
97 return &mcontext->gregs[REG_RBX];
99 return &mcontext->gregs[REG_RSI];
101 return &mcontext->gregs[REG_RDI];
103 return &mcontext->gregs[REG_RBP];
105 return &mcontext->gregs[REG_RSP];
107 return &mcontext->gregs[REG_R8];
109 return &mcontext->gregs[REG_R9];
111 return &mcontext->gregs[REG_R10];
113 return &mcontext->gregs[REG_R11];
115 return &mcontext->gregs[REG_R12];
117 return &mcontext->gregs[REG_R13];
119 return &mcontext->gregs[REG_R14];
121 return &mcontext->gregs[REG_R15];
123 return &mcontext->gregs[REG_RIP];
124 #elif defined __FreeBSD__
126 return &mcontext->mc_rax;
128 return &mcontext->mc_rdx;
130 return &mcontext->mc_rcx;
132 return &mcontext->mc_rbx;
134 return &mcontext->mc_rsi;
136 return &mcontext->mc_rdi;
138 return &mcontext->mc_rbp;
140 return &mcontext->mc_rsp;
142 return &mcontext->mc_r8;
144 return &mcontext->mc_r9;
146 return &mcontext->mc_r10;
148 return &mcontext->mc_r11;
150 return &mcontext->mc_r12;
152 return &mcontext->mc_r13;
154 return &mcontext->mc_r14;
156 return &mcontext->mc_r15;
158 return &mcontext->mc_rip;
160 #error "Unable to get register from ucontext, please add your case"
170 /** Read a standard register (libunwind method)
172 int UnwindContext::access_reg(unw_addr_space_t /*as*/, unw_regnum_t regnum, unw_word_t* valp, int write,
175 auto* as_context = static_cast<simgrid::mc::UnwindContext*>(arg);
176 unw_context_t* context = &as_context->unwind_context_;
178 return -UNW_EREADONLYREG;
179 const greg_t* preg = (greg_t*)get_reg(context, regnum);
186 /** Find information about a function (libunwind method)
188 int UnwindContext::get_proc_name(unw_addr_space_t /*as*/, unw_word_t addr, char* bufp, size_t buf_len, unw_word_t* offp,
191 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
192 const simgrid::mc::Frame* frame = context->process_->find_function(remote(addr));
195 *offp = (unw_word_t)frame->range.begin() - addr;
197 strncpy(bufp, frame->name.c_str(), buf_len);
198 if (bufp[buf_len - 1]) {
199 bufp[buf_len - 1] = 0;
208 /** Virtual table for our `libunwind` implementation
210 * Stack unwinding on a `simgrid::mc::Process*` (for memory, unwinding information)
211 * and `ucontext_t` (for processor registers).
213 * It works with the `simgrid::mc::UnwindContext` context.
215 * Use nullptr as access_fpreg and resume, as we don't need them.
217 unw_accessors_t UnwindContext::accessors = {&find_proc_info, &put_unwind_info, &get_dyn_info_list_addr,
218 &access_mem, &access_reg, nullptr,
219 nullptr, &get_proc_name};
221 unw_addr_space_t UnwindContext::createUnwindAddressSpace()
223 return unw_create_addr_space(&UnwindContext::accessors, BYTE_ORDER);
226 void UnwindContext::initialize(simgrid::mc::RemoteSimulation* process, unw_context_t* c)
228 this->address_space_ = process;
229 this->process_ = process;
231 // Take a copy of the context for our own purpose:
232 this->unwind_context_ = *c;
233 #if SIMGRID_PROCESSOR_x86_64 || SIMGRID_PROCESSOR_i686
235 // On x86_64, ucontext_t contains a pointer to itself for FP registers.
236 // We don't really need support for FR registers as they are caller saved
237 // and probably never use those fields as libunwind-x86_64 does not read
238 // FP registers from the unw_context_t
239 // Let's ignore this and see what happens:
240 this->unwind_context_.uc_mcontext.fpregs = nullptr;
243 // Do we need to do any fixup like this?
244 #error Target CPU type is not handled.
248 unw_cursor_t UnwindContext::cursor()
251 xbt_assert(process_ != nullptr && address_space_ != nullptr &&
252 unw_init_remote(&cursor, process_->unw_addr_space, this) == 0,
253 "UnwindContext not initialized");
258 } // namespace simgrid