1
// Copyright (c) 2005, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
31
// Author: Craig Silverstein
33
// This is an internal header file used by profiler.cc. It defines
34
// the single (inline) function GetPC. GetPC is used in a signal
35
// handler to figure out the instruction that was being executed when
36
// the signal-handler was triggered.
38
// To get this, we use the ucontext_t argument to the signal-handler
39
// callback, which holds the full context of what was going on when
40
// the signal triggered. How to get from a ucontext_t to a Program
41
// Counter is OS-dependent.
48
// On many linux systems, we may need _GNU_SOURCE to get access to
49
// the defined constants that define the register we want to see (eg
50
// REG_EIP). Note this #define must come first!
52
// If #define _GNU_SOURCE causes problems, this might work instead.
53
// It will cause problems for FreeBSD though!, because it turns off
54
// the needed __BSD_VISIBLE.
55
//#define _XOPEN_SOURCE 500
57
#include <string.h> // for memcmp
58
#ifdef HAVE_UCONTEXT_H
59
#include <ucontext.h> // for ucontext_t (and also mcontext_t)
63
// Take the example where function Foo() calls function Bar(). For
64
// many architectures, Bar() is responsible for setting up and tearing
65
// down its own stack frame. In that case, it's possible for the
66
// interrupt to happen when execution is in Bar(), but the stack frame
67
// is not properly set up (either before it's done being set up, or
68
// after it's been torn down but before Bar() returns). In those
69
// cases, the stack trace cannot see the caller function anymore.
71
// GetPC can try to identify this situation, on architectures where it
72
// might occur, and unwind the current function call in that case to
73
// avoid false edges in the profile graph (that is, edges that appear
74
// to show a call skipping over a function). To do this, we hard-code
75
// in the asm instructions we might see when setting up or tearing
76
// down a stack frame.
78
// This is difficult to get right: the instructions depend on the
79
// processor, the compiler ABI, and even the optimization level. This
80
// is a best effort patch -- if we fail to detect such a situation, or
81
// mess up the PC, nothing happens; the returned PC is not used for
82
// any further processing.
83
struct CallUnrollInfo {
84
// Offset from (e)ip register where this instruction sequence
85
// should be matched. Interpreted as bytes. Offset 0 is the next
86
// instruction to execute. Be extra careful with negative offsets in
87
// architectures of variable instruction length (like x86) - it is
88
// not that easy as taking an offset to step one instruction back!
90
// The actual instruction bytes. Feel free to make it larger if you
91
// need a longer sequence.
93
// How many bytes to match from ins array?
95
// The offset from the stack pointer (e)sp where to look for the
96
// call return address. Interpreted as bytes.
101
// The dereferences needed to get the PC from a struct ucontext were
102
// determined at configure time, and stored in the macro
103
// PC_FROM_UCONTEXT in config.h. The only thing we need to do here,
104
// then, is to do the magic call-unrolling for systems that support it.
106
// -- Special case 1: linux x86, for which we have CallUnrollInfo
107
#if defined(__linux) && defined(__i386) && defined(__GNUC__)
108
static const CallUnrollInfo callunrollinfo[] = {
109
// Entry to a function: push %ebp; mov %esp,%ebp
110
// Top-of-stack contains the caller IP.
112
{0x55, 0x89, 0xe5}, 3,
115
// Entry to a function, second instruction: push %ebp; mov %esp,%ebp
116
// Top-of-stack contains the old frame, caller IP is +4.
118
{0x55, 0x89, 0xe5}, 3,
121
// Return from a function: RET.
122
// Top-of-stack contains the caller IP.
129
inline void* GetPC(const ucontext_t& signal_ucontext) {
130
// See comment above struct CallUnrollInfo. Only try instruction
131
// flow matching if both eip and esp looks reasonable.
132
const int eip = signal_ucontext.uc_mcontext.gregs[REG_EIP];
133
const int esp = signal_ucontext.uc_mcontext.gregs[REG_ESP];
134
if ((eip & 0xffff0000) != 0 && (~eip & 0xffff0000) != 0 &&
135
(esp & 0xffff0000) != 0) {
136
char* eip_char = reinterpret_cast<char*>(eip);
137
for (int i = 0; i < sizeof(callunrollinfo)/sizeof(*callunrollinfo); ++i) {
138
if (!memcmp(eip_char + callunrollinfo[i].pc_offset,
139
callunrollinfo[i].ins, callunrollinfo[i].ins_size)) {
141
void **retaddr = (void**)(esp + callunrollinfo[i].return_sp_offset);
149
// Special case #2: Windows, which has to do something totally different.
151
// If this is ever implemented, probably the way to do it is to have
152
// profiler.cc use a high-precision timer via timeSetEvent:
153
// http://msdn2.microsoft.com/en-us/library/ms712713.aspx
154
// We'd use it in mode TIME_CALLBACK_FUNCTION/TIME_PERIODIC.
155
// The callback function would be something like prof_handler, but
156
// alas the arguments are different: no ucontext_t! I don't know
157
// how we'd get the PC (using StackWalk64?)
158
// http://msdn2.microsoft.com/en-us/library/ms680650.aspx
160
#include "base/logging.h" // for RAW_LOG
162
inline void* GetPC(const struct ucontext_t& signal_ucontext) {
163
RAW_LOG(ERROR, "GetPC is not yet implemented on Windows\n");
167
// Normal cases. If this doesn't compile, it's probably because
168
// PC_FROM_UCONTEXT is the empty string. You need to figure out
169
// the right value for your system, and add it to the list in
170
// configure.ac (or set it manually in your config.h).
172
inline void* GetPC(const ucontext_t& signal_ucontext) {
173
return (void*)signal_ucontext.PC_FROM_UCONTEXT; // defined in config.h
178
#endif // BASE_GETPC_H_