~ubuntu-branches/ubuntu/trusty/hddtemp/trusty

« back to all changes in this revision

Viewing changes to src/backtrace.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno
  • Date: 2011-06-05 00:18:46 UTC
  • Revision ID: james.westby@ubuntu.com-20110605001846-zf5c529j1b0n62zz
Tags: 0.3-beta15-48
* Fix backtrace support on i386.
* Add backtrace support on amd64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
17
 */
18
18
 
 
19
#define _GNU_SOURCE
19
20
#include <features.h>
20
21
 
21
 
#if defined(__i386__) && defined(__GLIBC__)
 
22
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GLIBC__)
22
23
 
23
24
#include <execinfo.h>
24
25
 
31
32
#include <bits/sigcontext.h>
32
33
#include <sys/param.h>
33
34
 
34
 
#define __USE_GNU
35
35
#include <ucontext.h>
 
36
#if defined(__i386__)
 
37
#  define REG_IP REG_EIP
 
38
#elif defined(__x86_64__)
 
39
#  define REG_IP REG_RIP
 
40
#endif
36
41
 
37
42
#define MAX_BTSIZE 64
38
43
 
42
47
  static char **messages = NULL;
43
48
  static size_t btsize = 0;
44
49
  static size_t i;
45
 
  static unsigned int old_eip, old_ebp;
46
50
  static char *strerr = "???";
47
51
  static FILE *fstrm;
48
52
 
61
65
      SIC_CASE(SEGV_MAPERR);
62
66
      SIC_CASE(SEGV_ACCERR);
63
67
    }
64
 
    fprintf(fstrm, "=== SEGFAULT (%s) : invalid access to %p, in 0x%08x\n",
 
68
    fprintf(fstrm, "=== SEGFAULT (%s) : invalid access to %p, in %p\n",
65
69
            strerr,
66
70
            ist->si_addr,
67
 
            puc->uc_mcontext.gregs[REG_EIP]);
 
71
            (void*)puc->uc_mcontext.gregs[REG_IP]);
68
72
    break;
69
73
  case SIGILL:
70
74
    switch(ist->si_code) {
77
81
      SIC_CASE(ILL_COPROC);
78
82
      SIC_CASE(ILL_BADSTK);
79
83
    }
80
 
    fprintf(fstrm, "=== ILLEGAL INSTR (%s) : invalid access to %p, in 0x%08x\n",
 
84
    fprintf(fstrm, "=== ILLEGAL INSTR (%s) : invalid access to %p, in %p\n",
81
85
            strerr,
82
86
            ist->si_addr,
83
 
            puc->uc_mcontext.gregs[REG_EIP]);
 
87
            (void*)puc->uc_mcontext.gregs[REG_IP]);
84
88
    break;
85
89
  case SIGBUS:
86
90
    switch(ist->si_code) {
88
92
      SIC_CASE(BUS_ADRERR);
89
93
      SIC_CASE(BUS_OBJERR);
90
94
    }
91
 
    fprintf(fstrm, "=== BUS ERROR (%p) : invalid access to %p, in 0x%08x\n",
 
95
    fprintf(fstrm, "=== BUS ERROR (%p) : invalid access to %p, in %p\n",
92
96
            strerr,
93
97
            ist->si_addr,
94
 
            puc->uc_mcontext.gregs[REG_EIP]);
 
98
            (void*)puc->uc_mcontext.gregs[REG_IP]);
95
99
    break;
96
100
  }
97
101
  fflush(fstrm);
101
105
  /*
102
106
    old_eip = *(unsigned int*)((void*)&n-4);
103
107
    old_ebp = *(unsigned int*)((void*)&n-8);
104
 
    *(unsigned int*)((void*)&n-4) = puc->uc_mcontext.gregs[REG_EIP];
 
108
    *(unsigned int*)((void*)&n-4) = puc->uc_mcontext.gregs[REG_IP];
105
109
    *(unsigned int*)((void*)&n-8) = puc->uc_mcontext.gregs[REG_EBP];    
106
110
    
107
111
    btsize = backtrace(btinfo, MAX_BTSIZE);
111
115
  */
112
116
  
113
117
  btsize = backtrace(btinfo, MAX_BTSIZE);
114
 
  btinfo[1] = (void*) puc->uc_mcontext.gregs[REG_EIP];
 
118
  btinfo[1] = (void*) puc->uc_mcontext.gregs[REG_IP];
115
119
 
116
120
  messages = backtrace_symbols(btinfo, btsize);
117
121
 
118
122
  for(i = 1;
119
123
      i < btsize;
120
124
      i++)
121
 
    fprintf(fstrm, "[%d] #%d: %s\n", getpid(), i, messages[i]);
 
125
    fprintf(fstrm, "[%d] #%zu: %s\n", getpid(), i, messages[i]);
122
126
  fflush(fstrm);
123
127
  fclose(fstrm);
124
128