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