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     const char * s;
492     uint32_t version = debug_line_data.GetU16(&offset);
493     if (version < 2 || version > 4)
494       return false;
495 
496     const dw_offset_t end_prologue_offset = debug_line_data.GetDWARFOffset(&offset) + offset;
497     // Skip instruction length, default is stmt, line base, line range
498     offset += 4;
499     // For DWARF4, skip maximum operations per instruction
500     if (version >= 4)
501         offset += 1;
502     // Skip opcode base, and all opcode lengths
503     const uint8_t opcode_base = debug_line_data.GetU8(&offset);
504     offset += opcode_base - 1;
505     std::vector<std::string> include_directories;
506     include_directories.push_back("");  // Directory at index zero doesn't exist
507     while (offset < end_prologue_offset)
508     {
509         s = debug_line_data.GetCStr(&offset);
510         if (s && s[0])
511             include_directories.push_back(s);
512         else
513             break;
514     }
515     std::string fullpath;
516     std::string remapped_fullpath;
517     while (offset < end_prologue_offset)
518     {
519         const char* path = debug_line_data.GetCStr( &offset );
520         if (path && path[0])
521         {
522             uint32_t dir_idx    = debug_line_data.GetULEB128( &offset );
523             debug_line_data.Skip_LEB128(&offset); // Skip mod_time
524             debug_line_data.Skip_LEB128(&offset); // Skip length
525 
526             if (path[0] == '/')
527             {
528                 // The path starts with a directory delimiter, so we are done.
529                 if (module_sp->RemapSourceFile (path, fullpath))
530                     support_files.Append(FileSpec (fullpath.c_str(), false));
531                 else
532                     support_files.Append(FileSpec (path, false));
533             }
534             else
535             {
536                 if (dir_idx > 0 && dir_idx < include_directories.size())
537                 {
538                     if (cu_comp_dir && include_directories[dir_idx][0] != '/')
539                     {
540                         fullpath = cu_comp_dir;
541 
542                         if (*fullpath.rbegin() != '/')
543                             fullpath += '/';
544                         fullpath += include_directories[dir_idx];
545 
546                     }
547                     else
548                         fullpath = include_directories[dir_idx];
549                 }
550                 else if (cu_comp_dir && cu_comp_dir[0])
551                 {
552                     fullpath = cu_comp_dir;
553                 }
554 
555                 if (!fullpath.empty())
556                 {
557                    if (*fullpath.rbegin() != '/')
558                         fullpath += '/';
559                 }
560                 fullpath += path;
561                 if (module_sp->RemapSourceFile (fullpath.c_str(), remapped_fullpath))
562                     support_files.Append(FileSpec (remapped_fullpath.c_str(), false));
563                 else
564                     support_files.Append(FileSpec (fullpath.c_str(), false));
565             }
566 
567         }
568     }
569 
570     if (offset != end_prologue_offset)
571     {
572         Host::SystemLog (Host::eSystemLogError,
573                          "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",
574                          stmt_list,
575                          end_prologue_offset,
576                          offset);
577     }
578     return end_prologue_offset;
579 }
580 
581 //----------------------------------------------------------------------
582 // ParseStatementTable
583 //
584 // Parse a single line table (prologue and all rows) and call the
585 // callback function once for the prologue (row in state will be zero)
586 // and each time a row is to be added to the line table.
587 //----------------------------------------------------------------------
588 bool
589 DWARFDebugLine::ParseStatementTable
590 (
591     const DWARFDataExtractor& debug_line_data,
592     lldb::offset_t* offset_ptr,
593     DWARFDebugLine::State::Callback callback,
594     void* userData
595 )
596 {
597     Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_LINE));
598     Prologue::shared_ptr prologue(new Prologue());
599 
600 
601     const dw_offset_t debug_line_offset = *offset_ptr;
602 
603     Timer scoped_timer (__PRETTY_FUNCTION__,
604                         "DWARFDebugLine::ParseStatementTable (.debug_line[0x%8.8x])",
605                         debug_line_offset);
606 
607     if (!ParsePrologue(debug_line_data, offset_ptr, prologue.get()))
608     {
609         if (log)
610             log->Error ("failed to parse DWARF line table prologue");
611         // Restore our offset and return false to indicate failure!
612         *offset_ptr = debug_line_offset;
613         return false;
614     }
615 
616     if (log)
617         prologue->Dump (log);
618 
619     const dw_offset_t end_offset = debug_line_offset + prologue->total_length + (debug_line_data.GetDWARFSizeofInitialLength());
620 
621     State state(prologue, log, callback, userData);
622 
623     while (*offset_ptr < end_offset)
624     {
625         //DEBUG_PRINTF("0x%8.8x: ", *offset_ptr);
626         uint8_t opcode = debug_line_data.GetU8(offset_ptr);
627 
628         if (opcode == 0)
629         {
630             // Extended Opcodes always start with a zero opcode followed by
631             // a uleb128 length so you can skip ones you don't know about
632             lldb::offset_t ext_offset = *offset_ptr;
633             dw_uleb128_t len = debug_line_data.GetULEB128(offset_ptr);
634             dw_offset_t arg_size = len - (*offset_ptr - ext_offset);
635 
636             //DEBUG_PRINTF("Extended: <%2u> ", len);
637             uint8_t sub_opcode = debug_line_data.GetU8(offset_ptr);
638             switch (sub_opcode)
639             {
640             case DW_LNE_end_sequence:
641                 // Set the end_sequence register of the state machine to true and
642                 // append a row to the matrix using the current values of the
643                 // state-machine registers. Then reset the registers to the initial
644                 // values specified above. Every statement program sequence must end
645                 // with a DW_LNE_end_sequence instruction which creates a row whose
646                 // address is that of the byte after the last target machine instruction
647                 // of the sequence.
648                 state.end_sequence = true;
649                 state.AppendRowToMatrix(*offset_ptr);
650                 state.Reset();
651                 break;
652 
653             case DW_LNE_set_address:
654                 // Takes a single relocatable address as an operand. The size of the
655                 // operand is the size appropriate to hold an address on the target
656                 // machine. Set the address register to the value given by the
657                 // relocatable address. All of the other statement program opcodes
658                 // that affect the address register add a delta to it. This instruction
659                 // stores a relocatable value into it instead.
660                 if (arg_size == 4)
661                     state.address = debug_line_data.GetU32(offset_ptr);
662                 else // arg_size == 8
663                     state.address = debug_line_data.GetU64(offset_ptr);
664                 break;
665 
666             case DW_LNE_define_file:
667                 // Takes 4 arguments. The first is a null terminated string containing
668                 // a source file name. The second is an unsigned LEB128 number representing
669                 // the directory index of the directory in which the file was found. The
670                 // third is an unsigned LEB128 number representing the time of last
671                 // modification of the file. The fourth is an unsigned LEB128 number
672                 // representing the length in bytes of the file. The time and length
673                 // fields may contain LEB128(0) if the information is not available.
674                 //
675                 // The directory index represents an entry in the include_directories
676                 // section of the statement program prologue. The index is LEB128(0)
677                 // if the file was found in the current directory of the compilation,
678                 // LEB128(1) if it was found in the first directory in the
679                 // include_directories section, and so on. The directory index is
680                 // ignored for file names that represent full path names.
681                 //
682                 // The files are numbered, starting at 1, in the order in which they
683                 // appear; the names in the prologue come before names defined by
684                 // the DW_LNE_define_file instruction. These numbers are used in the
685                 // file register of the state machine.
686                 {
687                     FileNameEntry fileEntry;
688                     fileEntry.name      = debug_line_data.GetCStr(offset_ptr);
689                     fileEntry.dir_idx   = debug_line_data.GetULEB128(offset_ptr);
690                     fileEntry.mod_time  = debug_line_data.GetULEB128(offset_ptr);
691                     fileEntry.length    = debug_line_data.GetULEB128(offset_ptr);
692                     state.prologue->file_names.push_back(fileEntry);
693                 }
694                 break;
695 
696             default:
697                 // Length doesn't include the zero opcode byte or the length itself, but
698                 // it does include the sub_opcode, so we have to adjust for that below
699                 (*offset_ptr) += arg_size;
700                 break;
701             }
702         }
703         else if (opcode < prologue->opcode_base)
704         {
705             switch (opcode)
706             {
707             // Standard Opcodes
708             case DW_LNS_copy:
709                 // Takes no arguments. Append a row to the matrix using the
710                 // current values of the state-machine registers. Then set
711                 // the basic_block register to false.
712                 state.AppendRowToMatrix(*offset_ptr);
713                 break;
714 
715             case DW_LNS_advance_pc:
716                 // Takes a single unsigned LEB128 operand, multiplies it by the
717                 // min_inst_length field of the prologue, and adds the
718                 // result to the address register of the state machine.
719                 state.address += debug_line_data.GetULEB128(offset_ptr) * prologue->min_inst_length;
720                 break;
721 
722             case DW_LNS_advance_line:
723                 // Takes a single signed LEB128 operand and adds that value to
724                 // the line register of the state machine.
725                 state.line += debug_line_data.GetSLEB128(offset_ptr);
726                 break;
727 
728             case DW_LNS_set_file:
729                 // Takes a single unsigned LEB128 operand and stores it in the file
730                 // register of the state machine.
731                 state.file = debug_line_data.GetULEB128(offset_ptr);
732                 break;
733 
734             case DW_LNS_set_column:
735                 // Takes a single unsigned LEB128 operand and stores it in the
736                 // column register of the state machine.
737                 state.column = debug_line_data.GetULEB128(offset_ptr);
738                 break;
739 
740             case DW_LNS_negate_stmt:
741                 // Takes no arguments. Set the is_stmt register of the state
742                 // machine to the logical negation of its current value.
743                 state.is_stmt = !state.is_stmt;
744                 break;
745 
746             case DW_LNS_set_basic_block:
747                 // Takes no arguments. Set the basic_block register of the
748                 // state machine to true
749                 state.basic_block = true;
750                 break;
751 
752             case DW_LNS_const_add_pc:
753                 // Takes no arguments. Add to the address register of the state
754                 // machine the address increment value corresponding to special
755                 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
756                 // when the statement program needs to advance the address by a
757                 // small amount, it can use a single special opcode, which occupies
758                 // a single byte. When it needs to advance the address by up to
759                 // twice the range of the last special opcode, it can use
760                 // DW_LNS_const_add_pc followed by a special opcode, for a total
761                 // of two bytes. Only if it needs to advance the address by more
762                 // than twice that range will it need to use both DW_LNS_advance_pc
763                 // and a special opcode, requiring three or more bytes.
764                 {
765                     uint8_t adjust_opcode = 255 - prologue->opcode_base;
766                     dw_addr_t addr_offset = (adjust_opcode / prologue->line_range) * prologue->min_inst_length;
767                     state.address += addr_offset;
768                 }
769                 break;
770 
771             case DW_LNS_fixed_advance_pc:
772                 // Takes a single uhalf operand. Add to the address register of
773                 // the state machine the value of the (unencoded) operand. This
774                 // is the only extended opcode that takes an argument that is not
775                 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
776                 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
777                 // special opcodes because they cannot encode LEB128 numbers or
778                 // judge when the computation of a special opcode overflows and
779                 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
780                 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
781                 state.address += debug_line_data.GetU16(offset_ptr);
782                 break;
783 
784             case DW_LNS_set_prologue_end:
785                 // Takes no arguments. Set the prologue_end register of the
786                 // state machine to true
787                 state.prologue_end = true;
788                 break;
789 
790             case DW_LNS_set_epilogue_begin:
791                 // Takes no arguments. Set the basic_block register of the
792                 // state machine to true
793                 state.epilogue_begin = true;
794                 break;
795 
796             case DW_LNS_set_isa:
797                 // Takes a single unsigned LEB128 operand and stores it in the
798                 // column register of the state machine.
799                 state.isa = debug_line_data.GetULEB128(offset_ptr);
800                 break;
801 
802             default:
803                 // Handle any unknown standard opcodes here. We know the lengths
804                 // of such opcodes because they are specified in the prologue
805                 // as a multiple of LEB128 operands for each opcode.
806                 {
807                     uint8_t i;
808                     assert (static_cast<size_t>(opcode - 1) < prologue->standard_opcode_lengths.size());
809                     const uint8_t opcode_length = prologue->standard_opcode_lengths[opcode - 1];
810                     for (i=0; i<opcode_length; ++i)
811                         debug_line_data.Skip_LEB128(offset_ptr);
812                 }
813                 break;
814             }
815         }
816         else
817         {
818             // Special Opcodes
819 
820             // A special opcode value is chosen based on the amount that needs
821             // to be added to the line and address registers. The maximum line
822             // increment for a special opcode is the value of the line_base
823             // field in the header, plus the value of the line_range field,
824             // minus 1 (line base + line range - 1). If the desired line
825             // increment is greater than the maximum line increment, a standard
826             // opcode must be used instead of a special opcode. The "address
827             // advance" is calculated by dividing the desired address increment
828             // by the minimum_instruction_length field from the header. The
829             // special opcode is then calculated using the following formula:
830             //
831             //  opcode = (desired line increment - line_base) + (line_range * address advance) + opcode_base
832             //
833             // If the resulting opcode is greater than 255, a standard opcode
834             // must be used instead.
835             //
836             // To decode a special opcode, subtract the opcode_base from the
837             // opcode itself to give the adjusted opcode. The amount to
838             // increment the address register is the result of the adjusted
839             // opcode divided by the line_range multiplied by the
840             // minimum_instruction_length field from the header. That is:
841             //
842             //  address increment = (adjusted opcode / line_range) * minimum_instruction_length
843             //
844             // The amount to increment the line register is the line_base plus
845             // the result of the adjusted opcode modulo the line_range. That is:
846             //
847             // line increment = line_base + (adjusted opcode % line_range)
848 
849             uint8_t adjust_opcode = opcode - prologue->opcode_base;
850             dw_addr_t addr_offset = (adjust_opcode / prologue->line_range) * prologue->min_inst_length;
851             int32_t line_offset = prologue->line_base + (adjust_opcode % prologue->line_range);
852             state.line += line_offset;
853             state.address += addr_offset;
854             state.AppendRowToMatrix(*offset_ptr);
855         }
856     }
857 
858     state.Finalize( *offset_ptr );
859 
860     return end_offset;
861 }
862 
863 
864 //----------------------------------------------------------------------
865 // ParseStatementTableCallback
866 //----------------------------------------------------------------------
867 static void
868 ParseStatementTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
869 {
870     DWARFDebugLine::LineTable* line_table = (DWARFDebugLine::LineTable*)userData;
871     if (state.row == DWARFDebugLine::State::StartParsingLineTable)
872     {
873         // Just started parsing the line table, so lets keep a reference to
874         // the prologue using the supplied shared pointer
875         line_table->prologue = state.prologue;
876     }
877     else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
878     {
879         // Done parsing line table, nothing to do for the cleanup
880     }
881     else
882     {
883         // We have a new row, lets append it
884         line_table->AppendRow(state);
885     }
886 }
887 
888 //----------------------------------------------------------------------
889 // ParseStatementTable
890 //
891 // Parse a line table at offset and populate the LineTable class with
892 // the prologue and all rows.
893 //----------------------------------------------------------------------
894 bool
895 DWARFDebugLine::ParseStatementTable(const DWARFDataExtractor& debug_line_data, lldb::offset_t *offset_ptr, LineTable* line_table)
896 {
897     return ParseStatementTable(debug_line_data, offset_ptr, ParseStatementTableCallback, line_table);
898 }
899 
900 
901 inline bool
902 DWARFDebugLine::Prologue::IsValid() const
903 {
904     return SymbolFileDWARF::SupportedVersion(version);
905 }
906 
907 //----------------------------------------------------------------------
908 // DWARFDebugLine::Prologue::Dump
909 //----------------------------------------------------------------------
910 void
911 DWARFDebugLine::Prologue::Dump(Log *log)
912 {
913     uint32_t i;
914 
915     log->Printf( "Line table prologue:");
916     log->Printf( "   total_length: 0x%8.8x", total_length);
917     log->Printf( "        version: %u", version);
918     log->Printf( "prologue_length: 0x%8.8x", prologue_length);
919     log->Printf( "min_inst_length: %u", min_inst_length);
920     log->Printf( "default_is_stmt: %u", default_is_stmt);
921     log->Printf( "      line_base: %i", line_base);
922     log->Printf( "     line_range: %u", line_range);
923     log->Printf( "    opcode_base: %u", opcode_base);
924 
925     for (i=0; i<standard_opcode_lengths.size(); ++i)
926     {
927         log->Printf( "standard_opcode_lengths[%s] = %u", DW_LNS_value_to_name(i+1), standard_opcode_lengths[i]);
928     }
929 
930     if (!include_directories.empty())
931     {
932         for (i=0; i<include_directories.size(); ++i)
933         {
934             log->Printf( "include_directories[%3u] = '%s'", i+1, include_directories[i].c_str());
935         }
936     }
937 
938     if (!file_names.empty())
939     {
940         log->PutCString ("                Dir  Mod Time   File Len   File Name");
941         log->PutCString ("                ---- ---------- ---------- ---------------------------");
942         for (i=0; i<file_names.size(); ++i)
943         {
944             const FileNameEntry& fileEntry = file_names[i];
945             log->Printf ("file_names[%3u] %4u 0x%8.8x 0x%8.8x %s",
946                 i+1,
947                 fileEntry.dir_idx,
948                 fileEntry.mod_time,
949                 fileEntry.length,
950                 fileEntry.name.c_str());
951         }
952     }
953 }
954 
955 
956 //----------------------------------------------------------------------
957 // DWARFDebugLine::ParsePrologue::Append
958 //
959 // Append the contents of the prologue to the binary stream buffer
960 //----------------------------------------------------------------------
961 //void
962 //DWARFDebugLine::Prologue::Append(BinaryStreamBuf& buff) const
963 //{
964 //  uint32_t i;
965 //
966 //  buff.Append32(total_length);
967 //  buff.Append16(version);
968 //  buff.Append32(prologue_length);
969 //  buff.Append8(min_inst_length);
970 //  buff.Append8(default_is_stmt);
971 //  buff.Append8(line_base);
972 //  buff.Append8(line_range);
973 //  buff.Append8(opcode_base);
974 //
975 //  for (i=0; i<standard_opcode_lengths.size(); ++i)
976 //      buff.Append8(standard_opcode_lengths[i]);
977 //
978 //  for (i=0; i<include_directories.size(); ++i)
979 //      buff.AppendCStr(include_directories[i].c_str());
980 //  buff.Append8(0);    // Terminate the include directory section with empty string
981 //
982 //  for (i=0; i<file_names.size(); ++i)
983 //  {
984 //      buff.AppendCStr(file_names[i].name.c_str());
985 //      buff.Append32_as_ULEB128(file_names[i].dir_idx);
986 //      buff.Append32_as_ULEB128(file_names[i].mod_time);
987 //      buff.Append32_as_ULEB128(file_names[i].length);
988 //  }
989 //  buff.Append8(0);    // Terminate the file names section with empty string
990 //}
991 
992 
993 bool DWARFDebugLine::Prologue::GetFile(uint32_t file_idx, std::string& path, std::string& directory) const
994 {
995     uint32_t idx = file_idx - 1;    // File indexes are 1 based...
996     if (idx < file_names.size())
997     {
998         path = file_names[idx].name;
999         uint32_t dir_idx = file_names[idx].dir_idx - 1;
1000         if (dir_idx < include_directories.size())
1001             directory = include_directories[dir_idx];
1002         else
1003             directory.clear();
1004         return true;
1005     }
1006     return false;
1007 }
1008 
1009 //----------------------------------------------------------------------
1010 // DWARFDebugLine::LineTable::Dump
1011 //----------------------------------------------------------------------
1012 void
1013 DWARFDebugLine::LineTable::Dump(Log *log) const
1014 {
1015     if (prologue.get())
1016         prologue->Dump (log);
1017 
1018     if (!rows.empty())
1019     {
1020         log->PutCString ("Address            Line   Column File   ISA Flags");
1021         log->PutCString ("------------------ ------ ------ ------ --- -------------");
1022         Row::const_iterator pos = rows.begin();
1023         Row::const_iterator end = rows.end();
1024         while (pos != end)
1025         {
1026             (*pos).Dump (log);
1027             ++pos;
1028         }
1029     }
1030 }
1031 
1032 
1033 void
1034 DWARFDebugLine::LineTable::AppendRow(const DWARFDebugLine::Row& state)
1035 {
1036     rows.push_back(state);
1037 }
1038 
1039 
1040 
1041 //----------------------------------------------------------------------
1042 // Compare function for the binary search in DWARFDebugLine::LineTable::LookupAddress()
1043 //----------------------------------------------------------------------
1044 static bool FindMatchingAddress (const DWARFDebugLine::Row& row1, const DWARFDebugLine::Row& row2)
1045 {
1046     return row1.address < row2.address;
1047 }
1048 
1049 
1050 //----------------------------------------------------------------------
1051 // DWARFDebugLine::LineTable::LookupAddress
1052 //----------------------------------------------------------------------
1053 uint32_t
1054 DWARFDebugLine::LineTable::LookupAddress(dw_addr_t address, dw_addr_t cu_high_pc) const
1055 {
1056     uint32_t index = UINT32_MAX;
1057     if (!rows.empty())
1058     {
1059         // Use the lower_bound algorithm to perform a binary search since we know
1060         // that our line table data is ordered by address.
1061         DWARFDebugLine::Row row;
1062         row.address = address;
1063         Row::const_iterator begin_pos = rows.begin();
1064         Row::const_iterator end_pos = rows.end();
1065         Row::const_iterator pos = lower_bound(begin_pos, end_pos, row, FindMatchingAddress);
1066         if (pos == end_pos)
1067         {
1068             if (address < cu_high_pc)
1069                 return rows.size()-1;
1070         }
1071         else
1072         {
1073             // Rely on fact that we are using a std::vector and we can do
1074             // pointer arithmetic to find the row index (which will be one less
1075             // that what we found since it will find the first position after
1076             // the current address) since std::vector iterators are just
1077             // pointers to the container type.
1078             index = pos - begin_pos;
1079             if (pos->address > address)
1080             {
1081                 if (index > 0)
1082                     --index;
1083                 else
1084                     index = UINT32_MAX;
1085             }
1086         }
1087     }
1088     return index;   // Failed to find address
1089 }
1090 
1091 
1092 //----------------------------------------------------------------------
1093 // DWARFDebugLine::Row::Row
1094 //----------------------------------------------------------------------
1095 DWARFDebugLine::Row::Row(bool default_is_stmt) :
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 
1109 //----------------------------------------------------------------------
1110 // Called after a row is appended to the matrix
1111 //----------------------------------------------------------------------
1112 void
1113 DWARFDebugLine::Row::PostAppend()
1114 {
1115     basic_block = false;
1116     prologue_end = false;
1117     epilogue_begin = false;
1118 }
1119 
1120 
1121 //----------------------------------------------------------------------
1122 // DWARFDebugLine::Row::Reset
1123 //----------------------------------------------------------------------
1124 void
1125 DWARFDebugLine::Row::Reset(bool default_is_stmt)
1126 {
1127     address = 0;
1128     line = 1;
1129     column = 0;
1130     file = 1;
1131     is_stmt = default_is_stmt;
1132     basic_block = false;
1133     end_sequence = false;
1134     prologue_end = false;
1135     epilogue_begin = false;
1136     isa = 0;
1137 }
1138 //----------------------------------------------------------------------
1139 // DWARFDebugLine::Row::Dump
1140 //----------------------------------------------------------------------
1141 void
1142 DWARFDebugLine::Row::Dump(Log *log) const
1143 {
1144     log->Printf( "0x%16.16" PRIx64 " %6u %6u %6u %3u %s%s%s%s%s",
1145                 address,
1146                 line,
1147                 column,
1148                 file,
1149                 isa,
1150                 is_stmt ? " is_stmt" : "",
1151                 basic_block ? " basic_block" : "",
1152                 prologue_end ? " prologue_end" : "",
1153                 epilogue_begin ? " epilogue_begin" : "",
1154                 end_sequence ? " end_sequence" : "");
1155 }
1156 
1157 //----------------------------------------------------------------------
1158 // Compare function LineTable structures
1159 //----------------------------------------------------------------------
1160 static bool AddressLessThan (const DWARFDebugLine::Row& a, const DWARFDebugLine::Row& b)
1161 {
1162     return a.address < b.address;
1163 }
1164 
1165 
1166 
1167 // Insert a row at the correct address if the addresses can be out of
1168 // order which can only happen when we are linking a line table that
1169 // may have had it's contents rearranged.
1170 void
1171 DWARFDebugLine::Row::Insert(Row::collection& state_coll, const Row& state)
1172 {
1173     // If we don't have anything yet, or if the address of the last state in our
1174     // line table is less than the current one, just append the current state
1175     if (state_coll.empty() || AddressLessThan(state_coll.back(), state))
1176     {
1177         state_coll.push_back(state);
1178     }
1179     else
1180     {
1181         // Do a binary search for the correct entry
1182         pair<Row::iterator, Row::iterator> range(equal_range(state_coll.begin(), state_coll.end(), state, AddressLessThan));
1183 
1184         // If the addresses are equal, we can safely replace the previous entry
1185         // with the current one if the one it is replacing is an end_sequence entry.
1186         // We currently always place an extra end sequence when ever we exit a valid
1187         // address range for a function in case the functions get rearranged by
1188         // optimizations or by order specifications. These extra end sequences will
1189         // disappear by getting replaced with valid consecutive entries within a
1190         // compile unit if there are no gaps.
1191         if (range.first == range.second)
1192         {
1193             state_coll.insert(range.first, state);
1194         }
1195         else
1196         {
1197             if ((distance(range.first, range.second) == 1) && range.first->end_sequence == true)
1198             {
1199                 *range.first = state;
1200             }
1201             else
1202             {
1203                 state_coll.insert(range.second, state);
1204             }
1205         }
1206     }
1207 }
1208 
1209 void
1210 DWARFDebugLine::Row::Dump(Log *log, const Row::collection& state_coll)
1211 {
1212     std::for_each (state_coll.begin(), state_coll.end(), bind2nd(std::mem_fun_ref(&Row::Dump),log));
1213 }
1214 
1215 
1216 //----------------------------------------------------------------------
1217 // DWARFDebugLine::State::State
1218 //----------------------------------------------------------------------
1219 DWARFDebugLine::State::State(Prologue::shared_ptr& p, Log *l, DWARFDebugLine::State::Callback cb, void* userData) :
1220     Row (p->default_is_stmt),
1221     prologue (p),
1222     log (l),
1223     callback (cb),
1224     callbackUserData (userData),
1225     row (StartParsingLineTable)
1226 {
1227     // Call the callback with the initial row state of zero for the prologue
1228     if (callback)
1229         callback(0, *this, callbackUserData);
1230 }
1231 
1232 //----------------------------------------------------------------------
1233 // DWARFDebugLine::State::Reset
1234 //----------------------------------------------------------------------
1235 void
1236 DWARFDebugLine::State::Reset()
1237 {
1238     Row::Reset(prologue->default_is_stmt);
1239 }
1240 
1241 //----------------------------------------------------------------------
1242 // DWARFDebugLine::State::AppendRowToMatrix
1243 //----------------------------------------------------------------------
1244 void
1245 DWARFDebugLine::State::AppendRowToMatrix(dw_offset_t offset)
1246 {
1247     // Each time we are to add an entry into the line table matrix
1248     // call the callback function so that someone can do something with
1249     // the current state of the state machine (like build a line table
1250     // or dump the line table!)
1251     if (log)
1252     {
1253         if (row == 0)
1254         {
1255             log->PutCString ("Address            Line   Column File   ISA Flags");
1256             log->PutCString ("------------------ ------ ------ ------ --- -------------");
1257         }
1258         Dump (log);
1259     }
1260 
1261     ++row;  // Increase the row number before we call our callback for a real row
1262     if (callback)
1263         callback(offset, *this, callbackUserData);
1264     PostAppend();
1265 }
1266 
1267 //----------------------------------------------------------------------
1268 // DWARFDebugLine::State::Finalize
1269 //----------------------------------------------------------------------
1270 void
1271 DWARFDebugLine::State::Finalize(dw_offset_t offset)
1272 {
1273     // Call the callback with a special row state when we are done parsing a
1274     // line table
1275     row = DoneParsingLineTable;
1276     if (callback)
1277         callback(offset, *this, callbackUserData);
1278 }
1279 
1280 //void
1281 //DWARFDebugLine::AppendLineTableData
1282 //(
1283 //  const DWARFDebugLine::Prologue* prologue,
1284 //  const DWARFDebugLine::Row::collection& state_coll,
1285 //  const uint32_t addr_size,
1286 //  BinaryStreamBuf &debug_line_data
1287 //)
1288 //{
1289 //  if (state_coll.empty())
1290 //  {
1291 //      // We have no entries, just make an empty line table
1292 //      debug_line_data.Append8(0);
1293 //      debug_line_data.Append8(1);
1294 //      debug_line_data.Append8(DW_LNE_end_sequence);
1295 //  }
1296 //  else
1297 //  {
1298 //      DWARFDebugLine::Row::const_iterator pos;
1299 //      Row::const_iterator end = state_coll.end();
1300 //      bool default_is_stmt = prologue->default_is_stmt;
1301 //      const DWARFDebugLine::Row reset_state(default_is_stmt);
1302 //      const DWARFDebugLine::Row* prev_state = &reset_state;
1303 //      const int32_t max_line_increment_for_special_opcode = prologue->MaxLineIncrementForSpecialOpcode();
1304 //      for (pos = state_coll.begin(); pos != end; ++pos)
1305 //      {
1306 //          const DWARFDebugLine::Row& curr_state = *pos;
1307 //          int32_t line_increment  = 0;
1308 //          dw_addr_t addr_offset   = curr_state.address - prev_state->address;
1309 //          dw_addr_t addr_advance  = (addr_offset) / prologue->min_inst_length;
1310 //          line_increment = (int32_t)(curr_state.line - prev_state->line);
1311 //
1312 //          // If our previous state was the reset state, then let's emit the
1313 //          // address to keep GDB's DWARF parser happy. If we don't start each
1314 //          // sequence with a DW_LNE_set_address opcode, the line table won't
1315 //          // get slid properly in GDB.
1316 //
1317 //          if (prev_state == &reset_state)
1318 //          {
1319 //              debug_line_data.Append8(0); // Extended opcode
1320 //              debug_line_data.Append32_as_ULEB128(addr_size + 1); // Length of opcode bytes
1321 //              debug_line_data.Append8(DW_LNE_set_address);
1322 //              debug_line_data.AppendMax64(curr_state.address, addr_size);
1323 //              addr_advance = 0;
1324 //          }
1325 //
1326 //          if (prev_state->file != curr_state.file)
1327 //          {
1328 //              debug_line_data.Append8(DW_LNS_set_file);
1329 //              debug_line_data.Append32_as_ULEB128(curr_state.file);
1330 //          }
1331 //
1332 //          if (prev_state->column != curr_state.column)
1333 //          {
1334 //              debug_line_data.Append8(DW_LNS_set_column);
1335 //              debug_line_data.Append32_as_ULEB128(curr_state.column);
1336 //          }
1337 //
1338 //          // Don't do anything fancy if we are at the end of a sequence
1339 //          // as we don't want to push any extra rows since the DW_LNE_end_sequence
1340 //          // will push a row itself!
1341 //          if (curr_state.end_sequence)
1342 //          {
1343 //              if (line_increment != 0)
1344 //              {
1345 //                  debug_line_data.Append8(DW_LNS_advance_line);
1346 //                  debug_line_data.Append32_as_SLEB128(line_increment);
1347 //              }
1348 //
1349 //              if (addr_advance > 0)
1350 //              {
1351 //                  debug_line_data.Append8(DW_LNS_advance_pc);
1352 //                  debug_line_data.Append32_as_ULEB128(addr_advance);
1353 //              }
1354 //
1355 //              // Now push the end sequence on!
1356 //              debug_line_data.Append8(0);
1357 //              debug_line_data.Append8(1);
1358 //              debug_line_data.Append8(DW_LNE_end_sequence);
1359 //
1360 //              prev_state = &reset_state;
1361 //          }
1362 //          else
1363 //          {
1364 //              if (line_increment || addr_advance)
1365 //              {
1366 //                  if (line_increment > max_line_increment_for_special_opcode)
1367 //                  {
1368 //                      debug_line_data.Append8(DW_LNS_advance_line);
1369 //                      debug_line_data.Append32_as_SLEB128(line_increment);
1370 //                      line_increment = 0;
1371 //                  }
1372 //
1373 //                  uint32_t special_opcode = (line_increment >= prologue->line_base) ? ((line_increment - prologue->line_base) + (prologue->line_range * addr_advance) + prologue->opcode_base) : 256;
1374 //                  if (special_opcode > 255)
1375 //                  {
1376 //                      // Both the address and line won't fit in one special opcode
1377 //                      // check to see if just the line advance will?
1378 //                      uint32_t special_opcode_line = ((line_increment >= prologue->line_base) && (line_increment != 0)) ?
1379 //                              ((line_increment - prologue->line_base) + prologue->opcode_base) : 256;
1380 //
1381 //
1382 //                      if (special_opcode_line > 255)
1383 //                      {
1384 //                          // Nope, the line advance won't fit by itself, check the address increment by itself
1385 //                          uint32_t special_opcode_addr = addr_advance ?
1386 //                              ((0 - prologue->line_base) + (prologue->line_range * addr_advance) + prologue->opcode_base) : 256;
1387 //
1388 //                          if (special_opcode_addr > 255)
1389 //                          {
1390 //                              // Neither the address nor the line will fit in a
1391 //                              // special opcode, we must manually enter both then
1392 //                              // do a DW_LNS_copy to push a row (special opcode
1393 //                              // automatically imply a new row is pushed)
1394 //                              if (line_increment != 0)
1395 //                              {
1396 //                                  debug_line_data.Append8(DW_LNS_advance_line);
1397 //                                  debug_line_data.Append32_as_SLEB128(line_increment);
1398 //                              }
1399 //
1400 //                              if (addr_advance > 0)
1401 //                              {
1402 //                                  debug_line_data.Append8(DW_LNS_advance_pc);
1403 //                                  debug_line_data.Append32_as_ULEB128(addr_advance);
1404 //                              }
1405 //
1406 //                              // Now push a row onto the line table manually
1407 //                              debug_line_data.Append8(DW_LNS_copy);
1408 //
1409 //                          }
1410 //                          else
1411 //                          {
1412 //                              // The address increment alone will fit into a special opcode
1413 //                              // so modify our line change, then issue a special opcode
1414 //                              // for the address increment and it will push a row into the
1415 //                              // line table
1416 //                              if (line_increment != 0)
1417 //                              {
1418 //                                  debug_line_data.Append8(DW_LNS_advance_line);
1419 //                                  debug_line_data.Append32_as_SLEB128(line_increment);
1420 //                              }
1421 //
1422 //                              // Advance of line and address will fit into a single byte special opcode
1423 //                              // and this will also push a row onto the line table
1424 //                              debug_line_data.Append8(special_opcode_addr);
1425 //                          }
1426 //                      }
1427 //                      else
1428 //                      {
1429 //                          // The line change alone will fit into a special opcode
1430 //                          // so modify our address increment first, then issue a
1431 //                          // special opcode for the line change and it will push
1432 //                          // a row into the line table
1433 //                          if (addr_advance > 0)
1434 //                          {
1435 //                              debug_line_data.Append8(DW_LNS_advance_pc);
1436 //                              debug_line_data.Append32_as_ULEB128(addr_advance);
1437 //                          }
1438 //
1439 //                          // Advance of line and address will fit into a single byte special opcode
1440 //                          // and this will also push a row onto the line table
1441 //                          debug_line_data.Append8(special_opcode_line);
1442 //                      }
1443 //                  }
1444 //                  else
1445 //                  {
1446 //                      // Advance of line and address will fit into a single byte special opcode
1447 //                      // and this will also push a row onto the line table
1448 //                      debug_line_data.Append8(special_opcode);
1449 //                  }
1450 //              }
1451 //              prev_state = &curr_state;
1452 //          }
1453 //      }
1454 //  }
1455 //}
1456