1// Copyright (c) 2005, 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// ---
31// Author: Craig Silverstein
32//
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.
37//
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.
42
43#ifndef BASE_GETPC_H_
44#define BASE_GETPC_H_
45
46#include "config.h"
47
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!
51#define _GNU_SOURCE 1
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
56
57#include <string.h>         // for memcmp
58#if defined(HAVE_SYS_UCONTEXT_H)
59#include <sys/ucontext.h>
60#elif defined(HAVE_UCONTEXT_H)
61#include <ucontext.h>       // for ucontext_t (and also mcontext_t)
62#elif defined(HAVE_CYGWIN_SIGNAL_H)
63#include <cygwin/signal.h>
64typedef ucontext ucontext_t;
65#endif
66
67
68// Take the example where function Foo() calls function Bar().  For
69// many architectures, Bar() is responsible for setting up and tearing
70// down its own stack frame.  In that case, it's possible for the
71// interrupt to happen when execution is in Bar(), but the stack frame
72// is not properly set up (either before it's done being set up, or
73// after it's been torn down but before Bar() returns).  In those
74// cases, the stack trace cannot see the caller function anymore.
75//
76// GetPC can try to identify this situation, on architectures where it
77// might occur, and unwind the current function call in that case to
78// avoid false edges in the profile graph (that is, edges that appear
79// to show a call skipping over a function).  To do this, we hard-code
80// in the asm instructions we might see when setting up or tearing
81// down a stack frame.
82//
83// This is difficult to get right: the instructions depend on the
84// processor, the compiler ABI, and even the optimization level.  This
85// is a best effort patch -- if we fail to detect such a situation, or
86// mess up the PC, nothing happens; the returned PC is not used for
87// any further processing.
88struct CallUnrollInfo {
89  // Offset from (e)ip register where this instruction sequence
90  // should be matched. Interpreted as bytes. Offset 0 is the next
91  // instruction to execute. Be extra careful with negative offsets in
92  // architectures of variable instruction length (like x86) - it is
93  // not that easy as taking an offset to step one instruction back!
94  int pc_offset;
95  // The actual instruction bytes. Feel free to make it larger if you
96  // need a longer sequence.
97  char ins[16];
98  // How many bytes to match from ins array?
99  int ins_size;
100  // The offset from the stack pointer (e)sp where to look for the
101  // call return address. Interpreted as bytes.
102  int return_sp_offset;
103};
104
105
106// The dereferences needed to get the PC from a struct ucontext were
107// determined at configure time, and stored in the macro
108// PC_FROM_UCONTEXT in config.h.  The only thing we need to do here,
109// then, is to do the magic call-unrolling for systems that support it.
110
111// -- Special case 1: linux x86, for which we have CallUnrollInfo
112#if defined(__linux) && defined(__i386) && defined(__GNUC__)
113static const CallUnrollInfo callunrollinfo[] = {
114  // Entry to a function:  push %ebp;  mov  %esp,%ebp
115  // Top-of-stack contains the caller IP.
116  { 0,
117    {0x55, 0x89, 0xe5}, 3,
118    0
119  },
120  // Entry to a function, second instruction:  push %ebp;  mov  %esp,%ebp
121  // Top-of-stack contains the old frame, caller IP is +4.
122  { -1,
123    {0x55, 0x89, 0xe5}, 3,
124    4
125  },
126  // Return from a function: RET.
127  // Top-of-stack contains the caller IP.
128  { 0,
129    {0xc3}, 1,
130    0
131  }
132};
133
134inline void* GetPC(const ucontext_t& signal_ucontext) {
135  // See comment above struct CallUnrollInfo.  Only try instruction
136  // flow matching if both eip and esp looks reasonable.
137  const int eip = signal_ucontext.uc_mcontext.gregs[REG_EIP];
138  const int esp = signal_ucontext.uc_mcontext.gregs[REG_ESP];
139  if ((eip & 0xffff0000) != 0 && (~eip & 0xffff0000) != 0 &&
140      (esp & 0xffff0000) != 0) {
141    char* eip_char = reinterpret_cast<char*>(eip);
142    for (int i = 0; i < sizeof(callunrollinfo)/sizeof(*callunrollinfo); ++i) {
143      if (!memcmp(eip_char + callunrollinfo[i].pc_offset,
144                  callunrollinfo[i].ins, callunrollinfo[i].ins_size)) {
145        // We have a match.
146        void **retaddr = (void**)(esp + callunrollinfo[i].return_sp_offset);
147        return *retaddr;
148      }
149    }
150  }
151  return (void*)eip;
152}
153
154// Special case #2: Windows, which has to do something totally different.
155#elif defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__MINGW32__)
156// If this is ever implemented, probably the way to do it is to have
157// profiler.cc use a high-precision timer via timeSetEvent:
158//    http://msdn2.microsoft.com/en-us/library/ms712713.aspx
159// We'd use it in mode TIME_CALLBACK_FUNCTION/TIME_PERIODIC.
160// The callback function would be something like prof_handler, but
161// alas the arguments are different: no ucontext_t!  I don't know
162// how we'd get the PC (using StackWalk64?)
163//    http://msdn2.microsoft.com/en-us/library/ms680650.aspx
164
165#include "base/logging.h"   // for RAW_LOG
166#ifndef HAVE_CYGWIN_SIGNAL_H
167typedef int ucontext_t;
168#endif
169
170inline void* GetPC(const struct ucontext_t& signal_ucontext) {
171  RAW_LOG(ERROR, "GetPC is not yet implemented on Windows\n");
172  return NULL;
173}
174
175// Normal cases.  If this doesn't compile, it's probably because
176// PC_FROM_UCONTEXT is the empty string.  You need to figure out
177// the right value for your system, and add it to the list in
178// configure.ac (or set it manually in your config.h).
179#else
180inline void* GetPC(const ucontext_t& signal_ucontext) {
181  return (void*)signal_ucontext.PC_FROM_UCONTEXT;   // defined in config.h
182}
183
184#endif
185
186#endif  // BASE_GETPC_H_
187