1 /* Copyright (c) 2014. 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. */
11 #include <elfutils/libdw.h>
13 #include "mc_object_info.h"
14 #include "mc_private.h"
16 using simgrid::mc::remote;
20 static int mc_dwarf_push_value(mc_expression_state_t state, Dwarf_Off value)
22 if (state->stack_size >= MC_EXPRESSION_STACK_SIZE)
23 return MC_EXPRESSION_E_STACK_OVERFLOW;
25 state->stack[state->stack_size++] = value;
29 /** Convert a DWARF register into a libunwind register
31 * DWARF and libunwind does not use the same convention for numbering the
32 * registers on some architectures. The function makes the necessary
35 static int mc_dwarf_register_to_libunwind(int dwarf_register)
37 #if defined(UNW_TARGET_X86_64)
38 // It seems for this arch, DWARF and libunwind agree in the numbering:
39 return dwarf_register;
40 #elif defined(UNW_TARGET_X86)
41 // Could't find the authoritative source of information for this.
42 // This is inspired from http://source.winehq.org/source/dlls/dbghelp/cpu_i386.c#L517.
43 switch (dwarf_register) {
63 return UNW_X86_EFLAGS;
93 xbt_die("Bad/unknown register number.");
96 #error This architecture is not supported yet for DWARF expression evaluation.
100 int mc_dwarf_execute_expression(size_t n, const Dwarf_Op * ops,
101 mc_expression_state_t state)
103 for (size_t i = 0; i != n; ++i) {
105 const Dwarf_Op *op = ops + i;
106 uint8_t atom = op->atom;
145 mc_dwarf_register_to_libunwind(op->atom - DW_OP_breg0);
148 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
149 unw_get_reg(state->cursor, register_id, &res);
150 error = mc_dwarf_push_value(state, res + op->number);
154 // Push the CFA (Canonical Frame Addresse):
155 case DW_OP_call_frame_cfa:
157 // UNW_X86_64_CFA does not return the CFA DWARF expects
158 // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
159 // unwind it once in order to find the parent SP:
162 return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
165 unw_cursor_t cursor = *(state->cursor);
169 unw_get_reg(&cursor, UNW_TDEP_SP, &res);
170 error = mc_dwarf_push_value(state, res);
178 if (!state->frame_base)
179 return MC_EXPRESSION_E_MISSING_FRAME_BASE;
180 uintptr_t fb = ((uintptr_t) state->frame_base) + op->number;
181 error = mc_dwarf_push_value(state, fb);
188 // Short constant literals:
189 // DW_OP_lit15 pushed the 15 on the stack.
222 error = mc_dwarf_push_value(state, atom - DW_OP_lit0);
225 // Address from the base address of this ELF object.
226 // Push the address on the stack (base_address + argument).
228 if (!state->object_info)
229 return MC_EXPRESSION_E_NO_BASE_ADDRESS;
230 if (state->stack_size == MC_EXPRESSION_STACK_SIZE)
231 return MC_EXPRESSION_E_STACK_OVERFLOW;
232 Dwarf_Off addr = (Dwarf_Off) (uintptr_t)
233 state->object_info->base_address() + op->number;
234 error = mc_dwarf_push_value(state, addr);
238 // General constants:
239 // Push the constant argument on the stack.
250 if (state->stack_size == MC_EXPRESSION_STACK_SIZE)
251 return MC_EXPRESSION_E_STACK_OVERFLOW;
252 error = mc_dwarf_push_value(state, op->number);
255 // ***** Stack manipulation:
257 // Push another copy/duplicate the value at the top of the stack:
259 if (state->stack_size == 0)
260 return MC_EXPRESSION_E_STACK_UNDERFLOW;
262 error = mc_dwarf_push_value(state, state->stack[state->stack_size - 1]);
265 // Pop/drop the top of the stack:
267 if (state->stack_size == 0)
268 return MC_EXPRESSION_E_STACK_UNDERFLOW;
273 // Swap the two top-most value of the stack:
275 if (state->stack_size < 2)
276 return MC_EXPRESSION_E_STACK_UNDERFLOW;
278 uintptr_t temp = state->stack[state->stack_size - 2];
279 state->stack[state->stack_size - 2] =
280 state->stack[state->stack_size - 1];
281 state->stack[state->stack_size - 1] = temp;
285 // Duplicate the value under the top of the stack:
287 if (state->stack_size < 2)
288 return MC_EXPRESSION_E_STACK_UNDERFLOW;
289 error = mc_dwarf_push_value(state, state->stack[state->stack_size - 2]);
293 // Those usually take the top of the stack and the next value as argument
294 // and replace the top of the stack with the computed value
295 // (stack.top() += stack.before_top()).
298 if (state->stack_size < 2)
299 return MC_EXPRESSION_E_STACK_UNDERFLOW;
302 state->stack[state->stack_size - 2] +
303 state->stack[state->stack_size - 1];
304 state->stack[state->stack_size - 2] = result;
310 if (state->stack_size < 2)
311 return MC_EXPRESSION_E_STACK_UNDERFLOW;
314 state->stack[state->stack_size - 2] -
315 state->stack[state->stack_size - 1];
316 state->stack[state->stack_size - 2] = result;
321 case DW_OP_plus_uconst:
322 if (state->stack_size == 0)
323 return MC_EXPRESSION_E_STACK_UNDERFLOW;
324 state->stack[state->stack_size - 1] += op->number;
328 if (state->stack_size == 0)
329 return MC_EXPRESSION_E_STACK_UNDERFLOW;
330 state->stack[state->stack_size - 1] =
331 ~state->stack[state->stack_size - 1];
335 if (state->stack_size == 0)
336 return MC_EXPRESSION_E_STACK_UNDERFLOW;
338 intptr_t value = state->stack[state->stack_size - 1];
341 state->stack[state->stack_size - 1] = value;
346 if (state->stack_size < 2)
347 return MC_EXPRESSION_E_STACK_UNDERFLOW;
350 state->stack[state->stack_size - 2] -
351 state->stack[state->stack_size - 1];
352 state->stack[state->stack_size - 2] = result;
358 if (state->stack_size < 2)
359 return MC_EXPRESSION_E_STACK_UNDERFLOW;
362 state->stack[state->stack_size -
363 2] & state->stack[state->stack_size - 1];
364 state->stack[state->stack_size - 2] = result;
370 if (state->stack_size < 2)
371 return MC_EXPRESSION_E_STACK_UNDERFLOW;
374 state->stack[state->stack_size -
375 2] | state->stack[state->stack_size - 1];
376 state->stack[state->stack_size - 2] = result;
382 if (state->stack_size < 2)
383 return MC_EXPRESSION_E_STACK_UNDERFLOW;
386 state->stack[state->stack_size -
387 2] ^ state->stack[state->stack_size - 1];
388 state->stack[state->stack_size - 2] = result;
396 // ***** Deference (memory fetch)
398 case DW_OP_deref_size:
399 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
402 if (state->stack_size == 0)
403 return MC_EXPRESSION_E_STACK_UNDERFLOW;
406 uintptr_t address = (uintptr_t) state->stack[state->stack_size - 1];
407 if (!state->address_space)
408 xbt_die("Missing address space");
409 state->address_space->read_bytes(
410 &state->stack[state->stack_size - 1], sizeof(uintptr_t),
411 remote(address), state->process_index);
417 return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
428 /** \brief Resolve a location expression
429 * \deprecated Use mc_dwarf_resolve_expression
431 void mc_dwarf_resolve_location(mc_location_t location,
432 simgrid::mc::DwarfExpression* expression,
433 simgrid::mc::ObjectInformation* object_info,
435 void *frame_pointer_address,
436 simgrid::mc::AddressSpace* address_space, int process_index)
438 s_mc_expression_state_t state;
439 memset(&state, 0, sizeof(s_mc_expression_state_t));
440 state.frame_base = frame_pointer_address;
442 state.address_space = address_space;
443 state.object_info = object_info;
444 state.process_index = process_index;
446 if (expression->size() >= 1
447 && (*expression)[0].atom >=DW_OP_reg0
448 && (*expression)[0].atom <= DW_OP_reg31) {
449 int dwarf_register = (*expression)[0].atom - DW_OP_reg0;
451 "Missing frame context for register operation DW_OP_reg%i",
453 location->memory_location = NULL;
454 location->cursor = c;
455 location->register_id = mc_dwarf_register_to_libunwind(dwarf_register);
459 if (mc_dwarf_execute_expression(
460 expression->size(), expression->data(), &state))
461 xbt_die("Error evaluating DWARF expression");
462 if (state.stack_size == 0)
463 xbt_die("No value on the stack");
465 location->memory_location = (void*) state.stack[state.stack_size - 1];
466 location->cursor = NULL;
467 location->register_id = 0;
471 // TODO, move this in a method of LocationList
472 static simgrid::mc::DwarfExpression* mc_find_expression(
473 simgrid::mc::LocationList* locations, unw_word_t ip)
475 for (simgrid::mc::LocationListEntry& entry : *locations)
476 if (entry.valid_for_ip(ip))
477 return &entry.expression;
481 void mc_dwarf_resolve_locations(mc_location_t location,
482 simgrid::mc::LocationList* locations,
483 simgrid::mc::ObjectInformation* object_info,
485 void *frame_pointer_address,
486 simgrid::mc::AddressSpace* address_space,
492 if (unw_get_reg(c, UNW_REG_IP, &ip))
493 xbt_die("Could not resolve IP");
496 simgrid::mc::DwarfExpression* expression = mc_find_expression(locations, ip);
498 mc_dwarf_resolve_location(location,
499 expression, object_info, c,
500 frame_pointer_address, address_space, process_index);
502 xbt_die("Could not resolve location");
506 /** \brief Find the frame base of a given frame
511 void *mc_find_frame_base(simgrid::mc::Frame* frame, simgrid::mc::ObjectInformation* object_info,
512 unw_cursor_t * unw_cursor)
514 s_mc_location_t location;
515 mc_dwarf_resolve_locations(&location,
516 &frame->frame_base, object_info,
517 unw_cursor, NULL, NULL, -1);
518 switch(mc_get_location_type(&location)) {
519 case MC_LOCATION_TYPE_ADDRESS:
520 return location.memory_location;
522 case MC_LOCATION_TYPE_REGISTER: {
523 // This is a special case.
524 // The register if not the location of the frame base
525 // (a frame base cannot be located in a register)
526 // Instead, DWARF defines this to mean that the register
527 // contains the address of the frame base.
529 unw_get_reg(location.cursor, location.register_id, &word);
534 xbt_die("Cannot handle non-address frame base");
535 return NULL; // Unreachable
539 void mc_dwarf_location_list_init(
540 simgrid::mc::LocationList* list, simgrid::mc::ObjectInformation* info,
541 Dwarf_Die * die, Dwarf_Attribute * attr)
545 ptrdiff_t offset = 0;
546 Dwarf_Addr base, start, end;
552 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
555 else if (offset == -1)
556 xbt_die("Error while loading location list");
558 simgrid::mc::LocationListEntry entry;
559 entry.expression = simgrid::mc::DwarfExpression(ops, ops + len);
561 void *base = info->base_address();
562 // If start == 0, this is not a location list:
563 entry.lowpc = start == 0 ? NULL : (char *) base + start;
564 entry.highpc = start == 0 ? NULL : (char *) base + end;
566 list->push_back(std::move(entry));