Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8879bde493fe187de890c273f670f44bf25edf8f
[simgrid.git] / src / mc / mc_unw.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /** \file
8  *  Libunwind support for mc_address_space objects.
9  */
10
11 // We need this for the register indices:
12 // #define _GNU_SOURCE
13
14 #include <string.h>
15
16 // On x86_64, libunwind unw_context_t has the same layout as ucontext_t:
17 #include <sys/ucontext.h>
18
19 #include <libunwind.h>
20
21 #include "mc_object_info.h"
22 #include "src/mc/Process.hpp"
23 #include "mc_unw.h"
24 #include "src/mc/Frame.hpp"
25
26 using simgrid::mc::remote;
27
28 extern "C" {
29
30 // ***** Implementation
31
32 /** Get frame unwind information (libunwind method)
33  *
34  *  Delegates to the local/ptrace implementation.
35  */
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)
39 {
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
44   );
45 }
46
47 /** Release frame unwind information (libunwind method)
48  *
49  *  Delegates to the local/ptrace implementation.
50  */
51 static void put_unwind_info(unw_addr_space_t as,
52               unw_proc_info_t *pip, void* arg)
53 {
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
58   );
59 }
60
61 /** (libunwind method)
62  *
63  *  Not implemented.
64  */
65 static int get_dyn_info_list_addr(unw_addr_space_t as,
66               unw_word_t *dilap, void* arg)
67 {
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,
71     dilap,
72     context->process->unw_underlying_context
73   );
74 }
75
76 /** Read from the target address space memory (libunwind method)
77  *
78  *  Delegates to the `simgrid::mc::Process*`.
79  */
80 static int access_mem(unw_addr_space_t as,
81               unw_word_t addr, unw_word_t *valp,
82               int write, void* arg)
83 {
84   mc_unw_context_t context = (mc_unw_context_t) arg;
85   if (write)
86     return - UNW_EREADONLYREG;
87   context->address_space->read_bytes(valp, sizeof(unw_word_t), remote(addr));
88   return 0;
89 }
90
91 static void* get_reg(unw_context_t* context, unw_regnum_t regnum)
92 {
93 #ifdef __x86_64
94   mcontext_t* mcontext = &context->uc_mcontext;
95   switch (regnum) {
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;
114   }
115 #else
116   return NULL;
117 #endif
118 }
119
120 /** Read a standard register (libunwind method)
121  */
122 static int access_reg(unw_addr_space_t as,
123               unw_regnum_t regnum, unw_word_t *valp,
124               int write, void* arg)
125 {
126   mc_unw_context_t as_context = (mc_unw_context_t) arg;
127   unw_context_t* context = &as_context->context;
128   if (write)
129     return -UNW_EREADONLYREG;
130   greg_t* preg = (greg_t*) get_reg(context, regnum);
131   if (!preg)
132     return -UNW_EBADREG;
133   *valp = *preg;
134   return 0;
135 }
136
137 /** Read a floating-point register (libunwind method)
138  *
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.
142  */
143 static int access_fpreg(unw_addr_space_t as,
144               unw_regnum_t regnum, unw_fpreg_t *fpvalp,
145               int write, void* arg)
146 {
147   return -UNW_EBADREG;
148 }
149
150 /** Resume the execution of the context (libunwind method)
151  *
152  * We don't use this.
153  */
154 static int resume(unw_addr_space_t as,
155               unw_cursor_t *cp, void* arg)
156 {
157   return -UNW_EUNSPEC;
158 }
159
160 /** Find informations about a function (libunwind method)
161  */
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,
165               void* arg)
166 {
167   mc_unw_context_t context = (mc_unw_context_t) arg;
168   simgrid::mc::Frame* frame = context->process->find_function(remote(addr));
169   if (!frame)
170     return - UNW_ENOINFO;
171   *offp = (unw_word_t) frame->low_pc - addr;
172
173   strncpy(bufp, frame->name.c_str(), buf_len);
174   if (bufp[buf_len - 1]) {
175     bufp[buf_len - 1] = 0;
176     return -UNW_ENOMEM;
177   }
178
179   return 0;
180 }
181
182 // ***** Init
183
184 unw_accessors_t mc_unw_accessors =
185   {
186     &find_proc_info,
187     &put_unwind_info,
188     &get_dyn_info_list_addr,
189     &access_mem,
190     &access_reg,
191     &access_fpreg,
192     &resume,
193     &get_proc_name
194   };
195
196 // ***** Context management
197
198 int mc_unw_init_context(
199   mc_unw_context_t context, simgrid::mc::Process* process, unw_context_t* c)
200 {
201   context->address_space = process;
202   context->process = process;
203
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);
213 #else
214   // Do we need to do any fixup like this?
215   #error Target CPU type is not handled.
216 #endif
217
218   return 0;
219 }
220
221 // ***** Cursor management
222
223 int mc_unw_init_cursor(unw_cursor_t *cursor, mc_unw_context_t context)
224 {
225   if (!context->process || !context->address_space)
226     return -UNW_EUNSPEC;
227   return unw_init_remote(cursor, context->process->unw_addr_space, context);
228 }
229
230 }