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