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