HPCToolkit
x86-unwind-interval.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 #include <string.h>
48 #include <stdio.h>
49 #include <assert.h>
50 #include <stdbool.h>
51 
52 #include <memory/hpcrun-malloc.h>
53 #include <hpcrun/hpcrun_stats.h>
54 #include "x86-unwind-interval.h"
55 
56 #include <messages/messages.h>
57 
58 #define STR(s) case s: return #s
59 
60 
61 
62 /*************************************************************************************
63  * forward declarations
64  ************************************************************************************/
65 
66 static const char *ra_status_string(ra_loc l);
67 static const char *bp_status_string(bp_loc l);
68 
69 
70 /*************************************************************************************
71  * interface operations
72  ************************************************************************************/
73 
74 void
76 {
77  TMSG(SUSPICIOUS_INTERVAL,"suspicious interval for pc = %p", pc);
79 }
80 
82 new_ui(char *start, ra_loc ra_status, const x86registers_t *reg)
83 {
85 
86  // DXN: what is this?
87 # include "mem_error_gen.h" // **** SPECIAL PURPOSE CODE TO INDUCE MEM FAILURE (conditionally included) ***
88 
90 
91  uwi_t *uwi = bitree_uwi_rootval(u);
92 
93  uwi->interval.start = (uintptr_t)start;
94 
95  x86recipe_t* x86recipe = (x86recipe_t*) uwi->recipe;
96  x86recipe->ra_status = ra_status;
97  x86recipe->reg = *reg;
98 
99  x86recipe->prev_canonical = NULL;
100  x86recipe->has_tail_calls = false;
101 
102  return u;
103 }
104 
105 
106 void
108 {
109  UWI_RECIPE(u)->prev_canonical = value;
110 }
111 
113 fluke_ui(char *loc, unsigned int pos)
114 {
116  uwi_t *uwi = bitree_uwi_rootval(u);
117 
118  uwi->interval.start = (uintptr_t)loc;
119  uwi->interval.end = (uintptr_t)loc;
120 
121  x86recipe_t* x86recipe = (x86recipe_t*) uwi->recipe;
122  x86recipe->ra_status = RA_SP_RELATIVE;
123  x86recipe->reg.sp_ra_pos = pos;
124  x86recipe->reg.bp_ra_pos = 0;
125  x86recipe->reg.bp_status = 0;
126  x86recipe->reg.sp_bp_pos = 0;
127  x86recipe->reg.bp_bp_pos = 0;
128  x86recipe->prev_canonical = NULL;
129  x86recipe->has_tail_calls = false;
130 
131  return u;
132 }
133 
134 void
136 {
137  UWI_END_ADDR(current) = UWI_START_ADDR(next);
138  bitree_uwi_set_rightsubtree(current, next);
139 }
140 
141 static void
142 dump_ui_str(unwind_interval *u, char *buf, size_t len)
143 {
144  x86recipe_t *xr = UWI_RECIPE(u);
145  x86registers_t reg = xr->reg;
146  snprintf(buf, len, "UWI: [%8p, %8p) "
147  "ra_status=%14s sp_ra_pos=%4d sp_bp_pos=%4d "
148  "bp_status=%12s bp_ra_pos=%4d bp_bp_pos=%4d "
149  "next=%14p prev_canon=%14p tail_call=%d\n",
150  (void *) UWI_START_ADDR(u), (void *) UWI_END_ADDR(u),
153  UWI_NEXT(u), xr->prev_canonical, UWI_RECIPE(u)->has_tail_calls);
154 }
155 
156 
157 void
159 {
160  char buf[1000];
161 
162  dump_ui_str(u, buf, sizeof(buf));
163 
164  EMSG(buf);
165 }
166 
167 void
168 dump_ui(unwind_interval *u, int dump_to_stderr)
169 {
170  if ( ! ENABLED(UNW) ) {
171  return;
172  }
173 
174  char buf[1000];
175  dump_ui_str(u, buf, sizeof(buf));
176 
177  TMSG(UNW, buf);
178  if (dump_to_stderr) {
179  fprintf(stderr, "%s", buf);
180  fflush(stderr);
181  }
182 }
183 
184 void
186 {
187  char buf[1000];
188 
189  dump_ui_str(u, buf, sizeof(buf));
190 
191  EEMSG(buf);
192 }
193 
194 void
196 {
197  char buf[1000];
198 
199  dump_ui_str(u, buf, sizeof(buf));
200 
201  fprintf(stderr,"%s", buf);
202  fflush(stderr);
203 }
204 
205 void
207 {
208  char buf[1000];
209 
210  dump_ui_str(u, buf, sizeof(buf));
211 
212  TMSG(TROLL,buf);
213 }
214 
215 void
216 x86recipe_tostr(void* recipe, char str[])
217 {
218  // TODO
219  x86recipe_t* x86recipe = (x86recipe_t*)recipe;
220  snprintf(str, MAX_RECIPE_STR, "%s%d%s%s",
221  "x86recipe ", x86recipe->reg.sp_ra_pos,
222  ": tail_call = ", x86recipe->has_tail_calls? "true": "false");
223 }
224 
225 void
226 x86recipe_print(void* recipe)
227 {
228  char str[MAX_RECIPE_STR];
229  x86recipe_tostr(recipe, str);
230  printf("%s", str);
231 }
232 
233 /*
234  * concrete implementation of the abstract function for printing an abstract
235  * unwind recipe specified in uw_recipe.h
236  */
237 void
238 uw_recipe_tostr(void* recipe, char str[], unwinder_t uw)
239 {
240  if (uw == NATIVE_UNWINDER)
241  x86recipe_tostr((x86recipe_t*)recipe, str);
242  else
243  libunw_uw_recipe_tostr(recipe, str);
244 }
245 
246 void
247 uw_recipe_print(void* recipe)
248 {
249  x86recipe_print((x86recipe_t*)recipe);
250 }
251 
252 
253 /*************************************************************************************
254  * private operations
255  ************************************************************************************/
256 
257 static const char *
259 {
260  switch(l) {
262  STR(RA_STD_FRAME);
263  STR(RA_BP_FRAME);
264  STR(RA_REGISTER);
265  STR(POISON);
266  default:
267  assert(0);
268  }
269  return NULL;
270 }
271 
272 static const char *
274 {
275  switch(l){
276  STR(BP_UNCHANGED);
277  STR(BP_SAVED);
278  STR(BP_HOSED);
279  default:
280  assert(0);
281  }
282 }
283 
unwind_interval * new_ui(char *start, ra_loc ra_status, const x86registers_t *reg)
void dump_ui_troll(unwind_interval *u)
void link_ui(unwind_interval *current, unwind_interval *next)
uwi_t * bitree_uwi_rootval(bitree_uwi_t *tree)
void dump_ui_log(unwind_interval *u)
void uw_recipe_print(void *recipe)
void dump_ui_dbg(unwind_interval *u)
void suspicious_interval(void *pc)
uintptr_t end
Definition: interval_t.h:28
Definition: fmt.c:108
static const char * bp_status_string(bp_loc l)
#define UWI_RECIPE(btuwi)
x86registers_t reg
bitree_uwi_t * bitree_uwi_malloc(unwinder_t uw, size_t recipe_size)
interval_t interval
static void dump_ui_str(unwind_interval *u, char *buf, size_t len)
void hpcrun_stats_num_unwind_intervals_total_inc(void)
Definition: hpcrun_stats.c:239
#define EMSG
Definition: messages.h:70
struct bitree_uwi_s bitree_uwi_t
#define MAX_RECIPE_STR
#define UWI_NEXT(btuwi)
void dump_ui(unwind_interval *u, int dump_to_stderr)
#define TMSG(f,...)
Definition: messages.h:93
#define UWI_END_ADDR(btuwi)
void dump_ui_stderr(unwind_interval *u)
char recipe[]
void uw_recipe_tostr(void *recipe, char str[], unwinder_t uw)
unwind_interval * fluke_ui(char *loc, unsigned int pos)
#define UWI_START_ADDR(btuwi)
#define EEMSG(...)
Definition: messages.h:90
#define NULL
Definition: ElfHelper.cpp:85
static const char * ra_status_string(ra_loc l)
void x86recipe_print(void *recipe)
bitree_uwi_t unwind_interval
enum unwinder_e unwinder_t
void set_ui_canonical(unwind_interval *u, unwind_interval *value)
void x86recipe_tostr(void *recipe, char str[])
bitree_uwi_t * prev_canonical
uintptr_t start
Definition: interval_t.h:27
void hpcrun_stats_num_unwind_intervals_suspicious_inc(void)
Definition: hpcrun_stats.c:258
#define STR(s)
void libunw_uw_recipe_tostr(void *uwr, char str[])
void bitree_uwi_set_rightsubtree(bitree_uwi_t *tree, bitree_uwi_t *subtree)
#define ENABLED(f)
Definition: debug-flag.h:76