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>
28 namespace simgrid::mc {
30 // ***** Implementation
32 /** Get frame unwind information (libunwind method)
34 * Delegates to the local/ptrace implementation.
36 int UnwindContext::find_proc_info(unw_addr_space_t /*as*/, unw_word_t ip, unw_proc_info_t* pip, int need_unwind_info,
39 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
40 return unw_get_accessors(context->process_->unw_underlying_addr_space)
41 ->find_proc_info(context->process_->unw_underlying_addr_space, ip, pip, need_unwind_info,
42 context->process_->unw_underlying_context);
45 /** Release frame unwind information (libunwind method)
47 * Delegates to the local/ptrace implementation.
49 void UnwindContext::put_unwind_info(unw_addr_space_t /*as*/, unw_proc_info_t* pip, void* arg) noexcept
51 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
52 return unw_get_accessors(context->process_->unw_underlying_addr_space)
53 ->put_unwind_info(context->process_->unw_underlying_addr_space, pip, context->process_->unw_underlying_context);
56 /** (libunwind method)
60 int UnwindContext::get_dyn_info_list_addr(unw_addr_space_t /*as*/, unw_word_t* dilap, void* arg) noexcept
62 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
63 return unw_get_accessors(context->process_->unw_underlying_addr_space)
64 ->get_dyn_info_list_addr(context->process_->unw_underlying_addr_space, dilap,
65 context->process_->unw_underlying_context);
68 /** Read from the target address space memory (libunwind method)
70 * Delegates to the `simgrid::mc::Process*`.
72 int UnwindContext::access_mem(unw_addr_space_t /*as*/, unw_word_t addr, unw_word_t* valp, int write, void* arg) noexcept
74 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
76 return -UNW_EREADONLYREG;
77 context->address_space_->read_bytes(valp, sizeof(unw_word_t), remote(addr));
81 void* UnwindContext::get_reg(unw_context_t* context, unw_regnum_t regnum) noexcept
84 mcontext_t* mcontext = &context->uc_mcontext;
88 return &mcontext->gregs[REG_RAX];
90 return &mcontext->gregs[REG_RDX];
92 return &mcontext->gregs[REG_RCX];
94 return &mcontext->gregs[REG_RBX];
96 return &mcontext->gregs[REG_RSI];
98 return &mcontext->gregs[REG_RDI];
100 return &mcontext->gregs[REG_RBP];
102 return &mcontext->gregs[REG_RSP];
104 return &mcontext->gregs[REG_R8];
106 return &mcontext->gregs[REG_R9];
108 return &mcontext->gregs[REG_R10];
110 return &mcontext->gregs[REG_R11];
112 return &mcontext->gregs[REG_R12];
114 return &mcontext->gregs[REG_R13];
116 return &mcontext->gregs[REG_R14];
118 return &mcontext->gregs[REG_R15];
120 return &mcontext->gregs[REG_RIP];
121 #elif defined __FreeBSD__
123 return &mcontext->mc_rax;
125 return &mcontext->mc_rdx;
127 return &mcontext->mc_rcx;
129 return &mcontext->mc_rbx;
131 return &mcontext->mc_rsi;
133 return &mcontext->mc_rdi;
135 return &mcontext->mc_rbp;
137 return &mcontext->mc_rsp;
139 return &mcontext->mc_r8;
141 return &mcontext->mc_r9;
143 return &mcontext->mc_r10;
145 return &mcontext->mc_r11;
147 return &mcontext->mc_r12;
149 return &mcontext->mc_r13;
151 return &mcontext->mc_r14;
153 return &mcontext->mc_r15;
155 return &mcontext->mc_rip;
157 #error "Unable to get register from ucontext, please add your case"
167 /** Read a standard register (libunwind method)
169 int UnwindContext::access_reg(unw_addr_space_t /*as*/, unw_regnum_t regnum, unw_word_t* valp, int write,
172 auto* as_context = static_cast<simgrid::mc::UnwindContext*>(arg);
173 unw_context_t* context = &as_context->unwind_context_;
175 return -UNW_EREADONLYREG;
176 const greg_t* preg = (greg_t*)get_reg(context, regnum);
183 /** Find information about a function (libunwind method)
185 int UnwindContext::get_proc_name(unw_addr_space_t /*as*/, unw_word_t addr, char* bufp, size_t buf_len, unw_word_t* offp,
188 const simgrid::mc::UnwindContext* context = (simgrid::mc::UnwindContext*)arg;
189 const simgrid::mc::Frame* frame = context->process_->find_function(remote(addr));
192 *offp = (unw_word_t)frame->range.begin() - addr;
194 strncpy(bufp, frame->name.c_str(), buf_len);
195 if (bufp[buf_len - 1]) {
196 bufp[buf_len - 1] = 0;
205 unw_addr_space_t UnwindContext::createUnwindAddressSpace()
207 /** Virtual table for our `libunwind` implementation
209 * Stack unwinding on a `simgrid::mc::Process*` (for memory, unwinding information)
210 * and `ucontext_t` (for processor registers).
212 * It works with the `simgrid::mc::UnwindContext` context.
214 * Use nullptr as access_fpreg and resume, as we don't need them.
216 unw_accessors_t accessors = {&find_proc_info, &put_unwind_info, &get_dyn_info_list_addr, &access_mem, &access_reg,
217 nullptr, nullptr, &get_proc_name};
218 return unw_create_addr_space(&accessors, BYTE_ORDER);
221 void UnwindContext::initialize(simgrid::mc::RemoteProcess* process, unw_context_t* c)
223 this->address_space_ = process;
224 this->process_ = process;
226 // Take a copy of the context for our own purpose:
227 this->unwind_context_ = *c;
228 #if SIMGRID_PROCESSOR_x86_64 || SIMGRID_PROCESSOR_i686 || defined(__aarch64__)
230 // On x86_64, ucontext_t contains a pointer to itself for FP registers.
231 // We don't really need support for FR registers as they are caller saved
232 // and probably never use those fields as libunwind-x86_64 does not read
233 // FP registers from the unw_context_t
234 // Let's ignore this and see what happens:
235 this->unwind_context_.uc_mcontext.fpregs = nullptr;
238 // Do we need to do any fixup like this?
239 #error Target CPU type is not handled.
243 unw_cursor_t UnwindContext::cursor()
246 xbt_assert(process_ != nullptr && address_space_ != nullptr &&
247 unw_init_remote(&cursor, process_->unw_addr_space, this) == 0,
248 "UnwindContext not initialized");
252 } // namespace simgrid::mc