f096bf
commit 434c7524413a8a47ae40e4b141f5821eabc506b7
f096bf
Author: iraisr <iraisr@a5019735-40e9-0310-863c-91ae7b9d1cf9>
f096bf
Date:   Fri Dec 4 13:14:10 2015 +0000
f096bf
f096bf
    Dwarf line info reader now correctly interprets 'is_stmt' register
f096bf
    
f096bf
    Line numbers should correctly reflect all instructions belonging to a source line,
f096bf
    regardless of is_stmt value. Previously only instructions covered by
f096bf
    'is_stmt = 1' were attributed to a source line.
f096bf
    
f096bf
    Fixes BZ#356044
f096bf
    
f096bf
    
f096bf
    git-svn-id: svn://svn.valgrind.org/valgrind/trunk@15741 a5019735-40e9-0310-863c-91ae7b9d1cf9
f096bf
f096bf
diff --git a/coregrind/m_debuginfo/readdwarf.c b/coregrind/m_debuginfo/readdwarf.c
f096bf
index 88d49e9..a95bb3d 100644
f096bf
--- a/coregrind/m_debuginfo/readdwarf.c
f096bf
+++ b/coregrind/m_debuginfo/readdwarf.c
f096bf
@@ -91,7 +91,6 @@ typedef struct
f096bf
   ULong  li_header_length;
f096bf
   UChar  li_min_insn_length;
f096bf
   UChar  li_max_ops_per_insn;
f096bf
-  UChar  li_default_is_stmt;
f096bf
   Int    li_line_base;
f096bf
   UChar  li_line_range;
f096bf
   UChar  li_opcode_base;
f096bf
@@ -150,7 +149,6 @@ typedef struct
f096bf
   UInt  file;
f096bf
   UInt  line;
f096bf
   UInt  column;
f096bf
-  Int   is_stmt;
f096bf
   Int   basic_block;
f096bf
   UChar end_sequence;
f096bf
 } LineSMR;
f096bf
@@ -230,7 +228,7 @@ ULong read_initial_length_field ( DiCursor p_img, /*OUT*/Bool* is64 )
f096bf
 static LineSMR state_machine_regs;
f096bf
 
f096bf
 static 
f096bf
-void reset_state_machine ( Int is_stmt )
f096bf
+void reset_state_machine ( void )
f096bf
 {
f096bf
    if (0) VG_(printf)("smr.a := %p (reset)\n", NULL );
f096bf
    state_machine_regs.last_address = 0;
f096bf
@@ -240,7 +238,6 @@ void reset_state_machine ( Int is_stmt )
f096bf
    state_machine_regs.file = 1;
f096bf
    state_machine_regs.line = 1;
f096bf
    state_machine_regs.column = 0;
f096bf
-   state_machine_regs.is_stmt = is_stmt;
f096bf
    state_machine_regs.basic_block = 0;
f096bf
    state_machine_regs.end_sequence = 0;
f096bf
 }
f096bf
@@ -253,7 +250,7 @@ void reset_state_machine ( Int is_stmt )
f096bf
 static 
f096bf
 void process_extended_line_op( struct _DebugInfo* di,
f096bf
                                XArray* fndn_ix_xa,
f096bf
-                               DiCursor* data, Int is_stmt)
f096bf
+                               DiCursor* data )
f096bf
 {
f096bf
    UInt len = step_leb128U(data);
f096bf
    if (len == 0) {
f096bf
@@ -275,19 +272,17 @@ void process_extended_line_op( struct _DebugInfo* di,
f096bf
             reset_state_machine below */
f096bf
          state_machine_regs.end_sequence = 1; 
f096bf
 
f096bf
-         if (state_machine_regs.is_stmt) {
f096bf
-            if (state_machine_regs.last_address) {
f096bf
-               ML_(addLineInfo) (
f096bf
-                  di,
f096bf
-                  safe_fndn_ix (fndn_ix_xa,
f096bf
-                                state_machine_regs.last_file),
f096bf
-                  di->text_debug_bias + state_machine_regs.last_address, 
f096bf
-                  di->text_debug_bias + state_machine_regs.address, 
f096bf
-                  state_machine_regs.last_line, 0
f096bf
-               );
f096bf
-            }
f096bf
+         if (state_machine_regs.last_address) {
f096bf
+            ML_(addLineInfo)(
f096bf
+               di,
f096bf
+               safe_fndn_ix(fndn_ix_xa,
f096bf
+                            state_machine_regs.last_file),
f096bf
+               di->text_debug_bias + state_machine_regs.last_address, 
f096bf
+               di->text_debug_bias + state_machine_regs.address, 
f096bf
+               state_machine_regs.last_line, 0
f096bf
+            );
f096bf
          }
f096bf
-         reset_state_machine (is_stmt);
f096bf
+         reset_state_machine();
f096bf
          if (di->ddump_line)
f096bf
             VG_(printf)("  Extended opcode %d: End of Sequence\n\n", 
f096bf
                         (Int)op_code);
f096bf
@@ -446,29 +441,9 @@ void read_dwarf2_lineblock ( struct _DebugInfo* di,
f096bf
       info.li_max_ops_per_insn = 1;
f096bf
    }
f096bf
 
f096bf
-   info.li_default_is_stmt = ML_(cur_step_UChar)(&external);
f096bf
-   if (di->ddump_line)
f096bf
-      VG_(printf)("  Initial value of 'is_stmt':  %d\n", 
f096bf
-                  (Int)info.li_default_is_stmt);
f096bf
-
f096bf
-   /* Josef Weidendorfer (20021021) writes:
f096bf
-
f096bf
-      It seems to me that the Intel Fortran compiler generates bad
f096bf
-      DWARF2 line info code: It sets "is_stmt" of the state machine in
f096bf
-      the line info reader to be always false. Thus, there is never
f096bf
-      a statement boundary generated and therefore never an instruction
f096bf
-      range/line number mapping generated for valgrind.
f096bf
-
f096bf
-      Please have a look at the DWARF2 specification, Ch. 6.2
f096bf
-      (x86.ddj.com/ftp/manuals/tools/dwarf.pdf).  Perhaps I understand
f096bf
-      this wrong, but I don't think so.
f096bf
-
f096bf
-      I just had a look at the GDB DWARF2 reader...  They completely
f096bf
-      ignore "is_stmt" when recording line info ;-) That's the reason
f096bf
-      "objdump -S" works on files from the intel fortran compiler.
f096bf
-
f096bf
-      Therefore: */
f096bf
-   info.li_default_is_stmt = True; 
f096bf
+   /* Register is_stmt is not tracked as we are interested only
f096bf
+      in pc -> line info mapping and not other debugger features. */
f096bf
+   /* default_is_stmt = */ ML_(cur_step_UChar)(&external);
f096bf
 
f096bf
    /* JRS: changed (UInt*) to (UChar*) */
f096bf
    info.li_line_base = ML_(cur_step_UChar)(&external);
f096bf
@@ -495,7 +470,7 @@ void read_dwarf2_lineblock ( struct _DebugInfo* di,
f096bf
    DiCursor end_of_sequence
f096bf
      = ML_(cur_plus)(data, info.li_length + (is64 ? 12 : 4));
f096bf
 
f096bf
-   reset_state_machine (info.li_default_is_stmt);
f096bf
+   reset_state_machine();
f096bf
 
f096bf
    /* Read the contents of the Opcodes table.  */
f096bf
    DiCursor standard_opcodes = external;
f096bf
@@ -632,55 +607,49 @@ void read_dwarf2_lineblock ( struct _DebugInfo* di,
f096bf
                         (Int)op_code, advAddr, state_machine_regs.address,
f096bf
                         (Int)adv, (Int)state_machine_regs.line );
f096bf
 
f096bf
-         if (state_machine_regs.is_stmt) {
f096bf
-            /* only add a statement if there was a previous boundary */
f096bf
-            if (state_machine_regs.last_address) {
f096bf
-               ML_(addLineInfo)(
f096bf
-                  di,
f096bf
-                  safe_fndn_ix (fndn_ix_xa,
f096bf
-                                state_machine_regs.last_file),
f096bf
-                  di->text_debug_bias + state_machine_regs.last_address, 
f096bf
-                  di->text_debug_bias + state_machine_regs.address, 
f096bf
-                  state_machine_regs.last_line, 
f096bf
-                  0
f096bf
-               );
f096bf
-            }
f096bf
-            state_machine_regs.last_address = state_machine_regs.address;
f096bf
-            state_machine_regs.last_file = state_machine_regs.file;
f096bf
-            state_machine_regs.last_line = state_machine_regs.line;
f096bf
+         /* only add a statement if there was a previous boundary */
f096bf
+         if (state_machine_regs.last_address) {
f096bf
+            ML_(addLineInfo)(
f096bf
+               di,
f096bf
+               safe_fndn_ix(fndn_ix_xa,
f096bf
+                            state_machine_regs.last_file),
f096bf
+               di->text_debug_bias + state_machine_regs.last_address, 
f096bf
+               di->text_debug_bias + state_machine_regs.address, 
f096bf
+               state_machine_regs.last_line, 
f096bf
+               0
f096bf
+            );
f096bf
          }
f096bf
+         state_machine_regs.last_address = state_machine_regs.address;
f096bf
+         state_machine_regs.last_file = state_machine_regs.file;
f096bf
+         state_machine_regs.last_line = state_machine_regs.line;
f096bf
       }
f096bf
 
f096bf
       else { /* ! (op_code >= info.li_opcode_base) */
f096bf
 
f096bf
       switch (op_code) {
f096bf
          case DW_LNS_extended_op:
f096bf
-            process_extended_line_op (
f096bf
-                       di, fndn_ix_xa,
f096bf
-                       &data, info.li_default_is_stmt);
f096bf
+            process_extended_line_op(di, fndn_ix_xa, &data);
f096bf
             break;
f096bf
 
f096bf
          case DW_LNS_copy:
f096bf
             if (0) VG_(printf)("1002: di->o %#lx, smr.a %#lx\n",
f096bf
                                (UWord)di->text_debug_bias,
f096bf
                                state_machine_regs.address );
f096bf
-            if (state_machine_regs.is_stmt) {
f096bf
-               /* only add a statement if there was a previous boundary */
f096bf
-               if (state_machine_regs.last_address) {
f096bf
-                  ML_(addLineInfo)(
f096bf
-                     di,
f096bf
-                     safe_fndn_ix (fndn_ix_xa,
f096bf
-                                   state_machine_regs.last_file), 
f096bf
-                     di->text_debug_bias + state_machine_regs.last_address, 
f096bf
-                     di->text_debug_bias + state_machine_regs.address,
f096bf
-                     state_machine_regs.last_line, 
f096bf
-                     0
f096bf
-                  );
f096bf
-               }
f096bf
-               state_machine_regs.last_address = state_machine_regs.address;
f096bf
-               state_machine_regs.last_file = state_machine_regs.file;
f096bf
-               state_machine_regs.last_line = state_machine_regs.line;
f096bf
+            /* only add a statement if there was a previous boundary */
f096bf
+            if (state_machine_regs.last_address) {
f096bf
+               ML_(addLineInfo)(
f096bf
+                  di,
f096bf
+                  safe_fndn_ix(fndn_ix_xa,
f096bf
+                               state_machine_regs.last_file), 
f096bf
+                  di->text_debug_bias + state_machine_regs.last_address, 
f096bf
+                  di->text_debug_bias + state_machine_regs.address,
f096bf
+                  state_machine_regs.last_line, 
f096bf
+                  0
f096bf
+               );
f096bf
             }
f096bf
+            state_machine_regs.last_address = state_machine_regs.address;
f096bf
+            state_machine_regs.last_file = state_machine_regs.file;
f096bf
+            state_machine_regs.last_line = state_machine_regs.line;
f096bf
             state_machine_regs.basic_block = 0; /* JRS added */
f096bf
             if (di->ddump_line)
f096bf
                VG_(printf)("  Copy\n");
f096bf
@@ -719,9 +688,6 @@ void read_dwarf2_lineblock ( struct _DebugInfo* di,
f096bf
             break;
f096bf
          }
f096bf
          case DW_LNS_negate_stmt: {
f096bf
-            Int adv = state_machine_regs.is_stmt;
f096bf
-            adv = ! adv;
f096bf
-            state_machine_regs.is_stmt = adv;
f096bf
             if (di->ddump_line)
f096bf
                VG_(printf)("  DWARF2-line: negate_stmt\n");
f096bf
             break;
f096bf
diff --git a/coregrind/m_debuginfo/storage.c b/coregrind/m_debuginfo/storage.c
f096bf
index 7b2e26a..e6a9856 100644
f096bf
--- a/coregrind/m_debuginfo/storage.c
f096bf
+++ b/coregrind/m_debuginfo/storage.c
f096bf
@@ -419,6 +419,21 @@ static void addLoc ( struct _DebugInfo* di, DiLoc* loc, UInt fndn_ix )
f096bf
    /* Zero-sized locs should have been ignored earlier */
f096bf
    vg_assert(loc->size > 0);
f096bf
 
f096bf
+   /* Check if the last entry has adjacent range for the same line. */
f096bf
+   if (di->loctab_used > 0) {
f096bf
+      DiLoc *previous = &di->loctab[di->loctab_used - 1];
f096bf
+      if ((previous->lineno == loc->lineno)
f096bf
+          && (previous->addr + previous->size == loc->addr)) {
f096bf
+         if (0)
f096bf
+            VG_(printf)("Merging previous: addr %#lx, size %d, line %d, "
f096bf
+                        "with current: addr %#lx, size %d, line %d.\n",
f096bf
+                        previous->addr, previous->size, previous->lineno,
f096bf
+                        loc->addr, loc->size, loc->lineno);
f096bf
+         previous->size += loc->size;
f096bf
+         return;
f096bf
+      }
f096bf
+   }
f096bf
+
f096bf
    if (di->loctab_used == di->loctab_size) {
f096bf
       UInt   new_sz;
f096bf
       DiLoc* new_loctab;