1// Copyright (c) 2014 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
30// microdump.h: A microdump reader. Microdump is a minified variant of a
31// minidump (see minidump.h for documentation) which contains the minimum
32// amount of information required to get a stack trace for the crashing thread.
33// The information contained in a microdump is:
34// - the crashing thread stack
35// - system information (os type / version)
36// - cpu context (state of the registers)
37// - list of mmaps
38
39#ifndef GOOGLE_BREAKPAD_PROCESSOR_MICRODUMP_H__
40#define GOOGLE_BREAKPAD_PROCESSOR_MICRODUMP_H__
41
42#include <string>
43#include <vector>
44
45#include "common/scoped_ptr.h"
46#include "common/using_std_string.h"
47#include "google_breakpad/processor/dump_context.h"
48#include "google_breakpad/processor/memory_region.h"
49#include "google_breakpad/processor/system_info.h"
50#include "processor/basic_code_modules.h"
51
52namespace google_breakpad {
53
54// MicrodumpModuleList contains all of the loaded code modules for a process
55// in the form of MicrodumpModules. It maintains a vector of these modules
56// and provides access to a code module corresponding to a specific address.
57class MicrodumpModules : public BasicCodeModules {
58 public:
59 // Takes over ownership of |module|.
60 void Add(const CodeModule* module);
61
62 // Enables/disables module address range shrink.
63 void SetEnableModuleShrink(bool is_enabled);
64};
65
66// MicrodumpContext carries a CPU-specific context.
67// See dump_context.h for documentation.
68class MicrodumpContext : public DumpContext {
69 public:
70 virtual void SetContextARM(MDRawContextARM* arm);
71 virtual void SetContextARM64(MDRawContextARM64* arm64);
72 virtual void SetContextX86(MDRawContextX86* x86);
73 virtual void SetContextMIPS(MDRawContextMIPS* mips32);
74 virtual void SetContextMIPS64(MDRawContextMIPS* mips64);
75};
76
77// This class provides access to microdump memory regions.
78// See memory_region.h for documentation.
79class MicrodumpMemoryRegion : public MemoryRegion {
80 public:
81 MicrodumpMemoryRegion();
82 virtual ~MicrodumpMemoryRegion() {}
83
84 // Set this region's address and contents. If we have placed an
85 // instance of this class in a test fixture class, individual tests
86 // can use this to provide the region's contents.
87 void Init(uint64_t base_address, const std::vector<uint8_t>& contents);
88
89 virtual uint64_t GetBase() const;
90 virtual uint32_t GetSize() const;
91
92 virtual bool GetMemoryAtAddress(uint64_t address, uint8_t* value) const;
93 virtual bool GetMemoryAtAddress(uint64_t address, uint16_t* value) const;
94 virtual bool GetMemoryAtAddress(uint64_t address, uint32_t* value) const;
95 virtual bool GetMemoryAtAddress(uint64_t address, uint64_t* value) const;
96
97 // Print a human-readable representation of the object to stdout.
98 virtual void Print() const;
99
100 private:
101 // Fetch a little-endian value from ADDRESS in contents_ whose size
102 // is BYTES, and store it in *VALUE. Returns true on success.
103 template<typename ValueType>
104 bool GetMemoryLittleEndian(uint64_t address, ValueType* value) const;
105
106 uint64_t base_address_;
107 std::vector<uint8_t> contents_;
108};
109
110// Microdump is the user's interface to a microdump file. It provides access to
111// the microdump's context, memory regions and modules.
112class Microdump {
113 public:
114 explicit Microdump(const string& contents);
115 virtual ~Microdump() {}
116
117 DumpContext* GetContext() { return context_.get(); }
118 MicrodumpMemoryRegion* GetMemory() { return stack_region_.get(); }
119 MicrodumpModules* GetModules() { return modules_.get(); }
120 SystemInfo* GetSystemInfo() { return system_info_.get(); }
121
122 string GetCrashReason() { return crash_reason_; }
123 uint64_t GetCrashAddress() { return crash_address_; }
124 private:
125 scoped_ptr<MicrodumpContext> context_;
126 scoped_ptr<MicrodumpMemoryRegion> stack_region_;
127 scoped_ptr<MicrodumpModules> modules_;
128 scoped_ptr<SystemInfo> system_info_;
129 string crash_reason_;
130 uint64_t crash_address_;
131};
132
133} // namespace google_breakpad
134
135#endif // GOOGLE_BREAKPAD_PROCESSOR_MICRODUMP_H__
136