1 //===-- DWARFDebugLine.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DWARFDebugLine.h"
11 
12 //#define ENABLE_DEBUG_PRINTF   // DO NOT LEAVE THIS DEFINED: DEBUG ONLY!!!
13 #include <assert.h>
14 
15 #include "lldb/Core/FileSpecList.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/Timer.h"
19 #include "lldb/Host/Host.h"
20 
21 #include "SymbolFileDWARF.h"
22 #include "LogChannelDWARF.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 using namespace std;
27 
28 //----------------------------------------------------------------------
29 // Parse
30 //
31 // Parse all information in the debug_line_data into an internal
32 // representation.
33 //----------------------------------------------------------------------
34 void
35 DWARFDebugLine::Parse(const DWARFDataExtractor& debug_line_data)
36 {
37     m_lineTableMap.clear();
38     lldb::offset_t offset = 0;
39     LineTable::shared_ptr line_table_sp(new LineTable);
40     while (debug_line_data.ValidOffset(offset))
41     {
42         const lldb::offset_t debug_line_offset = offset;
43 
44         if (line_table_sp.get() == NULL)
45             break;
46 
47         if (ParseStatementTable(debug_line_data, &offset, line_table_sp.get()))
48         {
49             // Make sure we don't don't loop infinitely
50             if (offset <= debug_line_offset)
51                 break;
52             //DEBUG_PRINTF("m_lineTableMap[0x%8.8x] = line_table_sp\n", debug_line_offset);
53             m_lineTableMap[debug_line_offset] = line_table_sp;
54             line_table_sp.reset(new LineTable);
55         }
56         else
57             ++offset;   // Try next byte in line table
58     }
59 }
60 
61 void
62 DWARFDebugLine::ParseIfNeeded(const DWARFDataExtractor& debug_line_data)
63 {
64     if (m_lineTableMap.empty())
65         Parse(debug_line_data);
66 }
67 
68 
69 //----------------------------------------------------------------------
70 // DWARFDebugLine::GetLineTable
71 //----------------------------------------------------------------------
72 DWARFDebugLine::LineTable::shared_ptr
73 DWARFDebugLine::GetLineTable(const dw_offset_t offset) const
74 {
75     DWARFDebugLine::LineTable::shared_ptr line_table_shared_ptr;
76     LineTableConstIter pos = m_lineTableMap.find(offset);
77     if (pos != m_lineTableMap.end())
78         line_table_shared_ptr = pos->second;
79     return line_table_shared_ptr;
80 }
81 
82 
83 //----------------------------------------------------------------------
84 // DumpStateToFile
85 //----------------------------------------------------------------------
86 static void
87 DumpStateToFile (dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
88 {
89     Log *log = (Log *)userData;
90     if (state.row == DWARFDebugLine::State::StartParsingLineTable)
91     {
92         // If the row is zero we are being called with the prologue only
93         state.prologue->Dump (log);
94         log->PutCString ("Address            Line   Column File");
95         log->PutCString ("------------------ ------ ------ ------");
96     }
97     else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
98     {
99         // Done parsing line table
100     }
101     else
102     {
103         log->Printf( "0x%16.16" PRIx64 " %6u %6u %6u%s\n", state.address, state.line, state.column, state.file, state.end_sequence ? " END" : "");
104     }
105 }
106 
107 //----------------------------------------------------------------------
108 // DWARFDebugLine::DumpLineTableRows
109 //----------------------------------------------------------------------
110 bool
111 DWARFDebugLine::DumpLineTableRows(Log *log, SymbolFileDWARF* dwarf2Data, dw_offset_t debug_line_offset)
112 {
113     const DWARFDataExtractor& debug_line_data = dwarf2Data->get_debug_line_data();
114 
115     if (debug_line_offset == DW_INVALID_OFFSET)
116     {
117         // Dump line table to a single file only
118         debug_line_offset = 0;
119         while (debug_line_data.ValidOffset(debug_line_offset))
120             debug_line_offset = DumpStatementTable (log, debug_line_data, debug_line_offset);
121     }
122     else
123     {
124         // Dump line table to a single file only
125         DumpStatementTable (log, debug_line_data, debug_line_offset);
126     }
127     return false;
128 }
129 
130 //----------------------------------------------------------------------
131 // DWARFDebugLine::DumpStatementTable
132 //----------------------------------------------------------------------
133 dw_offset_t
134 DWARFDebugLine::DumpStatementTable(Log *log, const DWARFDataExtractor& debug_line_data, const dw_offset_t debug_line_offset)
135 {
136     if (debug_line_data.ValidOffset(debug_line_offset))
137     {
138         lldb::offset_t offset = debug_line_offset;
139         log->Printf(  "----------------------------------------------------------------------\n"
140                     "debug_line[0x%8.8x]\n"
141                     "----------------------------------------------------------------------\n", debug_line_offset);
142 
143         if (ParseStatementTable(debug_line_data, &offset, DumpStateToFile, log))
144             return offset;
145         else
146             return debug_line_offset + 1;   // Skip to next byte in .debug_line section
147     }
148 
149     return DW_INVALID_OFFSET;
150 }
151 
152 
153 //----------------------------------------------------------------------
154 // DumpOpcodes
155 //----------------------------------------------------------------------
156 bool
157 DWARFDebugLine::DumpOpcodes(Log *log, SymbolFileDWARF* dwarf2Data, dw_offset_t debug_line_offset, uint32_t dump_flags)
158 {
159     const DWARFDataExtractor& debug_line_data = dwarf2Data->get_debug_line_data();
160 
161     if (debug_line_data.GetByteSize() == 0)
162     {
163         log->Printf( "< EMPTY >\n");
164         return false;
165     }
166 
167     if (debug_line_offset == DW_INVALID_OFFSET)
168     {
169         // Dump line table to a single file only
170         debug_line_offset = 0;
171         while (debug_line_data.ValidOffset(debug_line_offset))
172             debug_line_offset = DumpStatementOpcodes (log, debug_line_data, debug_line_offset, dump_flags);
173     }
174     else
175     {
176         // Dump line table to a single file only
177         DumpStatementOpcodes (log, debug_line_data, debug_line_offset, dump_flags);
178     }
179     return false;
180 }
181 
182 //----------------------------------------------------------------------
183 // DumpStatementOpcodes
184 //----------------------------------------------------------------------
185 dw_offset_t
186 DWARFDebugLine::DumpStatementOpcodes(Log *log, const DWARFDataExtractor& debug_line_data, const dw_offset_t debug_line_offset, uint32_t flags)
187 {
188     lldb::offset_t offset = debug_line_offset;
189     if (debug_line_data.ValidOffset(offset))
190     {
191         Prologue prologue;
192 
193         if (ParsePrologue(debug_line_data, &offset, &prologue))
194         {
195             log->PutCString ("----------------------------------------------------------------------");
196             log->Printf     ("debug_line[0x%8.8x]", debug_line_offset);
197             log->PutCString ("----------------------------------------------------------------------\n");
198             prologue.Dump (log);
199         }
200         else
201         {
202             offset = debug_line_offset;
203             log->Printf( "0x%8.8" PRIx64 ": skipping pad byte %2.2x", offset, debug_line_data.GetU8(&offset));
204             return offset;
205         }
206 
207         Row row(prologue.default_is_stmt);
208         const dw_offset_t end_offset = debug_line_offset + prologue.total_length + sizeof(prologue.total_length);
209 
210         assert(debug_line_data.ValidOffset(end_offset-1));
211 
212         while (offset < end_offset)
213         {
214             const uint32_t op_offset = offset;
215             uint8_t opcode = debug_line_data.GetU8(&offset);
216             switch (opcode)
217             {
218             case 0: // Extended Opcodes always start with a zero opcode followed by
219                 {   // a uleb128 length so you can skip ones you don't know about
220 
221                     dw_offset_t ext_offset = offset;
222                     dw_uleb128_t len = debug_line_data.GetULEB128(&offset);
223                     dw_offset_t arg_size = len - (offset - ext_offset);
224                     uint8_t sub_opcode = debug_line_data.GetU8(&offset);
225 //                    if (verbose)
226 //                        log->Printf( "Extended: <%u> %2.2x ", len, sub_opcode);
227 
228                     switch (sub_opcode)
229                     {
230                     case DW_LNE_end_sequence    :
231                         log->Printf( "0x%8.8x: DW_LNE_end_sequence", op_offset);
232                         row.Dump(log);
233                         row.Reset(prologue.default_is_stmt);
234                         break;
235 
236                     case DW_LNE_set_address     :
237                         {
238                             row.address = debug_line_data.GetMaxU64(&offset, arg_size);
239                             log->Printf( "0x%8.8x: DW_LNE_set_address (0x%" PRIx64 ")", op_offset, row.address);
240                         }
241                         break;
242 
243                     case DW_LNE_define_file:
244                         {
245                             FileNameEntry fileEntry;
246                             fileEntry.name      = debug_line_data.GetCStr(&offset);
247                             fileEntry.dir_idx   = debug_line_data.GetULEB128(&offset);
248                             fileEntry.mod_time  = debug_line_data.GetULEB128(&offset);
249                             fileEntry.length    = debug_line_data.GetULEB128(&offset);
250                             log->Printf( "0x%8.8x: DW_LNE_define_file('%s', dir=%i, mod_time=0x%8.8x, length=%i )",
251                                     op_offset,
252                                     fileEntry.name.c_str(),
253                                     fileEntry.dir_idx,
254                                     fileEntry.mod_time,
255                                     fileEntry.length);
256                             prologue.file_names.push_back(fileEntry);
257                         }
258                         break;
259 
260                     case DW_LNE_set_discriminator:
261                         {
262                             uint64_t discriminator = debug_line_data.GetULEB128(&offset);
263                             log->Printf( "0x%8.8x: DW_LNE_set_discriminator (0x%" PRIx64 ")", op_offset, discriminator);
264                         }
265                         break;
266                     default:
267                         log->Printf( "0x%8.8x: DW_LNE_??? (%2.2x) - Skipping unknown upcode", op_offset, opcode);
268                         // Length doesn't include the zero opcode byte or the length itself, but
269                         // it does include the sub_opcode, so we have to adjust for that below
270                         offset += arg_size;
271                         break;
272                     }
273                 }
274                 break;
275 
276             // Standard Opcodes
277             case DW_LNS_copy:
278                 log->Printf( "0x%8.8x: DW_LNS_copy", op_offset);
279                 row.Dump (log);
280                 break;
281 
282             case DW_LNS_advance_pc:
283                 {
284                     dw_uleb128_t addr_offset_n = debug_line_data.GetULEB128(&offset);
285                     dw_uleb128_t addr_offset = addr_offset_n * prologue.min_inst_length;
286                     log->Printf( "0x%8.8x: DW_LNS_advance_pc (0x%x)", op_offset, addr_offset);
287                     row.address += addr_offset;
288                 }
289                 break;
290 
291             case DW_LNS_advance_line:
292                 {
293                     dw_sleb128_t line_offset = debug_line_data.GetSLEB128(&offset);
294                     log->Printf( "0x%8.8x: DW_LNS_advance_line (%i)", op_offset, line_offset);
295                     row.line += line_offset;
296                 }
297                 break;
298 
299             case DW_LNS_set_file:
300                 row.file = debug_line_data.GetULEB128(&offset);
301                 log->Printf( "0x%8.8x: DW_LNS_set_file (%u)", op_offset, row.file);
302                 break;
303 
304             case DW_LNS_set_column:
305                 row.column = debug_line_data.GetULEB128(&offset);
306                 log->Printf( "0x%8.8x: DW_LNS_set_column (%u)", op_offset, row.column);
307                 break;
308 
309             case DW_LNS_negate_stmt:
310                 row.is_stmt = !row.is_stmt;
311                 log->Printf( "0x%8.8x: DW_LNS_negate_stmt", op_offset);
312                 break;
313 
314             case DW_LNS_set_basic_block:
315                 row.basic_block = true;
316                 log->Printf( "0x%8.8x: DW_LNS_set_basic_block", op_offset);
317                 break;
318 
319             case DW_LNS_const_add_pc:
320                 {
321                     uint8_t adjust_opcode = 255 - prologue.opcode_base;
322                     dw_addr_t addr_offset = (adjust_opcode / prologue.line_range) * prologue.min_inst_length;
323                     log->Printf( "0x%8.8x: DW_LNS_const_add_pc (0x%8.8" PRIx64 ")", op_offset, addr_offset);
324                     row.address += addr_offset;
325                 }
326                 break;
327 
328             case DW_LNS_fixed_advance_pc:
329                 {
330                     uint16_t pc_offset = debug_line_data.GetU16(&offset);
331                     log->Printf( "0x%8.8x: DW_LNS_fixed_advance_pc (0x%4.4x)", op_offset, pc_offset);
332                     row.address += pc_offset;
333                 }
334                 break;
335 
336             case DW_LNS_set_prologue_end:
337                 row.prologue_end = true;
338                 log->Printf( "0x%8.8x: DW_LNS_set_prologue_end", op_offset);
339                 break;
340 
341             case DW_LNS_set_epilogue_begin:
342                 row.epilogue_begin = true;
343                 log->Printf( "0x%8.8x: DW_LNS_set_epilogue_begin", op_offset);
344                 break;
345 
346             case DW_LNS_set_isa:
347                 row.isa = debug_line_data.GetULEB128(&offset);
348                 log->Printf( "0x%8.8x: DW_LNS_set_isa (%u)", op_offset, row.isa);
349                 break;
350 
351             // Special Opcodes
352             default:
353                 if (opcode < prologue.opcode_base)
354                 {
355                     // We have an opcode that this parser doesn't know about, skip
356                     // the number of ULEB128 numbers that is says to skip in the
357                     // prologue's standard_opcode_lengths array
358                     uint8_t n = prologue.standard_opcode_lengths[opcode-1];
359                     log->Printf( "0x%8.8x: Special : Unknown skipping %u ULEB128 values.", op_offset, n);
360                     while (n > 0)
361                     {
362                         debug_line_data.GetULEB128(&offset);
363                         --n;
364                     }
365                 }
366                 else
367                 {
368                     uint8_t adjust_opcode = opcode - prologue.opcode_base;
369                     dw_addr_t addr_offset = (adjust_opcode / prologue.line_range) * prologue.min_inst_length;
370                     int32_t line_offset = prologue.line_base + (adjust_opcode % prologue.line_range);
371                     log->Printf("0x%8.8x: address += 0x%" PRIx64 ",  line += %i\n", op_offset, (uint64_t)addr_offset, line_offset);
372                     row.address += addr_offset;
373                     row.line += line_offset;
374                     row.Dump (log);
375                 }
376                 break;
377             }
378         }
379         return end_offset;
380     }
381     return DW_INVALID_OFFSET;
382 }
383 
384 
385 
386 
387 //----------------------------------------------------------------------
388 // Parse
389 //
390 // Parse the entire line table contents calling callback each time a
391 // new prologue is parsed and every time a new row is to be added to
392 // the line table.
393 //----------------------------------------------------------------------
394 void
395 DWARFDebugLine::Parse(const DWARFDataExtractor& debug_line_data, DWARFDebugLine::State::Callback callback, void* userData)
396 {
397     lldb::offset_t offset = 0;
398     if (debug_line_data.ValidOffset(offset))
399     {
400         if (!ParseStatementTable(debug_line_data, &offset, callback, userData))
401             ++offset;   // Skip to next byte in .debug_line section
402     }
403 }
404 
405 
406 //----------------------------------------------------------------------
407 // DWARFDebugLine::ParsePrologue
408 //----------------------------------------------------------------------
409 bool
410 DWARFDebugLine::ParsePrologue(const DWARFDataExtractor& debug_line_data, lldb::offset_t* offset_ptr, Prologue* prologue)
411 {
412     const lldb::offset_t prologue_offset = *offset_ptr;
413 
414     //DEBUG_PRINTF("0x%8.8x: ParsePrologue()\n", *offset_ptr);
415 
416     prologue->Clear();
417     uint32_t i;
418     const char * s;
419     prologue->total_length      = debug_line_data.GetDWARFInitialLength(offset_ptr);
420     prologue->version           = debug_line_data.GetU16(offset_ptr);
421     if (prologue->version < 2 || prologue->version > 4)
422       return false;
423 
424     prologue->prologue_length   = debug_line_data.GetDWARFOffset(offset_ptr);
425     const lldb::offset_t end_prologue_offset = prologue->prologue_length + *offset_ptr;
426     prologue->min_inst_length   = debug_line_data.GetU8(offset_ptr);
427     if (prologue->version >= 4)
428         prologue->maximum_operations_per_instruction = debug_line_data.GetU8(offset_ptr);
429     else
430         prologue->maximum_operations_per_instruction = 1;
431     prologue->default_is_stmt   = debug_line_data.GetU8(offset_ptr);
432     prologue->line_base         = debug_line_data.GetU8(offset_ptr);
433     prologue->line_range        = debug_line_data.GetU8(offset_ptr);
434     prologue->opcode_base       = debug_line_data.GetU8(offset_ptr);
435 
436     prologue->standard_opcode_lengths.reserve(prologue->opcode_base-1);
437 
438     for (i=1; i<prologue->opcode_base; ++i)
439     {
440         uint8_t op_len = debug_line_data.GetU8(offset_ptr);
441         prologue->standard_opcode_lengths.push_back(op_len);
442     }
443 
444     while (*offset_ptr < end_prologue_offset)
445     {
446         s = debug_line_data.GetCStr(offset_ptr);
447         if (s && s[0])
448             prologue->include_directories.push_back(s);
449         else
450             break;
451     }
452 
453     while (*offset_ptr < end_prologue_offset)
454     {
455         const char* name = debug_line_data.GetCStr( offset_ptr );
456         if (name && name[0])
457         {
458             FileNameEntry fileEntry;
459             fileEntry.name      = name;
460             fileEntry.dir_idx   = debug_line_data.GetULEB128( offset_ptr );
461             fileEntry.mod_time  = debug_line_data.GetULEB128( offset_ptr );
462             fileEntry.length    = debug_line_data.GetULEB128( offset_ptr );
463             prologue->file_names.push_back(fileEntry);
464         }
465         else
466             break;
467     }
468 
469     // XXX GNU as is broken for 64-Bit DWARF
470     if (*offset_ptr != end_prologue_offset)
471     {
472         Host::SystemLog (Host::eSystemLogWarning,
473                          "warning: parsing line table prologue at 0x%8.8" PRIx64 " should have ended at 0x%8.8" PRIx64 " but it ended at 0x%8.8" PRIx64 "\n",
474                          prologue_offset,
475                          end_prologue_offset,
476                          *offset_ptr);
477     }
478     return end_prologue_offset;
479 }
480 
481 bool
482 DWARFDebugLine::ParseSupportFiles (const lldb::ModuleSP &module_sp,
483                                    const DWARFDataExtractor& debug_line_data,
484                                    const char *cu_comp_dir,
485                                    dw_offset_t stmt_list,
486                                    FileSpecList &support_files)
487 {
488     lldb::offset_t offset = stmt_list;
489     // Skip the total length
490     (void)debug_line_data.GetDWARFInitialLength(&offset);
491     uint32_t version = debug_line_data.GetU16(&offset);
492     if (version < 2 || version > 4)
493       return false;
494 
495     const dw_offset_t end_prologue_offset = debug_line_data.GetDWARFOffset(&offset) + offset;
496     // Skip instruction length, default is stmt, line base, line range
497     offset += 4;
498     // For DWARF4, skip maximum operations per instruction
499     if (version >= 4)
500         offset += 1;
501     // Skip opcode base, and all opcode lengths
502     const uint8_t opcode_base = debug_line_data.GetU8(&offset);
503     offset += opcode_base - 1;
504     std::vector<FileSpec> include_directories{{}}; // Directory at index zero doesn't exist
505     while (offset < end_prologue_offset)
506     {
507         FileSpec dir{debug_line_data.GetCStr(&offset), false};
508         if (dir)
509             include_directories.emplace_back(std::move(dir));
510         else
511             break;
512     }
513     while (offset < end_prologue_offset)
514     {
515         FileSpec file_spec{debug_line_data.GetCStr(&offset), false};
516         if (file_spec)
517         {
518             uint32_t dir_idx = debug_line_data.GetULEB128(&offset);
519             debug_line_data.Skip_LEB128(&offset); // Skip mod_time
520             debug_line_data.Skip_LEB128(&offset); // Skip length
521 
522             if (file_spec.IsRelative())
523             {
524                 if (0 < dir_idx && dir_idx < include_directories.size())
525                 {
526                     const FileSpec &dir = include_directories[dir_idx];
527                     file_spec.PrependPathComponent(dir);
528                 }
529                 if (file_spec.IsRelative())
530                     file_spec.PrependPathComponent(cu_comp_dir);
531             }
532             std::string remapped_file;
533             if (module_sp->RemapSourceFile(file_spec.GetCString(), remapped_file))
534                 file_spec.SetFile(remapped_file, false);
535             support_files.Append(file_spec);
536         }
537     }
538 
539     if (offset != end_prologue_offset)
540     {
541         Host::SystemLog (Host::eSystemLogError,
542                          "warning: parsing line table prologue at 0x%8.8x should have ended at 0x%8.8x but it ended at 0x%8.8" PRIx64 "\n",
543                          stmt_list,
544                          end_prologue_offset,
545                          offset);
546     }
547     return end_prologue_offset;
548 }
549 
550 //----------------------------------------------------------------------
551 // ParseStatementTable
552 //
553 // Parse a single line table (prologue and all rows) and call the
554 // callback function once for the prologue (row in state will be zero)
555 // and each time a row is to be added to the line table.
556 //----------------------------------------------------------------------
557 bool
558 DWARFDebugLine::ParseStatementTable
559 (
560     const DWARFDataExtractor& debug_line_data,
561     lldb::offset_t* offset_ptr,
562     DWARFDebugLine::State::Callback callback,
563     void* userData
564 )
565 {
566     Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_LINE));
567     Prologue::shared_ptr prologue(new Prologue());
568 
569 
570     const dw_offset_t debug_line_offset = *offset_ptr;
571 
572     Timer scoped_timer (__PRETTY_FUNCTION__,
573                         "DWARFDebugLine::ParseStatementTable (.debug_line[0x%8.8x])",
574                         debug_line_offset);
575 
576     if (!ParsePrologue(debug_line_data, offset_ptr, prologue.get()))
577     {
578         if (log)
579             log->Error ("failed to parse DWARF line table prologue");
580         // Restore our offset and return false to indicate failure!
581         *offset_ptr = debug_line_offset;
582         return false;
583     }
584 
585     if (log)
586         prologue->Dump (log);
587 
588     const dw_offset_t end_offset = debug_line_offset + prologue->total_length + (debug_line_data.GetDWARFSizeofInitialLength());
589 
590     State state(prologue, log, callback, userData);
591 
592     while (*offset_ptr < end_offset)
593     {
594         //DEBUG_PRINTF("0x%8.8x: ", *offset_ptr);
595         uint8_t opcode = debug_line_data.GetU8(offset_ptr);
596 
597         if (opcode == 0)
598         {
599             // Extended Opcodes always start with a zero opcode followed by
600             // a uleb128 length so you can skip ones you don't know about
601             lldb::offset_t ext_offset = *offset_ptr;
602             dw_uleb128_t len = debug_line_data.GetULEB128(offset_ptr);
603             dw_offset_t arg_size = len - (*offset_ptr - ext_offset);
604 
605             //DEBUG_PRINTF("Extended: <%2u> ", len);
606             uint8_t sub_opcode = debug_line_data.GetU8(offset_ptr);
607             switch (sub_opcode)
608             {
609             case DW_LNE_end_sequence:
610                 // Set the end_sequence register of the state machine to true and
611                 // append a row to the matrix using the current values of the
612                 // state-machine registers. Then reset the registers to the initial
613                 // values specified above. Every statement program sequence must end
614                 // with a DW_LNE_end_sequence instruction which creates a row whose
615                 // address is that of the byte after the last target machine instruction
616                 // of the sequence.
617                 state.end_sequence = true;
618                 state.AppendRowToMatrix(*offset_ptr);
619                 state.Reset();
620                 break;
621 
622             case DW_LNE_set_address:
623                 // Takes a single relocatable address as an operand. The size of the
624                 // operand is the size appropriate to hold an address on the target
625                 // machine. Set the address register to the value given by the
626                 // relocatable address. All of the other statement program opcodes
627                 // that affect the address register add a delta to it. This instruction
628                 // stores a relocatable value into it instead.
629                 if (arg_size == 4)
630                     state.address = debug_line_data.GetU32(offset_ptr);
631                 else // arg_size == 8
632                     state.address = debug_line_data.GetU64(offset_ptr);
633                 break;
634 
635             case DW_LNE_define_file:
636                 // Takes 4 arguments. The first is a null terminated string containing
637                 // a source file name. The second is an unsigned LEB128 number representing
638                 // the directory index of the directory in which the file was found. The
639                 // third is an unsigned LEB128 number representing the time of last
640                 // modification of the file. The fourth is an unsigned LEB128 number
641                 // representing the length in bytes of the file. The time and length
642                 // fields may contain LEB128(0) if the information is not available.
643                 //
644                 // The directory index represents an entry in the include_directories
645                 // section of the statement program prologue. The index is LEB128(0)
646                 // if the file was found in the current directory of the compilation,
647                 // LEB128(1) if it was found in the first directory in the
648                 // include_directories section, and so on. The directory index is
649                 // ignored for file names that represent full path names.
650                 //
651                 // The files are numbered, starting at 1, in the order in which they
652                 // appear; the names in the prologue come before names defined by
653                 // the DW_LNE_define_file instruction. These numbers are used in the
654                 // file register of the state machine.
655                 {
656                     FileNameEntry fileEntry;
657                     fileEntry.name      = debug_line_data.GetCStr(offset_ptr);
658                     fileEntry.dir_idx   = debug_line_data.GetULEB128(offset_ptr);
659                     fileEntry.mod_time  = debug_line_data.GetULEB128(offset_ptr);
660                     fileEntry.length    = debug_line_data.GetULEB128(offset_ptr);
661                     state.prologue->file_names.push_back(fileEntry);
662                 }
663                 break;
664 
665             default:
666                 // Length doesn't include the zero opcode byte or the length itself, but
667                 // it does include the sub_opcode, so we have to adjust for that below
668                 (*offset_ptr) += arg_size;
669                 break;
670             }
671         }
672         else if (opcode < prologue->opcode_base)
673         {
674             switch (opcode)
675             {
676             // Standard Opcodes
677             case DW_LNS_copy:
678                 // Takes no arguments. Append a row to the matrix using the
679                 // current values of the state-machine registers. Then set
680                 // the basic_block register to false.
681                 state.AppendRowToMatrix(*offset_ptr);
682                 break;
683 
684             case DW_LNS_advance_pc:
685                 // Takes a single unsigned LEB128 operand, multiplies it by the
686                 // min_inst_length field of the prologue, and adds the
687                 // result to the address register of the state machine.
688                 state.address += debug_line_data.GetULEB128(offset_ptr) * prologue->min_inst_length;
689                 break;
690 
691             case DW_LNS_advance_line:
692                 // Takes a single signed LEB128 operand and adds that value to
693                 // the line register of the state machine.
694                 state.line += debug_line_data.GetSLEB128(offset_ptr);
695                 break;
696 
697             case DW_LNS_set_file:
698                 // Takes a single unsigned LEB128 operand and stores it in the file
699                 // register of the state machine.
700                 state.file = debug_line_data.GetULEB128(offset_ptr);
701                 break;
702 
703             case DW_LNS_set_column:
704                 // Takes a single unsigned LEB128 operand and stores it in the
705                 // column register of the state machine.
706                 state.column = debug_line_data.GetULEB128(offset_ptr);
707                 break;
708 
709             case DW_LNS_negate_stmt:
710                 // Takes no arguments. Set the is_stmt register of the state
711                 // machine to the logical negation of its current value.
712                 state.is_stmt = !state.is_stmt;
713                 break;
714 
715             case DW_LNS_set_basic_block:
716                 // Takes no arguments. Set the basic_block register of the
717                 // state machine to true
718                 state.basic_block = true;
719                 break;
720 
721             case DW_LNS_const_add_pc:
722                 // Takes no arguments. Add to the address register of the state
723                 // machine the address increment value corresponding to special
724                 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
725                 // when the statement program needs to advance the address by a
726                 // small amount, it can use a single special opcode, which occupies
727                 // a single byte. When it needs to advance the address by up to
728                 // twice the range of the last special opcode, it can use
729                 // DW_LNS_const_add_pc followed by a special opcode, for a total
730                 // of two bytes. Only if it needs to advance the address by more
731                 // than twice that range will it need to use both DW_LNS_advance_pc
732                 // and a special opcode, requiring three or more bytes.
733                 {
734                     uint8_t adjust_opcode = 255 - prologue->opcode_base;
735                     dw_addr_t addr_offset = (adjust_opcode / prologue->line_range) * prologue->min_inst_length;
736                     state.address += addr_offset;
737                 }
738                 break;
739 
740             case DW_LNS_fixed_advance_pc:
741                 // Takes a single uhalf operand. Add to the address register of
742                 // the state machine the value of the (unencoded) operand. This
743                 // is the only extended opcode that takes an argument that is not
744                 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
745                 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
746                 // special opcodes because they cannot encode LEB128 numbers or
747                 // judge when the computation of a special opcode overflows and
748                 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
749                 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
750                 state.address += debug_line_data.GetU16(offset_ptr);
751                 break;
752 
753             case DW_LNS_set_prologue_end:
754                 // Takes no arguments. Set the prologue_end register of the
755                 // state machine to true
756                 state.prologue_end = true;
757                 break;
758 
759             case DW_LNS_set_epilogue_begin:
760                 // Takes no arguments. Set the basic_block register of the
761                 // state machine to true
762                 state.epilogue_begin = true;
763                 break;
764 
765             case DW_LNS_set_isa:
766                 // Takes a single unsigned LEB128 operand and stores it in the
767                 // column register of the state machine.
768                 state.isa = debug_line_data.GetULEB128(offset_ptr);
769                 break;
770 
771             default:
772                 // Handle any unknown standard opcodes here. We know the lengths
773                 // of such opcodes because they are specified in the prologue
774                 // as a multiple of LEB128 operands for each opcode.
775                 {
776                     uint8_t i;
777                     assert (static_cast<size_t>(opcode - 1) < prologue->standard_opcode_lengths.size());
778                     const uint8_t opcode_length = prologue->standard_opcode_lengths[opcode - 1];
779                     for (i=0; i<opcode_length; ++i)
780                         debug_line_data.Skip_LEB128(offset_ptr);
781                 }
782                 break;
783             }
784         }
785         else
786         {
787             // Special Opcodes
788 
789             // A special opcode value is chosen based on the amount that needs
790             // to be added to the line and address registers. The maximum line
791             // increment for a special opcode is the value of the line_base
792             // field in the header, plus the value of the line_range field,
793             // minus 1 (line base + line range - 1). If the desired line
794             // increment is greater than the maximum line increment, a standard
795             // opcode must be used instead of a special opcode. The "address
796             // advance" is calculated by dividing the desired address increment
797             // by the minimum_instruction_length field from the header. The
798             // special opcode is then calculated using the following formula:
799             //
800             //  opcode = (desired line increment - line_base) + (line_range * address advance) + opcode_base
801             //
802             // If the resulting opcode is greater than 255, a standard opcode
803             // must be used instead.
804             //
805             // To decode a special opcode, subtract the opcode_base from the
806             // opcode itself to give the adjusted opcode. The amount to
807             // increment the address register is the result of the adjusted
808             // opcode divided by the line_range multiplied by the
809             // minimum_instruction_length field from the header. That is:
810             //
811             //  address increment = (adjusted opcode / line_range) * minimum_instruction_length
812             //
813             // The amount to increment the line register is the line_base plus
814             // the result of the adjusted opcode modulo the line_range. That is:
815             //
816             // line increment = line_base + (adjusted opcode % line_range)
817 
818             uint8_t adjust_opcode = opcode - prologue->opcode_base;
819             dw_addr_t addr_offset = (adjust_opcode / prologue->line_range) * prologue->min_inst_length;
820             int32_t line_offset = prologue->line_base + (adjust_opcode % prologue->line_range);
821             state.line += line_offset;
822             state.address += addr_offset;
823             state.AppendRowToMatrix(*offset_ptr);
824         }
825     }
826 
827     state.Finalize( *offset_ptr );
828 
829     return end_offset;
830 }
831 
832 
833 //----------------------------------------------------------------------
834 // ParseStatementTableCallback
835 //----------------------------------------------------------------------
836 static void
837 ParseStatementTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
838 {
839     DWARFDebugLine::LineTable* line_table = (DWARFDebugLine::LineTable*)userData;
840     if (state.row == DWARFDebugLine::State::StartParsingLineTable)
841     {
842         // Just started parsing the line table, so lets keep a reference to
843         // the prologue using the supplied shared pointer
844         line_table->prologue = state.prologue;
845     }
846     else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
847     {
848         // Done parsing line table, nothing to do for the cleanup
849     }
850     else
851     {
852         // We have a new row, lets append it
853         line_table->AppendRow(state);
854     }
855 }
856 
857 //----------------------------------------------------------------------
858 // ParseStatementTable
859 //
860 // Parse a line table at offset and populate the LineTable class with
861 // the prologue and all rows.
862 //----------------------------------------------------------------------
863 bool
864 DWARFDebugLine::ParseStatementTable(const DWARFDataExtractor& debug_line_data, lldb::offset_t *offset_ptr, LineTable* line_table)
865 {
866     return ParseStatementTable(debug_line_data, offset_ptr, ParseStatementTableCallback, line_table);
867 }
868 
869 
870 inline bool
871 DWARFDebugLine::Prologue::IsValid() const
872 {
873     return SymbolFileDWARF::SupportedVersion(version);
874 }
875 
876 //----------------------------------------------------------------------
877 // DWARFDebugLine::Prologue::Dump
878 //----------------------------------------------------------------------
879 void
880 DWARFDebugLine::Prologue::Dump(Log *log)
881 {
882     uint32_t i;
883 
884     log->Printf( "Line table prologue:");
885     log->Printf( "   total_length: 0x%8.8x", total_length);
886     log->Printf( "        version: %u", version);
887     log->Printf( "prologue_length: 0x%8.8x", prologue_length);
888     log->Printf( "min_inst_length: %u", min_inst_length);
889     log->Printf( "default_is_stmt: %u", default_is_stmt);
890     log->Printf( "      line_base: %i", line_base);
891     log->Printf( "     line_range: %u", line_range);
892     log->Printf( "    opcode_base: %u", opcode_base);
893 
894     for (i=0; i<standard_opcode_lengths.size(); ++i)
895     {
896         log->Printf( "standard_opcode_lengths[%s] = %u", DW_LNS_value_to_name(i+1), standard_opcode_lengths[i]);
897     }
898 
899     if (!include_directories.empty())
900     {
901         for (i=0; i<include_directories.size(); ++i)
902         {
903             log->Printf( "include_directories[%3u] = '%s'", i+1, include_directories[i].c_str());
904         }
905     }
906 
907     if (!file_names.empty())
908     {
909         log->PutCString ("                Dir  Mod Time   File Len   File Name");
910         log->PutCString ("                ---- ---------- ---------- ---------------------------");
911         for (i=0; i<file_names.size(); ++i)
912         {
913             const FileNameEntry& fileEntry = file_names[i];
914             log->Printf ("file_names[%3u] %4u 0x%8.8x 0x%8.8x %s",
915                 i+1,
916                 fileEntry.dir_idx,
917                 fileEntry.mod_time,
918                 fileEntry.length,
919                 fileEntry.name.c_str());
920         }
921     }
922 }
923 
924 
925 //----------------------------------------------------------------------
926 // DWARFDebugLine::ParsePrologue::Append
927 //
928 // Append the contents of the prologue to the binary stream buffer
929 //----------------------------------------------------------------------
930 //void
931 //DWARFDebugLine::Prologue::Append(BinaryStreamBuf& buff) const
932 //{
933 //  uint32_t i;
934 //
935 //  buff.Append32(total_length);
936 //  buff.Append16(version);
937 //  buff.Append32(prologue_length);
938 //  buff.Append8(min_inst_length);
939 //  buff.Append8(default_is_stmt);
940 //  buff.Append8(line_base);
941 //  buff.Append8(line_range);
942 //  buff.Append8(opcode_base);
943 //
944 //  for (i=0; i<standard_opcode_lengths.size(); ++i)
945 //      buff.Append8(standard_opcode_lengths[i]);
946 //
947 //  for (i=0; i<include_directories.size(); ++i)
948 //      buff.AppendCStr(include_directories[i].c_str());
949 //  buff.Append8(0);    // Terminate the include directory section with empty string
950 //
951 //  for (i=0; i<file_names.size(); ++i)
952 //  {
953 //      buff.AppendCStr(file_names[i].name.c_str());
954 //      buff.Append32_as_ULEB128(file_names[i].dir_idx);
955 //      buff.Append32_as_ULEB128(file_names[i].mod_time);
956 //      buff.Append32_as_ULEB128(file_names[i].length);
957 //  }
958 //  buff.Append8(0);    // Terminate the file names section with empty string
959 //}
960 
961 
962 bool DWARFDebugLine::Prologue::GetFile(uint32_t file_idx, std::string& path, std::string& directory) const
963 {
964     uint32_t idx = file_idx - 1;    // File indexes are 1 based...
965     if (idx < file_names.size())
966     {
967         path = file_names[idx].name;
968         uint32_t dir_idx = file_names[idx].dir_idx - 1;
969         if (dir_idx < include_directories.size())
970             directory = include_directories[dir_idx];
971         else
972             directory.clear();
973         return true;
974     }
975     return false;
976 }
977 
978 //----------------------------------------------------------------------
979 // DWARFDebugLine::LineTable::Dump
980 //----------------------------------------------------------------------
981 void
982 DWARFDebugLine::LineTable::Dump(Log *log) const
983 {
984     if (prologue.get())
985         prologue->Dump (log);
986 
987     if (!rows.empty())
988     {
989         log->PutCString ("Address            Line   Column File   ISA Flags");
990         log->PutCString ("------------------ ------ ------ ------ --- -------------");
991         Row::const_iterator pos = rows.begin();
992         Row::const_iterator end = rows.end();
993         while (pos != end)
994         {
995             (*pos).Dump (log);
996             ++pos;
997         }
998     }
999 }
1000 
1001 
1002 void
1003 DWARFDebugLine::LineTable::AppendRow(const DWARFDebugLine::Row& state)
1004 {
1005     rows.push_back(state);
1006 }
1007 
1008 
1009 
1010 //----------------------------------------------------------------------
1011 // Compare function for the binary search in DWARFDebugLine::LineTable::LookupAddress()
1012 //----------------------------------------------------------------------
1013 static bool FindMatchingAddress (const DWARFDebugLine::Row& row1, const DWARFDebugLine::Row& row2)
1014 {
1015     return row1.address < row2.address;
1016 }
1017 
1018 
1019 //----------------------------------------------------------------------
1020 // DWARFDebugLine::LineTable::LookupAddress
1021 //----------------------------------------------------------------------
1022 uint32_t
1023 DWARFDebugLine::LineTable::LookupAddress(dw_addr_t address, dw_addr_t cu_high_pc) const
1024 {
1025     uint32_t index = UINT32_MAX;
1026     if (!rows.empty())
1027     {
1028         // Use the lower_bound algorithm to perform a binary search since we know
1029         // that our line table data is ordered by address.
1030         DWARFDebugLine::Row row;
1031         row.address = address;
1032         Row::const_iterator begin_pos = rows.begin();
1033         Row::const_iterator end_pos = rows.end();
1034         Row::const_iterator pos = lower_bound(begin_pos, end_pos, row, FindMatchingAddress);
1035         if (pos == end_pos)
1036         {
1037             if (address < cu_high_pc)
1038                 return rows.size()-1;
1039         }
1040         else
1041         {
1042             // Rely on fact that we are using a std::vector and we can do
1043             // pointer arithmetic to find the row index (which will be one less
1044             // that what we found since it will find the first position after
1045             // the current address) since std::vector iterators are just
1046             // pointers to the container type.
1047             index = pos - begin_pos;
1048             if (pos->address > address)
1049             {
1050                 if (index > 0)
1051                     --index;
1052                 else
1053                     index = UINT32_MAX;
1054             }
1055         }
1056     }
1057     return index;   // Failed to find address
1058 }
1059 
1060 
1061 //----------------------------------------------------------------------
1062 // DWARFDebugLine::Row::Row
1063 //----------------------------------------------------------------------
1064 DWARFDebugLine::Row::Row(bool default_is_stmt) :
1065     address(0),
1066     line(1),
1067     column(0),
1068     file(1),
1069     is_stmt(default_is_stmt),
1070     basic_block(false),
1071     end_sequence(false),
1072     prologue_end(false),
1073     epilogue_begin(false),
1074     isa(0)
1075 {
1076 }
1077 
1078 //----------------------------------------------------------------------
1079 // Called after a row is appended to the matrix
1080 //----------------------------------------------------------------------
1081 void
1082 DWARFDebugLine::Row::PostAppend()
1083 {
1084     basic_block = false;
1085     prologue_end = false;
1086     epilogue_begin = false;
1087 }
1088 
1089 
1090 //----------------------------------------------------------------------
1091 // DWARFDebugLine::Row::Reset
1092 //----------------------------------------------------------------------
1093 void
1094 DWARFDebugLine::Row::Reset(bool default_is_stmt)
1095 {
1096     address = 0;
1097     line = 1;
1098     column = 0;
1099     file = 1;
1100     is_stmt = default_is_stmt;
1101     basic_block = false;
1102     end_sequence = false;
1103     prologue_end = false;
1104     epilogue_begin = false;
1105     isa = 0;
1106 }
1107 //----------------------------------------------------------------------
1108 // DWARFDebugLine::Row::Dump
1109 //----------------------------------------------------------------------
1110 void
1111 DWARFDebugLine::Row::Dump(Log *log) const
1112 {
1113     log->Printf( "0x%16.16" PRIx64 " %6u %6u %6u %3u %s%s%s%s%s",
1114                 address,
1115                 line,
1116                 column,
1117                 file,
1118                 isa,
1119                 is_stmt ? " is_stmt" : "",
1120                 basic_block ? " basic_block" : "",
1121                 prologue_end ? " prologue_end" : "",
1122                 epilogue_begin ? " epilogue_begin" : "",
1123                 end_sequence ? " end_sequence" : "");
1124 }
1125 
1126 //----------------------------------------------------------------------
1127 // Compare function LineTable structures
1128 //----------------------------------------------------------------------
1129 static bool AddressLessThan (const DWARFDebugLine::Row& a, const DWARFDebugLine::Row& b)
1130 {
1131     return a.address < b.address;
1132 }
1133 
1134 
1135 
1136 // Insert a row at the correct address if the addresses can be out of
1137 // order which can only happen when we are linking a line table that
1138 // may have had it's contents rearranged.
1139 void
1140 DWARFDebugLine::Row::Insert(Row::collection& state_coll, const Row& state)
1141 {
1142     // If we don't have anything yet, or if the address of the last state in our
1143     // line table is less than the current one, just append the current state
1144     if (state_coll.empty() || AddressLessThan(state_coll.back(), state))
1145     {
1146         state_coll.push_back(state);
1147     }
1148     else
1149     {
1150         // Do a binary search for the correct entry
1151         pair<Row::iterator, Row::iterator> range(equal_range(state_coll.begin(), state_coll.end(), state, AddressLessThan));
1152 
1153         // If the addresses are equal, we can safely replace the previous entry
1154         // with the current one if the one it is replacing is an end_sequence entry.
1155         // We currently always place an extra end sequence when ever we exit a valid
1156         // address range for a function in case the functions get rearranged by
1157         // optimizations or by order specifications. These extra end sequences will
1158         // disappear by getting replaced with valid consecutive entries within a
1159         // compile unit if there are no gaps.
1160         if (range.first == range.second)
1161         {
1162             state_coll.insert(range.first, state);
1163         }
1164         else
1165         {
1166             if ((distance(range.first, range.second) == 1) && range.first->end_sequence == true)
1167             {
1168                 *range.first = state;
1169             }
1170             else
1171             {
1172                 state_coll.insert(range.second, state);
1173             }
1174         }
1175     }
1176 }
1177 
1178 void
1179 DWARFDebugLine::Row::Dump(Log *log, const Row::collection& state_coll)
1180 {
1181     std::for_each (state_coll.begin(), state_coll.end(), bind2nd(std::mem_fun_ref(&Row::Dump),log));
1182 }
1183 
1184 
1185 //----------------------------------------------------------------------
1186 // DWARFDebugLine::State::State
1187 //----------------------------------------------------------------------
1188 DWARFDebugLine::State::State(Prologue::shared_ptr& p, Log *l, DWARFDebugLine::State::Callback cb, void* userData) :
1189     Row (p->default_is_stmt),
1190     prologue (p),
1191     log (l),
1192     callback (cb),
1193     callbackUserData (userData),
1194     row (StartParsingLineTable)
1195 {
1196     // Call the callback with the initial row state of zero for the prologue
1197     if (callback)
1198         callback(0, *this, callbackUserData);
1199 }
1200 
1201 //----------------------------------------------------------------------
1202 // DWARFDebugLine::State::Reset
1203 //----------------------------------------------------------------------
1204 void
1205 DWARFDebugLine::State::Reset()
1206 {
1207     Row::Reset(prologue->default_is_stmt);
1208 }
1209 
1210 //----------------------------------------------------------------------
1211 // DWARFDebugLine::State::AppendRowToMatrix
1212 //----------------------------------------------------------------------
1213 void
1214 DWARFDebugLine::State::AppendRowToMatrix(dw_offset_t offset)
1215 {
1216     // Each time we are to add an entry into the line table matrix
1217     // call the callback function so that someone can do something with
1218     // the current state of the state machine (like build a line table
1219     // or dump the line table!)
1220     if (log)
1221     {
1222         if (row == 0)
1223         {
1224             log->PutCString ("Address            Line   Column File   ISA Flags");
1225             log->PutCString ("------------------ ------ ------ ------ --- -------------");
1226         }
1227         Dump (log);
1228     }
1229 
1230     ++row;  // Increase the row number before we call our callback for a real row
1231     if (callback)
1232         callback(offset, *this, callbackUserData);
1233     PostAppend();
1234 }
1235 
1236 //----------------------------------------------------------------------
1237 // DWARFDebugLine::State::Finalize
1238 //----------------------------------------------------------------------
1239 void
1240 DWARFDebugLine::State::Finalize(dw_offset_t offset)
1241 {
1242     // Call the callback with a special row state when we are done parsing a
1243     // line table
1244     row = DoneParsingLineTable;
1245     if (callback)
1246         callback(offset, *this, callbackUserData);
1247 }
1248 
1249 //void
1250 //DWARFDebugLine::AppendLineTableData
1251 //(
1252 //  const DWARFDebugLine::Prologue* prologue,
1253 //  const DWARFDebugLine::Row::collection& state_coll,
1254 //  const uint32_t addr_size,
1255 //  BinaryStreamBuf &debug_line_data
1256 //)
1257 //{
1258 //  if (state_coll.empty())
1259 //  {
1260 //      // We have no entries, just make an empty line table
1261 //      debug_line_data.Append8(0);
1262 //      debug_line_data.Append8(1);
1263 //      debug_line_data.Append8(DW_LNE_end_sequence);
1264 //  }
1265 //  else
1266 //  {
1267 //      DWARFDebugLine::Row::const_iterator pos;
1268 //      Row::const_iterator end = state_coll.end();
1269 //      bool default_is_stmt = prologue->default_is_stmt;
1270 //      const DWARFDebugLine::Row reset_state(default_is_stmt);
1271 //      const DWARFDebugLine::Row* prev_state = &reset_state;
1272 //      const int32_t max_line_increment_for_special_opcode = prologue->MaxLineIncrementForSpecialOpcode();
1273 //      for (pos = state_coll.begin(); pos != end; ++pos)
1274 //      {
1275 //          const DWARFDebugLine::Row& curr_state = *pos;
1276 //          int32_t line_increment  = 0;
1277 //          dw_addr_t addr_offset   = curr_state.address - prev_state->address;
1278 //          dw_addr_t addr_advance  = (addr_offset) / prologue->min_inst_length;
1279 //          line_increment = (int32_t)(curr_state.line - prev_state->line);
1280 //
1281 //          // If our previous state was the reset state, then let's emit the
1282 //          // address to keep GDB's DWARF parser happy. If we don't start each
1283 //          // sequence with a DW_LNE_set_address opcode, the line table won't
1284 //          // get slid properly in GDB.
1285 //
1286 //          if (prev_state == &reset_state)
1287 //          {
1288 //              debug_line_data.Append8(0); // Extended opcode
1289 //              debug_line_data.Append32_as_ULEB128(addr_size + 1); // Length of opcode bytes
1290 //              debug_line_data.Append8(DW_LNE_set_address);
1291 //              debug_line_data.AppendMax64(curr_state.address, addr_size);
1292 //              addr_advance = 0;
1293 //          }
1294 //
1295 //          if (prev_state->file != curr_state.file)
1296 //          {
1297 //              debug_line_data.Append8(DW_LNS_set_file);
1298 //              debug_line_data.Append32_as_ULEB128(curr_state.file);
1299 //          }
1300 //
1301 //          if (prev_state->column != curr_state.column)
1302 //          {
1303 //              debug_line_data.Append8(DW_LNS_set_column);
1304 //              debug_line_data.Append32_as_ULEB128(curr_state.column);
1305 //          }
1306 //
1307 //          // Don't do anything fancy if we are at the end of a sequence
1308 //          // as we don't want to push any extra rows since the DW_LNE_end_sequence
1309 //          // will push a row itself!
1310 //          if (curr_state.end_sequence)
1311 //          {
1312 //              if (line_increment != 0)
1313 //              {
1314 //                  debug_line_data.Append8(DW_LNS_advance_line);
1315 //                  debug_line_data.Append32_as_SLEB128(line_increment);
1316 //              }
1317 //
1318 //              if (addr_advance > 0)
1319 //              {
1320 //                  debug_line_data.Append8(DW_LNS_advance_pc);
1321 //                  debug_line_data.Append32_as_ULEB128(addr_advance);
1322 //              }
1323 //
1324 //              // Now push the end sequence on!
1325 //              debug_line_data.Append8(0);
1326 //              debug_line_data.Append8(1);
1327 //              debug_line_data.Append8(DW_LNE_end_sequence);
1328 //
1329 //              prev_state = &reset_state;
1330 //          }
1331 //          else
1332 //          {
1333 //              if (line_increment || addr_advance)
1334 //              {
1335 //                  if (line_increment > max_line_increment_for_special_opcode)
1336 //                  {
1337 //                      debug_line_data.Append8(DW_LNS_advance_line);
1338 //                      debug_line_data.Append32_as_SLEB128(line_increment);
1339 //                      line_increment = 0;
1340 //                  }
1341 //
1342 //                  uint32_t special_opcode = (line_increment >= prologue->line_base) ? ((line_increment - prologue->line_base) + (prologue->line_range * addr_advance) + prologue->opcode_base) : 256;
1343 //                  if (special_opcode > 255)
1344 //                  {
1345 //                      // Both the address and line won't fit in one special opcode
1346 //                      // check to see if just the line advance will?
1347 //                      uint32_t special_opcode_line = ((line_increment >= prologue->line_base) && (line_increment != 0)) ?
1348 //                              ((line_increment - prologue->line_base) + prologue->opcode_base) : 256;
1349 //
1350 //
1351 //                      if (special_opcode_line > 255)
1352 //                      {
1353 //                          // Nope, the line advance won't fit by itself, check the address increment by itself
1354 //                          uint32_t special_opcode_addr = addr_advance ?
1355 //                              ((0 - prologue->line_base) + (prologue->line_range * addr_advance) + prologue->opcode_base) : 256;
1356 //
1357 //                          if (special_opcode_addr > 255)
1358 //                          {
1359 //                              // Neither the address nor the line will fit in a
1360 //                              // special opcode, we must manually enter both then
1361 //                              // do a DW_LNS_copy to push a row (special opcode
1362 //                              // automatically imply a new row is pushed)
1363 //                              if (line_increment != 0)
1364 //                              {
1365 //                                  debug_line_data.Append8(DW_LNS_advance_line);
1366 //                                  debug_line_data.Append32_as_SLEB128(line_increment);
1367 //                              }
1368 //
1369 //                              if (addr_advance > 0)
1370 //                              {
1371 //                                  debug_line_data.Append8(DW_LNS_advance_pc);
1372 //                                  debug_line_data.Append32_as_ULEB128(addr_advance);
1373 //                              }
1374 //
1375 //                              // Now push a row onto the line table manually
1376 //                              debug_line_data.Append8(DW_LNS_copy);
1377 //
1378 //                          }
1379 //                          else
1380 //                          {
1381 //                              // The address increment alone will fit into a special opcode
1382 //                              // so modify our line change, then issue a special opcode
1383 //                              // for the address increment and it will push a row into the
1384 //                              // line table
1385 //                              if (line_increment != 0)
1386 //                              {
1387 //                                  debug_line_data.Append8(DW_LNS_advance_line);
1388 //                                  debug_line_data.Append32_as_SLEB128(line_increment);
1389 //                              }
1390 //
1391 //                              // Advance of line and address will fit into a single byte special opcode
1392 //                              // and this will also push a row onto the line table
1393 //                              debug_line_data.Append8(special_opcode_addr);
1394 //                          }
1395 //                      }
1396 //                      else
1397 //                      {
1398 //                          // The line change alone will fit into a special opcode
1399 //                          // so modify our address increment first, then issue a
1400 //                          // special opcode for the line change and it will push
1401 //                          // a row into the line table
1402 //                          if (addr_advance > 0)
1403 //                          {
1404 //                              debug_line_data.Append8(DW_LNS_advance_pc);
1405 //                              debug_line_data.Append32_as_ULEB128(addr_advance);
1406 //                          }
1407 //
1408 //                          // Advance of line and address will fit into a single byte special opcode
1409 //                          // and this will also push a row onto the line table
1410 //                          debug_line_data.Append8(special_opcode_line);
1411 //                      }
1412 //                  }
1413 //                  else
1414 //                  {
1415 //                      // Advance of line and address will fit into a single byte special opcode
1416 //                      // and this will also push a row onto the line table
1417 //                      debug_line_data.Append8(special_opcode);
1418 //                  }
1419 //              }
1420 //              prev_state = &curr_state;
1421 //          }
1422 //      }
1423 //  }
1424 //}
1425