~ubuntu-branches/ubuntu/jaunty/google-perftools/jaunty

« back to all changes in this revision

Viewing changes to src/getpc.h

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2008-06-15 23:41:36 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20080615234136-al5gawvdvt5vhdtz
Tags: 0.98-1
* New upstream release. (Closes: #425147)
* Compiled with GCC 4.3. (Closes: #454841)
* debian/watch: can now report upstream's version (Closes: #450294)
* Because of a file conflict between tau and libgoogle-perftools the
  binary pprof is renamed as google-pprof. (Closes: #404001)
  Great thanks to Michael Mende.
* debian/rules: autoconf files are now generated at the build time.
* Bumped up Standards-Version to 3.7.3, no changes are required.
* Split a new package, libtcmallc_minimal0. The upstream supports
  this module for wider platforms. So I leave its architecture to be
  `any'.
* libgoogle-perftools0's architecture is now i386. The upstream
  supports this module for x86 and x86_64. However, x86_64 requires
  libunwind's development head, which Debian does not have yet.
* Removed an unnecessary patch, debian/patches/02_profiler.cc_alpha.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifdef HAVE_UCONTEXT_H
 
59
#include <ucontext.h>       // for ucontext_t (and also mcontext_t)
 
60
#endif
 
61
 
 
62
 
 
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.
 
70
//
 
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.
 
77
//
 
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!
 
89
  int pc_offset;
 
90
  // The actual instruction bytes. Feel free to make it larger if you
 
91
  // need a longer sequence.
 
92
  char ins[16];
 
93
  // How many bytes to match from ins array?
 
94
  int ins_size;
 
95
  // The offset from the stack pointer (e)sp where to look for the
 
96
  // call return address. Interpreted as bytes.
 
97
  int return_sp_offset;
 
98
};
 
99
 
 
100
 
 
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.
 
105
 
 
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.
 
111
  { 0,
 
112
    {0x55, 0x89, 0xe5}, 3,
 
113
    0
 
114
  },
 
115
  // Entry to a function, second instruction:  push %ebp;  mov  %esp,%ebp
 
116
  // Top-of-stack contains the old frame, caller IP is +4.
 
117
  { -1,
 
118
    {0x55, 0x89, 0xe5}, 3,
 
119
    4
 
120
  },
 
121
  // Return from a function: RET.
 
122
  // Top-of-stack contains the caller IP.
 
123
  { 0,
 
124
    {0xc3}, 1,
 
125
    0
 
126
  }
 
127
};
 
128
 
 
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)) {
 
140
        // We have a match.
 
141
        void **retaddr = (void**)(esp + callunrollinfo[i].return_sp_offset);
 
142
        return *retaddr;
 
143
      }
 
144
    }
 
145
  }
 
146
  return (void*)eip;
 
147
}
 
148
 
 
149
// Special case #2: Windows, which has to do something totally different.
 
150
#elif defined(WIN32)
 
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
 
159
 
 
160
#include "base/logging.h"   // for RAW_LOG
 
161
 
 
162
inline void* GetPC(const struct ucontext_t& signal_ucontext) {
 
163
  RAW_LOG(ERROR, "GetPC is not yet implemented on Windows\n");
 
164
  return NULL;
 
165
}
 
166
 
 
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).
 
171
#else
 
172
inline void* GetPC(const ucontext_t& signal_ucontext) {
 
173
  return (void*)signal_ucontext.PC_FROM_UCONTEXT;   // defined in config.h
 
174
}
 
175
 
 
176
#endif
 
177
 
 
178
#endif  // BASE_GETPC_H_