/thirdparty/breakpad/processor/basic_code_modules.cc

http://github.com/tomahawk-player/tomahawk · C++ · 123 lines · 63 code · 18 blank · 42 comment · 5 complexity · 380734171a7b46d57804c8a53a52a06a MD5 · raw file

  1. // Copyright (c) 2006, 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. // basic_code_modules.cc: Contains all of the CodeModule objects that
  30. // were loaded into a single process.
  31. //
  32. // See basic_code_modules.h for documentation.
  33. //
  34. // Author: Mark Mentovai
  35. #include "processor/basic_code_modules.h"
  36. #include <assert.h>
  37. #include "google_breakpad/processor/code_module.h"
  38. #include "processor/linked_ptr.h"
  39. #include "processor/logging.h"
  40. #include "processor/range_map-inl.h"
  41. namespace google_breakpad {
  42. BasicCodeModules::BasicCodeModules(const CodeModules *that)
  43. : main_address_(0),
  44. map_(new RangeMap<u_int64_t, linked_ptr<const CodeModule> >()) {
  45. BPLOG_IF(ERROR, !that) << "BasicCodeModules::BasicCodeModules requires "
  46. "|that|";
  47. assert(that);
  48. const CodeModule *main_module = that->GetMainModule();
  49. if (main_module)
  50. main_address_ = main_module->base_address();
  51. unsigned int count = that->module_count();
  52. for (unsigned int module_sequence = 0;
  53. module_sequence < count;
  54. ++module_sequence) {
  55. // Make a copy of the module and insert it into the map. Use
  56. // GetModuleAtIndex because ordering is unimportant when slurping the
  57. // entire list, and GetModuleAtIndex may be faster than
  58. // GetModuleAtSequence.
  59. const CodeModule *module = that->GetModuleAtIndex(module_sequence)->Copy();
  60. if (!map_->StoreRange(module->base_address(), module->size(),
  61. linked_ptr<const CodeModule>(module))) {
  62. BPLOG(ERROR) << "Module " << module->code_file() <<
  63. " could not be stored";
  64. }
  65. }
  66. }
  67. BasicCodeModules::~BasicCodeModules() {
  68. delete map_;
  69. }
  70. unsigned int BasicCodeModules::module_count() const {
  71. return map_->GetCount();
  72. }
  73. const CodeModule* BasicCodeModules::GetModuleForAddress(
  74. u_int64_t address) const {
  75. linked_ptr<const CodeModule> module;
  76. if (!map_->RetrieveRange(address, &module, NULL, NULL)) {
  77. BPLOG(INFO) << "No module at " << HexString(address);
  78. return NULL;
  79. }
  80. return module.get();
  81. }
  82. const CodeModule* BasicCodeModules::GetMainModule() const {
  83. return GetModuleForAddress(main_address_);
  84. }
  85. const CodeModule* BasicCodeModules::GetModuleAtSequence(
  86. unsigned int sequence) const {
  87. linked_ptr<const CodeModule> module;
  88. if (!map_->RetrieveRangeAtIndex(sequence, &module, NULL, NULL)) {
  89. BPLOG(ERROR) << "RetrieveRangeAtIndex failed for sequence " << sequence;
  90. return NULL;
  91. }
  92. return module.get();
  93. }
  94. const CodeModule* BasicCodeModules::GetModuleAtIndex(
  95. unsigned int index) const {
  96. // This class stores everything in a RangeMap, without any more-efficient
  97. // way to walk the list of CodeModule objects. Implement GetModuleAtIndex
  98. // using GetModuleAtSequence, which meets all of the requirements, and
  99. // in addition, guarantees ordering.
  100. return GetModuleAtSequence(index);
  101. }
  102. const CodeModules* BasicCodeModules::Copy() const {
  103. return new BasicCodeModules(this);
  104. }
  105. } // namespace google_breakpad