/thirdparty/breakpad/common/dwarf_line_to_module.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 179 lines · 35 code · 20 blank · 124 comment · 0 complexity · 811be83cb5a323d7ac2aa68b3fc55277 MD5 · raw file

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