| 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 | // On many linux systems, we may need _GNU_SOURCE to get access to |
| 47 | // the defined constants that define the register we want to see (eg |
| 48 | // REG_EIP). Note this #define must come first! |
| 49 | #define _GNU_SOURCE 1 |
| 50 | // If #define _GNU_SOURCE causes problems, this might work instead. |
| 51 | // It will cause problems for FreeBSD though!, because it turns off |
| 52 | // the needed __BSD_VISIBLE. |
| 53 | #ifdef __APPLE__ |
| 54 | #include <limits.h> |
| 55 | #define _XOPEN_SOURCE 500 |
| 56 | #endif |
| 57 | |
| 58 | #include "vmprof_config.h" |
| 59 | |
| 60 | #include <string.h> // for memcmp |
| 61 | #if defined(HAVE_SYS_UCONTEXT_H) |
| 62 | #include <sys/ucontext.h> |
| 63 | #elif defined(HAVE_UCONTEXT_H) |
| 64 | #include <ucontext.h> // for ucontext_t (and also mcontext_t) |
| 65 | #elif defined(HAVE_CYGWIN_SIGNAL_H) |
| 66 | #include <cygwin/signal.h> |
| 67 | typedef ucontext ucontext_t; |
| 68 | #elif defined(HAVE_SIGNAL_H) |
| 69 | #include <signal.h> |
| 70 | #else |
| 71 | # error "don't know how to get the pc on this platform" |
| 72 | #endif |
| 73 | |
| 74 | |
| 75 | // Take the example where function Foo() calls function Bar(). For |
| 76 | // many architectures, Bar() is responsible for setting up and tearing |
| 77 | // down its own stack frame. In that case, it's possible for the |
| 78 | // interrupt to happen when execution is in Bar(), but the stack frame |
| 79 | // is not properly set up (either before it's done being set up, or |
| 80 | // after it's been torn down but before Bar() returns). In those |
| 81 | // cases, the stack trace cannot see the caller function anymore. |
| 82 | // |
| 83 | // GetPC can try to identify this situation, on architectures where it |
| 84 | // might occur, and unwind the current function call in that case to |
| 85 | // avoid false edges in the profile graph (that is, edges that appear |
| 86 | // to show a call skipping over a function). To do this, we hard-code |
| 87 | // in the asm instructions we might see when setting up or tearing |
| 88 | // down a stack frame. |
| 89 | // |
| 90 | // This is difficult to get right: the instructions depend on the |
| 91 | // processor, the compiler ABI, and even the optimization level. This |
| 92 | // is a best effort patch -- if we fail to detect such a situation, or |
| 93 | // mess up the PC, nothing happens; the returned PC is not used for |
| 94 | // any further processing. |
| 95 | struct CallUnrollInfo { |
| 96 | // Offset from (e)ip register where this instruction sequence |
| 97 | // should be matched. Interpreted as bytes. Offset 0 is the next |
| 98 | // instruction to execute. Be extra careful with negative offsets in |
| 99 | // architectures of variable instruction length (like x86) - it is |
| 100 | // not that easy as taking an offset to step one instruction back! |
| 101 | int pc_offset; |
| 102 | // The actual instruction bytes. Feel free to make it larger if you |
| 103 | // need a longer sequence. |
| 104 | unsigned char ins[16]; |
| 105 | // How many bytes to match from ins array? |
| 106 | int ins_size; |
| 107 | // The offset from the stack pointer (e)sp where to look for the |
| 108 | // call return address. Interpreted as bytes. |
| 109 | int return_sp_offset; |
| 110 | }; |
| 111 | |
| 112 | |
| 113 | // The dereferences needed to get the PC from a struct ucontext were |
| 114 | // determined at configure time, and stored in the macro |
| 115 | // PC_FROM_UCONTEXT in config.h. The only thing we need to do here, |
| 116 | // then, is to do the magic call-unrolling for systems that support it. |
| 117 | |
| 118 | // Special case Windows, which has to do something totally different. |
| 119 | #if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__MINGW32__) |
| 120 | // If this is ever implemented, probably the way to do it is to have |
| 121 | // profiler.cc use a high-precision timer via timeSetEvent: |
| 122 | // http://msdn2.microsoft.com/en-us/library/ms712713.aspx |
| 123 | // We'd use it in mode TIME_CALLBACK_FUNCTION/TIME_PERIODIC. |
| 124 | // The callback function would be something like prof_handler, but |
| 125 | // alas the arguments are different: no ucontext_t! I don't know |
| 126 | // how we'd get the PC (using StackWalk64?) |
| 127 | // http://msdn2.microsoft.com/en-us/library/ms680650.aspx |
| 128 | |
| 129 | // #include "base/logging.h" // for RAW_LOG |
| 130 | // #ifndef HAVE_CYGWIN_SIGNAL_H |
| 131 | // typedef int ucontext_t; |
| 132 | // #endif |
| 133 | |
| 134 | static intptr_t GetPC(ucontext_t *signal_ucontext) { |
| 135 | // RAW_LOG(ERROR, "GetPC is not yet implemented on Windows\n"); |
| 136 | fprintf(stderr, "GetPC is not yet implemented on Windows\n"); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | // Normal cases. If this doesn't compile, it's probably because |
| 141 | // PC_FROM_UCONTEXT is the empty string. You need to figure out |
| 142 | // the right value for your system, and add it to the list in |
| 143 | // vmrpof_config.h |
| 144 | #else |
| 145 | |
| 146 | static intptr_t GetPC(ucontext_t *signal_ucontext) { |
| 147 | return signal_ucontext->PC_FROM_UCONTEXT; // defined in config.h |
| 148 | } |
| 149 | |
| 150 | #endif |
| 151 | |
| 152 | #endif // BASE_GETPC_H_ |