blob: 9605363bb04d2984e75c563857efbca8f1608b35 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2005, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Craig Silverstein
33//
34// This is an internal header file used by profiler.cc. It defines
35// the single (inline) function GetPC. GetPC is used in a signal
36// handler to figure out the instruction that was being executed when
37// the signal-handler was triggered.
38//
39// To get this, we use the ucontext_t argument to the signal-handler
40// callback, which holds the full context of what was going on when
41// the signal triggered. How to get from a ucontext_t to a Program
42// Counter is OS-dependent.
43
44#ifndef BASE_GETPC_H_
45#define BASE_GETPC_H_
46
47#include "config.h"
48
49// On many linux systems, we may need _GNU_SOURCE to get access to
50// the defined constants that define the register we want to see (eg
51// REG_EIP). Note this #define must come first!
52#define _GNU_SOURCE 1
53// If #define _GNU_SOURCE causes problems, this might work instead.
54// It will cause problems for FreeBSD though!, because it turns off
55// the needed __BSD_VISIBLE.
56//#define _XOPEN_SOURCE 500
57
58#include <string.h> // for memcmp
Brian Silverman20350ac2021-11-17 18:19:55 -080059#ifdef HAVE_ASM_PTRACE_H
60#include <asm/ptrace.h>
61#endif
Austin Schuh745610d2015-09-06 18:19:50 -070062#if defined(HAVE_SYS_UCONTEXT_H)
63#include <sys/ucontext.h>
64#elif defined(HAVE_UCONTEXT_H)
65#include <ucontext.h> // for ucontext_t (and also mcontext_t)
66#elif defined(HAVE_CYGWIN_SIGNAL_H)
67#include <cygwin/signal.h>
68typedef ucontext ucontext_t;
69#endif
70
71
72// Take the example where function Foo() calls function Bar(). For
73// many architectures, Bar() is responsible for setting up and tearing
74// down its own stack frame. In that case, it's possible for the
75// interrupt to happen when execution is in Bar(), but the stack frame
76// is not properly set up (either before it's done being set up, or
77// after it's been torn down but before Bar() returns). In those
78// cases, the stack trace cannot see the caller function anymore.
79//
80// GetPC can try to identify this situation, on architectures where it
81// might occur, and unwind the current function call in that case to
82// avoid false edges in the profile graph (that is, edges that appear
83// to show a call skipping over a function). To do this, we hard-code
84// in the asm instructions we might see when setting up or tearing
85// down a stack frame.
86//
87// This is difficult to get right: the instructions depend on the
88// processor, the compiler ABI, and even the optimization level. This
89// is a best effort patch -- if we fail to detect such a situation, or
90// mess up the PC, nothing happens; the returned PC is not used for
91// any further processing.
92struct CallUnrollInfo {
93 // Offset from (e)ip register where this instruction sequence
94 // should be matched. Interpreted as bytes. Offset 0 is the next
95 // instruction to execute. Be extra careful with negative offsets in
96 // architectures of variable instruction length (like x86) - it is
97 // not that easy as taking an offset to step one instruction back!
98 int pc_offset;
99 // The actual instruction bytes. Feel free to make it larger if you
100 // need a longer sequence.
101 unsigned char ins[16];
102 // How many bytes to match from ins array?
103 int ins_size;
104 // The offset from the stack pointer (e)sp where to look for the
105 // call return address. Interpreted as bytes.
106 int return_sp_offset;
107};
108
109
110// The dereferences needed to get the PC from a struct ucontext were
111// determined at configure time, and stored in the macro
112// PC_FROM_UCONTEXT in config.h. The only thing we need to do here,
113// then, is to do the magic call-unrolling for systems that support it.
114
115// -- Special case 1: linux x86, for which we have CallUnrollInfo
116#if defined(__linux) && defined(__i386) && defined(__GNUC__)
117static const CallUnrollInfo callunrollinfo[] = {
118 // Entry to a function: push %ebp; mov %esp,%ebp
119 // Top-of-stack contains the caller IP.
120 { 0,
121 {0x55, 0x89, 0xe5}, 3,
122 0
123 },
124 // Entry to a function, second instruction: push %ebp; mov %esp,%ebp
125 // Top-of-stack contains the old frame, caller IP is +4.
126 { -1,
127 {0x55, 0x89, 0xe5}, 3,
128 4
129 },
130 // Return from a function: RET.
131 // Top-of-stack contains the caller IP.
132 { 0,
133 {0xc3}, 1,
134 0
135 }
136};
137
138inline void* GetPC(const ucontext_t& signal_ucontext) {
139 // See comment above struct CallUnrollInfo. Only try instruction
140 // flow matching if both eip and esp looks reasonable.
141 const int eip = signal_ucontext.uc_mcontext.gregs[REG_EIP];
142 const int esp = signal_ucontext.uc_mcontext.gregs[REG_ESP];
143 if ((eip & 0xffff0000) != 0 && (~eip & 0xffff0000) != 0 &&
144 (esp & 0xffff0000) != 0) {
145 char* eip_char = reinterpret_cast<char*>(eip);
146 for (int i = 0; i < sizeof(callunrollinfo)/sizeof(*callunrollinfo); ++i) {
147 if (!memcmp(eip_char + callunrollinfo[i].pc_offset,
148 callunrollinfo[i].ins, callunrollinfo[i].ins_size)) {
149 // We have a match.
150 void **retaddr = (void**)(esp + callunrollinfo[i].return_sp_offset);
151 return *retaddr;
152 }
153 }
154 }
155 return (void*)eip;
156}
157
158// Special case #2: Windows, which has to do something totally different.
159#elif defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__MINGW32__)
160// If this is ever implemented, probably the way to do it is to have
161// profiler.cc use a high-precision timer via timeSetEvent:
162// http://msdn2.microsoft.com/en-us/library/ms712713.aspx
163// We'd use it in mode TIME_CALLBACK_FUNCTION/TIME_PERIODIC.
164// The callback function would be something like prof_handler, but
165// alas the arguments are different: no ucontext_t! I don't know
166// how we'd get the PC (using StackWalk64?)
167// http://msdn2.microsoft.com/en-us/library/ms680650.aspx
168
169#include "base/logging.h" // for RAW_LOG
170#ifndef HAVE_CYGWIN_SIGNAL_H
171typedef int ucontext_t;
172#endif
173
174inline void* GetPC(const struct ucontext_t& signal_ucontext) {
175 RAW_LOG(ERROR, "GetPC is not yet implemented on Windows\n");
176 return NULL;
177}
178
179// Normal cases. If this doesn't compile, it's probably because
180// PC_FROM_UCONTEXT is the empty string. You need to figure out
181// the right value for your system, and add it to the list in
182// configure.ac (or set it manually in your config.h).
183#else
184inline void* GetPC(const ucontext_t& signal_ucontext) {
Brian Silverman20350ac2021-11-17 18:19:55 -0800185#if defined(__s390__) && !defined(__s390x__)
186 // Mask out the AMODE31 bit from the PC recorded in the context.
187 return (void*)((unsigned long)signal_ucontext.PC_FROM_UCONTEXT & 0x7fffffffUL);
188#else
Austin Schuh745610d2015-09-06 18:19:50 -0700189 return (void*)signal_ucontext.PC_FROM_UCONTEXT; // defined in config.h
Brian Silverman20350ac2021-11-17 18:19:55 -0800190#endif
Austin Schuh745610d2015-09-06 18:19:50 -0700191}
192
193#endif
194
195#endif // BASE_GETPC_H_