]> AND Public Git Repository - simgrid.git/blobdiff - src/mc/mc_dwarf_expression.c
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Process nested scopes DWARF informations
[simgrid.git] / src / mc / mc_dwarf_expression.c
index a73ac2f52ae5c6310d98213da4f9577fb8ed8323..0bc9dcca0680de11653adbd020ca3da44fb0611a 100644 (file)
@@ -43,6 +43,31 @@ int mc_dwarf_execute_expression(
         break;
       }
 
+    // Push the CFA (Call 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.");
+
+      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);
+      break;
+    }
+
     // Frame base:
 
     case DW_OP_fbreg:
@@ -204,9 +229,19 @@ int mc_dwarf_execute_expression(
 
     // Dereference:
     case DW_OP_deref_size:
-    case DW_OP_deref:
       return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
 
+    case DW_OP_deref:
+      if(state->stack_size==0)
+        return MC_EXPRESSION_E_STACK_UNDERFLOW;
+      {
+        // Computed address:
+        uintptr_t address = (uintptr_t) state->stack[state->stack_size-1];
+        uintptr_t* p = (uintptr_t*)mc_translate_address(address, state->snapshot);
+        state->stack[state->stack_size-1] = *p;
+      }
+      break;
+
     // Not handled:
     default:
      return MC_EXPRESSION_E_UNSUPPORTED_OPERATION;
@@ -222,11 +257,12 @@ 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) {
+Dwarf_Off mc_dwarf_resolve_location(mc_expression_t expression, 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;
 
   if(mc_dwarf_execute_expression(expression->size, expression->ops, &state))
     xbt_die("Error evaluating DWARF expression");
@@ -236,3 +272,100 @@ Dwarf_Off mc_dwarf_resolve_location(mc_expression_t expression, unw_cursor_t* c,
     return (Dwarf_Off) 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) {
+
+  unw_word_t ip;
+  if(c) {
+    if(unw_get_reg(c, UNW_REG_IP, &ip))
+      xbt_die("Could not resolve IP");
+  }
+
+  for(size_t i=0; i!=locations->size; ++i) {
+    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);
+    }
+  }
+  xbt_die("Could not resolve location");
+}
+
+/** \brief Find the frame base of a given frame
+ *
+ *  \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_dwarf_expression_clear(mc_expression_t expression) {
+  free(expression->ops);
+  expression->ops = NULL;
+  expression->size = 0;
+  expression->lowpc = NULL;
+  expression->highpc = NULL;
+}
+
+void mc_dwarf_location_list_clear(mc_location_list_t list) {
+  for(size_t i=0; i!=list->size; ++i) {
+    mc_dwarf_expression_clear(list->locations + i);
+  }
+  free(list->locations);
+  list->locations = NULL;
+  list->size = 0;
+}
+
+void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
+  if(expression->ops) {
+    free(expression->ops);
+  }
+  expression->lowpc = NULL;
+  expression->highpc = NULL;
+  expression->size = len;
+  expression->ops = xbt_malloc(len*sizeof(Dwarf_Op));
+  memcpy(expression->ops, ops, len*sizeof(Dwarf_Op));
+}
+
+void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
+  if(target->locations) {
+    mc_dwarf_location_list_clear(target);
+  }
+  target->size = 1;
+  target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
+  mc_dwarf_expression_init(target->locations, len, ops);
+}
+
+void mc_dwarf_location_list_init(mc_location_list_t list, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
+  if(list->locations) {
+    mc_dwarf_location_list_clear(list);
+  }
+  list->size = 0;
+
+  ptrdiff_t offset = 0;
+  Dwarf_Addr base, start, end;
+  Dwarf_Op *ops;
+  size_t len;
+
+  while (1) {
+
+    offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
+    if (offset==0)
+      return;
+    else if (offset==-1)
+      xbt_die("Error while loading location list");
+
+    int i = list->size;
+    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);
+    mc_dwarf_expression_init(expression, len, ops);
+
+    // 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;
+  }
+
+}