/thirdparty/breakpad/common/dwarf_line_to_module.cc

http://github.com/tomahawk-player/tomahawk · C++ · 135 lines · 71 code · 17 blank · 47 comment · 26 complexity · 58eca34febcf1ba2b10475eac1014591 MD5 · raw file

  1. // Copyright (c) 2010 Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
  30. // dwarf_line_to_module.cc: Implementation of DwarfLineToModule class.
  31. // See dwarf_line_to_module.h for details.
  32. #include "common/dwarf_line_to_module.h"
  33. #include <stdio.h>
  34. // Trying to support Windows paths in a reasonable way adds a lot of
  35. // variations to test; it would be better to just put off dealing with
  36. // it until we actually have to deal with DWARF on Windows.
  37. // Return true if PATH is an absolute path, false if it is relative.
  38. static bool PathIsAbsolute(const std::string &path) {
  39. return (path.size() >= 1 && path[0] == '/');
  40. }
  41. // If PATH is an absolute path, return PATH. If PATH is a relative path,
  42. // treat it as relative to BASE and return the combined path.
  43. static std::string ExpandPath(const std::string &path,
  44. const std::string &base) {
  45. if (PathIsAbsolute(path))
  46. return path;
  47. return base + "/" + path;
  48. }
  49. namespace google_breakpad {
  50. void DwarfLineToModule::DefineDir(const std::string &name, uint32 dir_num) {
  51. // Directory number zero is reserved to mean the compilation
  52. // directory. Silently ignore attempts to redefine it.
  53. if (dir_num != 0)
  54. directories_[dir_num] = name;
  55. }
  56. void DwarfLineToModule::DefineFile(const std::string &name, int32 file_num,
  57. uint32 dir_num, uint64 mod_time,
  58. uint64 length) {
  59. if (file_num == -1)
  60. file_num = ++highest_file_number_;
  61. else if (file_num > highest_file_number_)
  62. highest_file_number_ = file_num;
  63. std::string full_name;
  64. if (dir_num != 0) {
  65. DirectoryTable::const_iterator directory_it = directories_.find(dir_num);
  66. if (directory_it != directories_.end()) {
  67. full_name = ExpandPath(name, directory_it->second);
  68. } else {
  69. if (!warned_bad_directory_number_) {
  70. fprintf(stderr, "warning: DWARF line number data refers to undefined"
  71. " directory numbers\n");
  72. warned_bad_directory_number_ = true;
  73. }
  74. full_name = name; // just treat name as relative
  75. }
  76. } else {
  77. // Directory number zero is the compilation directory; we just report
  78. // relative paths in that case.
  79. full_name = name;
  80. }
  81. // Find a Module::File object of the given name, and add it to the
  82. // file table.
  83. files_[file_num] = module_->FindFile(full_name);
  84. }
  85. void DwarfLineToModule::AddLine(uint64 address, uint64 length,
  86. uint32 file_num, uint32 line_num,
  87. uint32 column_num) {
  88. if (length == 0)
  89. return;
  90. // Clip lines not to extend beyond the end of the address space.
  91. if (address + length < address)
  92. length = -address;
  93. // Should we omit this line? (See the comments for omitted_line_end_.)
  94. if (address == 0 || address == omitted_line_end_) {
  95. omitted_line_end_ = address + length;
  96. return;
  97. } else {
  98. omitted_line_end_ = 0;
  99. }
  100. // Find the source file being referred to.
  101. Module::File *file = files_[file_num];
  102. if (!file) {
  103. if (!warned_bad_file_number_) {
  104. fprintf(stderr, "warning: DWARF line number data refers to "
  105. "undefined file numbers\n");
  106. warned_bad_file_number_ = true;
  107. }
  108. return;
  109. }
  110. Module::Line line;
  111. line.address = address;
  112. // We set the size when we get the next line or the EndSequence call.
  113. line.size = length;
  114. line.file = file;
  115. line.number = line_num;
  116. lines_->push_back(line);
  117. }
  118. } // namespace google_breakpad