1 /* Copyright (c) 2015-2022. 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/RemoteProcess.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>
31 // ***** Implementation
33 /** Get frame unwind information (libunwind method)
35 * Delegates to the local/ptrace implementation.
37 int UnwindContext::find_proc_info(unw_addr_space_t /*as*/, unw_word_t ip, unw_proc_info_t* pip, int need_unwind_info,
40 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
41 return unw_get_accessors(context->process_->unw_underlying_addr_space)
42 ->find_proc_info(context->process_->unw_underlying_addr_space, ip, pip, need_unwind_info,
43 context->process_->unw_underlying_context);
46 /** Release frame unwind information (libunwind method)
48 * Delegates to the local/ptrace implementation.
50 void UnwindContext::put_unwind_info(unw_addr_space_t /*as*/, unw_proc_info_t* pip, void* arg) noexcept
52 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
53 return unw_get_accessors(context->process_->unw_underlying_addr_space)
54 ->put_unwind_info(context->process_->unw_underlying_addr_space, pip, context->process_->unw_underlying_context);
57 /** (libunwind method)
61 int UnwindContext::get_dyn_info_list_addr(unw_addr_space_t /*as*/, unw_word_t* dilap, void* arg) noexcept
63 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
64 return unw_get_accessors(context->process_->unw_underlying_addr_space)
65 ->get_dyn_info_list_addr(context->process_->unw_underlying_addr_space, dilap,
66 context->process_->unw_underlying_context);
69 /** Read from the target address space memory (libunwind method)
71 * Delegates to the `simgrid::mc::Process*`.
73 int UnwindContext::access_mem(unw_addr_space_t /*as*/, unw_word_t addr, unw_word_t* valp, int write, void* arg) noexcept
75 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
77 return -UNW_EREADONLYREG;
78 context->address_space_->read_bytes(valp, sizeof(unw_word_t), remote(addr));
82 void* UnwindContext::get_reg(unw_context_t* context, unw_regnum_t regnum) noexcept
85 mcontext_t* mcontext = &context->uc_mcontext;
89 return &mcontext->gregs[REG_RAX];
91 return &mcontext->gregs[REG_RDX];
93 return &mcontext->gregs[REG_RCX];
95 return &mcontext->gregs[REG_RBX];
97 return &mcontext->gregs[REG_RSI];
99 return &mcontext->gregs[REG_RDI];
101 return &mcontext->gregs[REG_RBP];
103 return &mcontext->gregs[REG_RSP];
105 return &mcontext->gregs[REG_R8];
107 return &mcontext->gregs[REG_R9];
109 return &mcontext->gregs[REG_R10];
111 return &mcontext->gregs[REG_R11];
113 return &mcontext->gregs[REG_R12];
115 return &mcontext->gregs[REG_R13];
117 return &mcontext->gregs[REG_R14];
119 return &mcontext->gregs[REG_R15];
121 return &mcontext->gregs[REG_RIP];
122 #elif defined __FreeBSD__
124 return &mcontext->mc_rax;
126 return &mcontext->mc_rdx;
128 return &mcontext->mc_rcx;
130 return &mcontext->mc_rbx;
132 return &mcontext->mc_rsi;
134 return &mcontext->mc_rdi;
136 return &mcontext->mc_rbp;
138 return &mcontext->mc_rsp;
140 return &mcontext->mc_r8;
142 return &mcontext->mc_r9;
144 return &mcontext->mc_r10;
146 return &mcontext->mc_r11;
148 return &mcontext->mc_r12;
150 return &mcontext->mc_r13;
152 return &mcontext->mc_r14;
154 return &mcontext->mc_r15;
156 return &mcontext->mc_rip;
158 #error "Unable to get register from ucontext, please add your case"
168 /** Read a standard register (libunwind method)
170 int UnwindContext::access_reg(unw_addr_space_t /*as*/, unw_regnum_t regnum, unw_word_t* valp, int write,
173 auto* as_context = static_cast<simgrid::mc::UnwindContext*>(arg);
174 unw_context_t* context = &as_context->unwind_context_;
176 return -UNW_EREADONLYREG;
177 const greg_t* preg = (greg_t*)get_reg(context, regnum);
184 /** Find information about a function (libunwind method)
186 int UnwindContext::get_proc_name(unw_addr_space_t /*as*/, unw_word_t addr, char* bufp, size_t buf_len, unw_word_t* offp,
189 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
190 const simgrid::mc::Frame* frame = context->process_->find_function(remote(addr));
193 *offp = (unw_word_t)frame->range.begin() - addr;
195 strncpy(bufp, frame->name.c_str(), buf_len);
196 if (bufp[buf_len - 1]) {
197 bufp[buf_len - 1] = 0;
206 unw_addr_space_t UnwindContext::createUnwindAddressSpace()
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 accessors = {&find_proc_info, &put_unwind_info, &get_dyn_info_list_addr, &access_mem, &access_reg,
218 nullptr, nullptr, &get_proc_name};
219 return unw_create_addr_space(&accessors, BYTE_ORDER);
222 void UnwindContext::initialize(simgrid::mc::RemoteProcess* process, unw_context_t* c)
224 this->address_space_ = process;
225 this->process_ = process;
227 // Take a copy of the context for our own purpose:
228 this->unwind_context_ = *c;
229 #if SIMGRID_PROCESSOR_x86_64 || SIMGRID_PROCESSOR_i686 || defined(__aarch64__)
231 // On x86_64, ucontext_t contains a pointer to itself for FP registers.
232 // We don't really need support for FR registers as they are caller saved
233 // and probably never use those fields as libunwind-x86_64 does not read
234 // FP registers from the unw_context_t
235 // Let's ignore this and see what happens:
236 this->unwind_context_.uc_mcontext.fpregs = nullptr;
239 // Do we need to do any fixup like this?
240 #error Target CPU type is not handled.
244 unw_cursor_t UnwindContext::cursor()
247 xbt_assert(process_ != nullptr && address_space_ != nullptr &&
248 unw_init_remote(&cursor, process_->unw_addr_space, this) == 0,
249 "UnwindContext not initialized");
254 } // namespace simgrid