xref: /aosp_15_r20/external/google-breakpad/src/processor/stackwalker_x86.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1*9712c20fSFrederick Mayle // -*- mode: c++ -*-
2*9712c20fSFrederick Mayle 
3*9712c20fSFrederick Mayle // Copyright 2010 Google LLC
4*9712c20fSFrederick Mayle //
5*9712c20fSFrederick Mayle // Redistribution and use in source and binary forms, with or without
6*9712c20fSFrederick Mayle // modification, are permitted provided that the following conditions are
7*9712c20fSFrederick Mayle // met:
8*9712c20fSFrederick Mayle //
9*9712c20fSFrederick Mayle //     * Redistributions of source code must retain the above copyright
10*9712c20fSFrederick Mayle // notice, this list of conditions and the following disclaimer.
11*9712c20fSFrederick Mayle //     * Redistributions in binary form must reproduce the above
12*9712c20fSFrederick Mayle // copyright notice, this list of conditions and the following disclaimer
13*9712c20fSFrederick Mayle // in the documentation and/or other materials provided with the
14*9712c20fSFrederick Mayle // distribution.
15*9712c20fSFrederick Mayle //     * Neither the name of Google LLC nor the names of its
16*9712c20fSFrederick Mayle // contributors may be used to endorse or promote products derived from
17*9712c20fSFrederick Mayle // this software without specific prior written permission.
18*9712c20fSFrederick Mayle //
19*9712c20fSFrederick Mayle // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*9712c20fSFrederick Mayle // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*9712c20fSFrederick Mayle // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*9712c20fSFrederick Mayle // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*9712c20fSFrederick Mayle // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*9712c20fSFrederick Mayle // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*9712c20fSFrederick Mayle // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*9712c20fSFrederick Mayle // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*9712c20fSFrederick Mayle // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*9712c20fSFrederick Mayle // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*9712c20fSFrederick Mayle // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*9712c20fSFrederick Mayle 
31*9712c20fSFrederick Mayle // stackwalker_x86.h: x86-specific stackwalker.
32*9712c20fSFrederick Mayle //
33*9712c20fSFrederick Mayle // Provides stack frames given x86 register context and a memory region
34*9712c20fSFrederick Mayle // corresponding to an x86 stack.
35*9712c20fSFrederick Mayle //
36*9712c20fSFrederick Mayle // Author: Mark Mentovai
37*9712c20fSFrederick Mayle 
38*9712c20fSFrederick Mayle 
39*9712c20fSFrederick Mayle #ifndef PROCESSOR_STACKWALKER_X86_H__
40*9712c20fSFrederick Mayle #define PROCESSOR_STACKWALKER_X86_H__
41*9712c20fSFrederick Mayle 
42*9712c20fSFrederick Mayle #include <vector>
43*9712c20fSFrederick Mayle 
44*9712c20fSFrederick Mayle #include "google_breakpad/common/breakpad_types.h"
45*9712c20fSFrederick Mayle #include "google_breakpad/common/minidump_format.h"
46*9712c20fSFrederick Mayle #include "google_breakpad/processor/stackwalker.h"
47*9712c20fSFrederick Mayle #include "google_breakpad/processor/stack_frame_cpu.h"
48*9712c20fSFrederick Mayle #include "processor/cfi_frame_info.h"
49*9712c20fSFrederick Mayle 
50*9712c20fSFrederick Mayle namespace google_breakpad {
51*9712c20fSFrederick Mayle 
52*9712c20fSFrederick Mayle class CodeModules;
53*9712c20fSFrederick Mayle 
54*9712c20fSFrederick Mayle 
55*9712c20fSFrederick Mayle class StackwalkerX86 : public Stackwalker {
56*9712c20fSFrederick Mayle  public:
57*9712c20fSFrederick Mayle   // context is an x86 context object that gives access to x86-specific
58*9712c20fSFrederick Mayle   // register state corresponding to the innermost called frame to be
59*9712c20fSFrederick Mayle   // included in the stack.  The other arguments are passed directly through
60*9712c20fSFrederick Mayle   // to the base Stackwalker constructor.
61*9712c20fSFrederick Mayle   StackwalkerX86(const SystemInfo* system_info,
62*9712c20fSFrederick Mayle                  const MDRawContextX86* context,
63*9712c20fSFrederick Mayle                  MemoryRegion* memory,
64*9712c20fSFrederick Mayle                  const CodeModules* modules,
65*9712c20fSFrederick Mayle                  StackFrameSymbolizer* frame_symbolizer);
66*9712c20fSFrederick Mayle 
67*9712c20fSFrederick Mayle  private:
68*9712c20fSFrederick Mayle   // A STACK CFI-driven frame walker for the X86.
69*9712c20fSFrederick Mayle   typedef SimpleCFIWalker<uint32_t, MDRawContextX86> CFIWalker;
70*9712c20fSFrederick Mayle 
71*9712c20fSFrederick Mayle   // Implementation of Stackwalker, using x86 context (%ebp, %esp, %eip) and
72*9712c20fSFrederick Mayle   // stack conventions (saved %ebp at [%ebp], saved %eip at 4[%ebp], or
73*9712c20fSFrederick Mayle   // alternate conventions as guided by any WindowsFrameInfo available for the
74*9712c20fSFrederick Mayle   // code in question.).
75*9712c20fSFrederick Mayle   virtual StackFrame* GetContextFrame();
76*9712c20fSFrederick Mayle   virtual StackFrame* GetCallerFrame(const CallStack* stack,
77*9712c20fSFrederick Mayle                                      bool stack_scan_allowed);
78*9712c20fSFrederick Mayle 
79*9712c20fSFrederick Mayle   // Use windows_frame_info (derived from STACK WIN and FUNC records)
80*9712c20fSFrederick Mayle   // to construct the frame that called frames.back(). The caller
81*9712c20fSFrederick Mayle   // takes ownership of the returned frame. Return NULL on failure.
82*9712c20fSFrederick Mayle   StackFrameX86* GetCallerByWindowsFrameInfo(
83*9712c20fSFrederick Mayle       const vector<StackFrame*>& frames,
84*9712c20fSFrederick Mayle       WindowsFrameInfo* windows_frame_info,
85*9712c20fSFrederick Mayle       bool stack_scan_allowed);
86*9712c20fSFrederick Mayle 
87*9712c20fSFrederick Mayle   // Use cfi_frame_info (derived from STACK CFI records) to construct
88*9712c20fSFrederick Mayle   // the frame that called frames.back(). The caller takes ownership
89*9712c20fSFrederick Mayle   // of the returned frame. Return NULL on failure.
90*9712c20fSFrederick Mayle   StackFrameX86* GetCallerByCFIFrameInfo(const vector<StackFrame*>& frames,
91*9712c20fSFrederick Mayle                                          CFIFrameInfo* cfi_frame_info);
92*9712c20fSFrederick Mayle 
93*9712c20fSFrederick Mayle   // Assuming a traditional frame layout --- where the caller's %ebp
94*9712c20fSFrederick Mayle   // has been pushed just after the return address and the callee's
95*9712c20fSFrederick Mayle   // %ebp points to the saved %ebp --- construct the frame that called
96*9712c20fSFrederick Mayle   // frames.back(). The caller takes ownership of the returned frame.
97*9712c20fSFrederick Mayle   // Return NULL on failure.
98*9712c20fSFrederick Mayle   StackFrameX86* GetCallerByEBPAtBase(const vector<StackFrame*>& frames,
99*9712c20fSFrederick Mayle                                       bool stack_scan_allowed);
100*9712c20fSFrederick Mayle 
101*9712c20fSFrederick Mayle   // Stores the CPU context corresponding to the innermost stack frame to
102*9712c20fSFrederick Mayle   // be returned by GetContextFrame.
103*9712c20fSFrederick Mayle   const MDRawContextX86* context_;
104*9712c20fSFrederick Mayle 
105*9712c20fSFrederick Mayle   // Our register map, for cfi_walker_.
106*9712c20fSFrederick Mayle   static const CFIWalker::RegisterSet cfi_register_map_[];
107*9712c20fSFrederick Mayle 
108*9712c20fSFrederick Mayle   // Our CFI frame walker.
109*9712c20fSFrederick Mayle   const CFIWalker cfi_walker_;
110*9712c20fSFrederick Mayle };
111*9712c20fSFrederick Mayle 
112*9712c20fSFrederick Mayle 
113*9712c20fSFrederick Mayle }  // namespace google_breakpad
114*9712c20fSFrederick Mayle 
115*9712c20fSFrederick Mayle 
116*9712c20fSFrederick Mayle #endif  // PROCESSOR_STACKWALKER_X86_H__
117