Home | History | Annotate | Download | only in common
      1 // -*- mode: c++ -*-
      2 
      3 // Copyright (c) 2010 Google Inc.
      4 // All rights reserved.
      5 //
      6 // Redistribution and use in source and binary forms, with or without
      7 // modification, are permitted provided that the following conditions are
      8 // met:
      9 //
     10 //     * Redistributions of source code must retain the above copyright
     11 // notice, this list of conditions and the following disclaimer.
     12 //     * Redistributions in binary form must reproduce the above
     13 // copyright notice, this list of conditions and the following disclaimer
     14 // in the documentation and/or other materials provided with the
     15 // distribution.
     16 //     * Neither the name of Google Inc. nor the names of its
     17 // contributors may be used to endorse or promote products derived from
     18 // this software without specific prior written permission.
     19 //
     20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 
     32 // Original author: Jim Blandy <jimb (at) mozilla.com> <jimb (at) red-bean.com>
     33 
     34 // The DwarfLineToModule class accepts line number information from a
     35 // DWARF parser and adds it to a google_breakpad::Module. The Module
     36 // can write that data out as a Breakpad symbol file.
     37 
     38 #ifndef COMMON_LINUX_DWARF_LINE_TO_MODULE_H
     39 #define COMMON_LINUX_DWARF_LINE_TO_MODULE_H
     40 
     41 #include <string>
     42 
     43 #include "common/module.h"
     44 #include "common/dwarf/dwarf2reader.h"
     45 #include "common/using_std_string.h"
     46 
     47 namespace google_breakpad {
     48 
     49 // A class for producing a vector of google_breakpad::Module::Line
     50 // instances from parsed DWARF line number data.
     51 //
     52 // An instance of this class can be provided as a handler to a
     53 // dwarf2reader::LineInfo DWARF line number information parser. The
     54 // handler accepts source location information from the parser and
     55 // uses it to produce a vector of google_breakpad::Module::Line
     56 // objects, referring to google_breakpad::Module::File objects added
     57 // to a particular google_breakpad::Module.
     58 //
     59 // GNU toolchain omitted sections support:
     60 // ======================================
     61 //
     62 // Given the right options, the GNU toolchain will omit unreferenced
     63 // functions from the final executable. Unfortunately, when it does so, it
     64 // does not remove the associated portions of the DWARF line number
     65 // program; instead, it gives the DW_LNE_set_address instructions referring
     66 // to the now-deleted code addresses of zero. Given this input, the DWARF
     67 // line parser will call AddLine with a series of lines starting at address
     68 // zero. For example, here is the output from 'readelf -wl' for a program
     69 // with four functions, the first three of which have been omitted:
     70 //
     71 //   Line Number Statements:
     72 //    Extended opcode 2: set Address to 0x0
     73 //    Advance Line by 14 to 15
     74 //    Copy
     75 //    Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 16
     76 //    Special opcode 119: advance Address by 8 to 0xb and Line by 2 to 18
     77 //    Advance PC by 2 to 0xd
     78 //    Extended opcode 1: End of Sequence
     79 //
     80 //    Extended opcode 2: set Address to 0x0
     81 //    Advance Line by 14 to 15
     82 //    Copy
     83 //    Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 16
     84 //    Special opcode 119: advance Address by 8 to 0xb and Line by 2 to 18
     85 //    Advance PC by 2 to 0xd
     86 //    Extended opcode 1: End of Sequence
     87 //
     88 //    Extended opcode 2: set Address to 0x0
     89 //    Advance Line by 19 to 20
     90 //    Copy
     91 //    Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 21
     92 //    Special opcode 76: advance Address by 5 to 0x8 and Line by 1 to 22
     93 //    Advance PC by 2 to 0xa
     94 //    Extended opcode 1: End of Sequence
     95 //
     96 //    Extended opcode 2: set Address to 0x80483a4
     97 //    Advance Line by 23 to 24
     98 //    Copy
     99 //    Special opcode 202: advance Address by 14 to 0x80483b2 and Line by 1 to 25
    100 //    Special opcode 76: advance Address by 5 to 0x80483b7 and Line by 1 to 26
    101 //    Advance PC by 6 to 0x80483bd
    102 //    Extended opcode 1: End of Sequence
    103 //
    104 // Instead of collecting runs of lines describing code that is not there,
    105 // we try to recognize and drop them. Since the linker doesn't explicitly
    106 // distinguish references to dropped sections from genuine references to
    107 // code at address zero, we must use a heuristic. We have chosen:
    108 //
    109 // - If a line starts at address zero, omit it. (On the platforms
    110 //   breakpad targets, it is extremely unlikely that there will be code
    111 //   at address zero.)
    112 //
    113 // - If a line starts immediately after an omitted line, omit it too.
    114 class DwarfLineToModule: public dwarf2reader::LineInfoHandler {
    115  public:
    116   // As the DWARF line info parser passes us line records, add source
    117   // files to MODULE, and add all lines to the end of LINES. LINES
    118   // need not be empty. If the parser hands us a zero-length line, we
    119   // omit it. If the parser hands us a line that extends beyond the
    120   // end of the address space, we clip it. It's up to our client to
    121   // sort out which lines belong to which functions; we don't add them
    122   // to any particular function in MODULE ourselves.
    123   DwarfLineToModule(Module *module, const string& compilation_dir,
    124                     vector<Module::Line> *lines)
    125       : module_(module),
    126         compilation_dir_(compilation_dir),
    127         lines_(lines),
    128         highest_file_number_(-1),
    129         omitted_line_end_(0),
    130         warned_bad_file_number_(false),
    131         warned_bad_directory_number_(false) { }
    132 
    133   ~DwarfLineToModule() { }
    134 
    135   void DefineDir(const string &name, uint32 dir_num);
    136   void DefineFile(const string &name, int32 file_num,
    137                   uint32 dir_num, uint64 mod_time,
    138                   uint64 length);
    139   void AddLine(uint64 address, uint64 length,
    140                uint32 file_num, uint32 line_num, uint32 column_num);
    141 
    142  private:
    143 
    144   typedef std::map<uint32, string> DirectoryTable;
    145   typedef std::map<uint32, Module::File *> FileTable;
    146 
    147   // The module we're contributing debugging info to. Owned by our
    148   // client.
    149   Module *module_;
    150 
    151   // The compilation directory for the current compilation unit whose
    152   // lines are being accumulated.
    153   string compilation_dir_;
    154 
    155   // The vector of lines we're accumulating. Owned by our client.
    156   //
    157   // In a Module, as in a breakpad symbol file, lines belong to
    158   // specific functions, but DWARF simply assigns lines to addresses;
    159   // one must infer the line/function relationship using the
    160   // functions' beginning and ending addresses. So we can't add these
    161   // to the appropriate function from module_ until we've read the
    162   // function info as well. Instead, we accumulate lines here, and let
    163   // whoever constructed this sort it all out.
    164   vector<Module::Line> *lines_;
    165 
    166   // A table mapping directory numbers to paths.
    167   DirectoryTable directories_;
    168 
    169   // A table mapping file numbers to Module::File pointers.
    170   FileTable files_;
    171 
    172   // The highest file number we've seen so far, or -1 if we've seen
    173   // none.  Used for dynamically defined file numbers.
    174   int32 highest_file_number_;
    175 
    176   // This is the ending address of the last line we omitted, or zero if we
    177   // didn't omit the previous line. It is zero before we have received any
    178   // AddLine calls.
    179   uint64 omitted_line_end_;
    180 
    181   // True if we've warned about:
    182   bool warned_bad_file_number_; // bad file numbers
    183   bool warned_bad_directory_number_; // bad directory numbers
    184 };
    185 
    186 } // namespace google_breakpad
    187 
    188 #endif // COMMON_LINUX_DWARF_LINE_TO_MODULE_H
    189