Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Initialize expression->ops before calling mc_dwarf_expression_init.
[simgrid.git] / src / mc / mc_dwarf_expression.c
index b45645e..f143a37 100644 (file)
@@ -1,3 +1,8 @@
+/* Copyright (c) 2014. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include <stdint.h>
 #include <stdarg.h>
@@ -43,28 +48,23 @@ int mc_dwarf_execute_expression(
         break;
       }
 
-    // Push the CFA (Call Frame Addresse):
+    // Push the CFA (Canonical Frame Addresse):
     case DW_OP_call_frame_cfa:
     {
-      unw_word_t res;
-
-      int register_id =
-#if defined(UNW_TARGET_X86_64)
-          UNW_X86_64_CFA
-#elif defined(UNW_TARGET_X86)
-          UNW_X86_CFA
-#else
-          -1;
-#endif
-        ;
-      if(register_id<0)
-        xbt_die("Support for CFA not implemented for this achitecture.");
+      // UNW_X86_64_CFA does not return the CFA DWARF expects
+      // (it is a synonym for UNW_X86_64_RSP) so copy the cursor,
+      // unwind it once in order to find the parent SP:
 
       if(!state->cursor)
         return MC_EXPRESSION_E_MISSING_STACK_CONTEXT;
 
-      unw_get_reg(state->cursor, register_id, &res);
-      error = mc_dwarf_push_value(state, res + op->number);
+      // Get frame:
+      unw_cursor_t cursor = *(state->cursor);
+      unw_step(&cursor);
+
+      unw_word_t res;
+      unw_get_reg(&cursor, UNW_TDEP_SP, &res);
+      error = mc_dwarf_push_value(state, res);
       break;
     }
 
@@ -78,6 +78,7 @@ int mc_dwarf_execute_expression(
         break;
       }
 
+
     // Constants:
 
     case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3:
@@ -92,6 +93,14 @@ int mc_dwarf_execute_expression(
       break;
 
     case DW_OP_addr:
+      if(!state->object_info)
+        return MC_EXPRESSION_E_NO_BASE_ADDRESS;
+      if(state->stack_size==MC_EXPRESSION_STACK_SIZE)
+        return MC_EXPRESSION_E_STACK_OVERFLOW;
+      error = mc_dwarf_push_value(state,
+        (Dwarf_Off)(uintptr_t)MC_object_base_address(state->object_info) + op->number);
+      break;
+
     case DW_OP_const1u:
     case DW_OP_const2u:
     case DW_OP_const4u:
@@ -257,22 +266,23 @@ int mc_dwarf_execute_expression(
 /** \brief Resolve a location expression
  *  \deprecated Use mc_dwarf_resolve_expression
  */
-Dwarf_Off mc_dwarf_resolve_location(mc_expression_t expression, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
+uintptr_t mc_dwarf_resolve_location(mc_expression_t expression, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
   s_mc_expression_state_t state;
   memset(&state, 0, sizeof(s_mc_expression_state_t));
   state.frame_base = frame_pointer_address;
   state.cursor = c;
   state.snapshot = snapshot;
+  state.object_info = object_info;
 
   if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
     xbt_die("Error evaluating DWARF expression");
   if(state.stack_size==0)
     xbt_die("No value on the stack");
   else
-    return (Dwarf_Off) state.stack[state.stack_size-1];
+    return state.stack[state.stack_size-1];
 }
 
-Dwarf_Off mc_dwarf_resolve_locations(mc_location_list_t locations, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
+uintptr_t mc_dwarf_resolve_locations(mc_location_list_t locations, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot) {
 
   unw_word_t ip;
   if(c) {
@@ -284,7 +294,7 @@ Dwarf_Off mc_dwarf_resolve_locations(mc_location_list_t locations, unw_cursor_t*
     mc_expression_t expression = locations->locations + i;
     if( (expression->lowpc==NULL && expression->highpc==NULL)
       || (c && ip >= (unw_word_t) expression->lowpc && ip < (unw_word_t) expression->highpc)) {
-      return mc_dwarf_resolve_location(expression, c, frame_pointer_address, snapshot);
+      return mc_dwarf_resolve_location(expression, object_info, c, frame_pointer_address, snapshot);
     }
   }
   xbt_die("Could not resolve location");
@@ -295,11 +305,10 @@ Dwarf_Off mc_dwarf_resolve_locations(mc_location_list_t locations, unw_cursor_t*
  *  \param frame
  *  \param unw_cursor
  */
-void* mc_find_frame_base(dw_frame_t frame, unw_cursor_t* unw_cursor) {
-  return (void*) mc_dwarf_resolve_locations(&frame->frame_base, unw_cursor, NULL, NULL);
+void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor) {
+  return (void*) mc_dwarf_resolve_locations(&frame->frame_base, object_info, unw_cursor, NULL, NULL);
 }
 
-static
 void mc_dwarf_expression_clear(mc_expression_t expression) {
   free(expression->ops);
   expression->ops = NULL;
@@ -317,7 +326,6 @@ void mc_dwarf_location_list_clear(mc_location_list_t list) {
   list->size = 0;
 }
 
-static
 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
   if(expression->ops) {
     free(expression->ops);
@@ -361,10 +369,10 @@ void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info,
     list->size++;
     list->locations = (mc_expression_t) realloc(list->locations, list->size*sizeof(s_mc_expression_t));
     mc_expression_t expression = list->locations + i;
-
-    void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
+    expression->ops = NULL;
     mc_dwarf_expression_init(expression, len, ops);
 
+    void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
     // If start == 0, this is not a location list:
     expression->lowpc = start == 0 ? NULL : (char*) base + start;
     expression->highpc = start == 0 ? NULL : (char*) base + end;