HPCToolkit
backtrace-lite.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 
48 //***************************************************************************
49 // system include files
50 //***************************************************************************
51 
52 #include <stdbool.h>
53 
54 #include <sys/types.h>
55 #include <ucontext.h>
56 
57 
58 //***************************************************************************
59 // local include files
60 //***************************************************************************
61 
62 #include "unwind.h"
63 
64 #include "backtrace.h"
65 #include "epoch.h"
66 #include "monitor.h"
67 #include "sample_event.h"
68 
69 #include <messages/messages.h>
70 
71 //***************************************************************************
72 //
73 //***************************************************************************
74 
75 
76 #if (HPC_UNW_LITE)
77 static int
78 hpcrun_backtrace_lite(void** buffer, int size, ucontext_t* context)
79 {
80  // special trivial case: size == 0 (N.B.: permit size < 0)
81  if (size <= 0) {
82  return 0;
83  }
84 
85  // INVARIANT: 'size' > 0; 'buffer' is non-empty; 'context' is non-NULL
86  if ( !(size > 0 && buffer && context) ) {
87  return -1; // error
88  }
89 
91 
92  hpcrun_unw_cursor_t cursor;
93  hpcrun_unw_init_cursor(&cursor, context);
94 
95  int my_size = 0;
96  while (my_size < size) {
97  int ret;
98 
99  void *ip = 0;
100  ret = hpcrun_unw_get_ip_reg(&cursor, &ip);
101  if (ret < 0) { /* ignore error */ }
102 
103  buffer[my_size] = ip; // my_size < size
104  my_size++;
105 
106  ret = hpcrun_unw_step(&cursor);
107  if (ret <= 0) {
108  // N.B. (ret < 0) indicates an unwind error, which we ignore
109  break;
110  }
111  }
112 
113  return my_size;
114 }
115 #endif
116 
117 
118 #if (HPC_UNW_LITE)
119 static int
120 test_backtrace_lite(ucontext_t* context)
121 {
122  const int bufsz = 100;
123 
124  void* buffer[bufsz];
125  int sz = hpcrun_backtrace_lite(buffer, bufsz, context);
126 
127  for (int i = 0; i < sz; ++i) {
128  TMSG(UNW, "backtrace_lite: pc=%p", buffer[i]);
129  }
130 
131  return sz;
132 }
133 #endif
134 
135 
136 //***************************************************************************
137 //
138 //***************************************************************************
step_state hpcrun_unw_step(hpcrun_unw_cursor_t *c)
#define TMSG(f,...)
Definition: messages.h:93
void hpcrun_unw_init()
int hpcrun_unw_get_ip_reg(hpcrun_unw_cursor_t *cursor, void **val)
void hpcrun_unw_init_cursor(hpcrun_unw_cursor_t *cursor, void *context)