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// dump_context.h: A (mini/micro) dump CPU-specific context.
31
32#ifndef GOOGLE_BREAKPAD_PROCESSOR_DUMP_CONTEXT_H__
33#define GOOGLE_BREAKPAD_PROCESSOR_DUMP_CONTEXT_H__
34
35#include "google_breakpad/common/minidump_format.h"
36#include "google_breakpad/processor/dump_object.h"
37
38namespace google_breakpad {
39
40// DumpContext carries a CPU-specific MDRawContext structure, which contains CPU
41// context such as register states.
42class DumpContext : public DumpObject {
43 public:
44 virtual ~DumpContext();
45
46 // Returns an MD_CONTEXT_* value such as MD_CONTEXT_X86 or MD_CONTEXT_PPC
47 // identifying the CPU type that the context was collected from. The
48 // returned value will identify the CPU only, and will have any other
49 // MD_CONTEXT_* bits masked out. Returns 0 on failure.
50 uint32_t GetContextCPU() const;
51
52 // Return the raw value of |context_flags_|
53 uint32_t GetContextFlags() const;
54
55 // Returns raw CPU-specific context data for the named CPU type. If the
56 // context data does not match the CPU type or does not exist, returns NULL.
57 const MDRawContextAMD64* GetContextAMD64() const;
58 const MDRawContextARM* GetContextARM() const;
59 const MDRawContextARM64* GetContextARM64() const;
60 const MDRawContextMIPS* GetContextMIPS() const;
61 const MDRawContextPPC* GetContextPPC() const;
62 const MDRawContextPPC64* GetContextPPC64() const;
63 const MDRawContextSPARC* GetContextSPARC() const;
64 const MDRawContextX86* GetContextX86() const;
65
66 // A convenience method to get the instruction pointer out of the
67 // MDRawContext, since it varies per-CPU architecture.
68 bool GetInstructionPointer(uint64_t* ip) const;
69
70 // Similar to the GetInstructionPointer method, this method gets the stack
71 // pointer for all CPU architectures.
72 bool GetStackPointer(uint64_t* sp) const;
73
74 // Print a human-readable representation of the object to stdout.
75 void Print();
76
77 protected:
78 DumpContext();
79
80 // Sets row CPU-specific context data for the names CPU type.
81 void SetContextFlags(uint32_t context_flags);
82 void SetContextX86(MDRawContextX86* x86);
83 void SetContextPPC(MDRawContextPPC* ppc);
84 void SetContextPPC64(MDRawContextPPC64* ppc64);
85 void SetContextAMD64(MDRawContextAMD64* amd64);
86 void SetContextSPARC(MDRawContextSPARC* ctx_sparc);
87 void SetContextARM(MDRawContextARM* arm);
88 void SetContextARM64(MDRawContextARM64* arm64);
89 void SetContextMIPS(MDRawContextMIPS* ctx_mips);
90
91 // Free the CPU-specific context structure.
92 void FreeContext();
93
94 private:
95 // The CPU-specific context structure.
96 union {
97 MDRawContextBase* base;
98 MDRawContextX86* x86;
99 MDRawContextPPC* ppc;
100 MDRawContextPPC64* ppc64;
101 MDRawContextAMD64* amd64;
102 // on Solaris SPARC, sparc is defined as a numeric constant,
103 // so variables can NOT be named as sparc
104 MDRawContextSPARC* ctx_sparc;
105 MDRawContextARM* arm;
106 MDRawContextARM64* arm64;
107 MDRawContextMIPS* ctx_mips;
108 } context_;
109
110 // Store this separately because of the weirdo AMD64 context
111 uint32_t context_flags_;
112};
113
114} // namespace google_breakpad
115
116#endif // GOOGLE_BREAKPAD_PROCESSOR_DUMP_CONTEXT_H__
117