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 verifies that GetPC works correctly. This test uses a minimum 34// of Google infrastructure, to make it very easy to port to various 35// O/Ses and CPUs and test that GetPC is working. 36 37#include "config.h" 38#include "getpc.h" // should be first to get the _GNU_SOURCE dfn 39#include <stdio.h> 40#include <stdlib.h> 41#include <signal.h> 42#include <sys/time.h> // for setitimer 43 44// Needs to be volatile so compiler doesn't try to optimize it away 45static volatile void* getpc_retval = NULL; // what GetPC returns 46static volatile bool prof_handler_called = false; 47 48static void prof_handler(int sig, siginfo_t*, void* signal_ucontext) { 49 if (!prof_handler_called) 50 getpc_retval = GetPC(*reinterpret_cast<ucontext_t*>(signal_ucontext)); 51 prof_handler_called = true; // only store the retval once 52} 53 54static void RoutineCallingTheSignal() { 55 struct sigaction sa; 56 sa.sa_sigaction = prof_handler; 57 sa.sa_flags = SA_RESTART | SA_SIGINFO; 58 sigemptyset(&sa.sa_mask); 59 if (sigaction(SIGPROF, &sa, NULL) != 0) { 60 perror("sigaction"); 61 exit(1); 62 } 63 64 struct itimerval timer; 65 timer.it_interval.tv_sec = 0; 66 timer.it_interval.tv_usec = 1000; 67 timer.it_value = timer.it_interval; 68 setitimer(ITIMER_PROF, &timer, 0); 69 70 // Now we need to do some work for a while, that doesn't call any 71 // other functions, so we can be guaranteed that when the SIGPROF 72 // fires, we're the routine executing. 73 int r = 0; 74 for (int i = 0; !prof_handler_called; ++i) { 75 for (int j = 0; j < i; j++) { 76 r ^= i; 77 r <<= 1; 78 r ^= j; 79 r >>= 1; 80 } 81 } 82 83 // Now make sure the above loop doesn't get optimized out 84 srand(r); 85} 86 87// This is an upper bound of how many bytes the instructions for 88// RoutineCallingTheSignal might be. There's probably a more 89// principled way to do this, but I don't know how portable it would be. 90// (The function is 372 bytes when compiled with -g on Mac OS X 10.4. 91// I can imagine it would be even bigger in 64-bit architectures.) 92const int kRoutineSize = 512 * sizeof(void*)/4; // allow 1024 for 64-bit 93 94int main(int argc, char** argv) { 95 RoutineCallingTheSignal(); 96 97 // Annoyingly, C++ disallows casting pointer-to-function to 98 // pointer-to-object, so we use a C-style cast instead. 99 char* expected = (char*)&RoutineCallingTheSignal; 100 char* actual = (char*)getpc_retval; 101 102 // For ia64, ppc64, and parisc64, the function pointer is actually 103 // a struct. For instance, ia64's dl-fptr.h: 104 // struct fdesc { /* An FDESC is a function descriptor. */ 105 // ElfW(Addr) ip; /* code entry point */ 106 // ElfW(Addr) gp; /* global pointer */ 107 // }; 108 // We want the code entry point. 109#if defined(__ia64) || defined(__ppc64) // NOTE: ppc64 is UNTESTED 110 expected = ((char**)expected)[0]; // this is "ip" 111#endif 112 113 if (actual < expected || actual > expected + kRoutineSize) { 114 printf("Test FAILED: actual PC: %p, expected PC: %p\n", actual, expected); 115 return 1; 116 } else { 117 printf("PASS\n"); 118 return 0; 119 } 120} 121