1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of libunwind.
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 #include "libunwind_i.h"
32 free_regions (unw_dyn_region_info_t
*region
)
35 free_regions (region
->next
);
40 intern_op (unw_addr_space_t as
, unw_accessors_t
*a
, unw_word_t
*addr
,
41 unw_dyn_op_t
*op
, void *arg
)
45 if ((ret
= fetch8 (as
, a
, addr
, &op
->tag
, arg
)) < 0
46 || (ret
= fetch8 (as
, a
, addr
, &op
->qp
, arg
)) < 0
47 || (ret
= fetch16 (as
, a
, addr
, &op
->reg
, arg
)) < 0
48 || (ret
= fetch32 (as
, a
, addr
, &op
->when
, arg
)) < 0
49 || (ret
= fetchw (as
, a
, addr
, &op
->val
, arg
)) < 0)
55 intern_regions (unw_addr_space_t as
, unw_accessors_t
*a
,
56 unw_word_t
*addr
, unw_dyn_region_info_t
**regionp
, void *arg
)
58 uint32_t insn_count
, op_count
, i
;
59 unw_dyn_region_info_t
*region
;
66 return 0; /* NULL region-list */
68 if ((ret
= fetchw (as
, a
, addr
, &next_addr
, arg
)) < 0
69 || (ret
= fetch32 (as
, a
, addr
, (int32_t *) &insn_count
, arg
)) < 0
70 || (ret
= fetch32 (as
, a
, addr
, (int32_t *) &op_count
, arg
)) < 0)
73 region
= calloc (1, _U_dyn_region_info_size (op_count
));
80 region
->insn_count
= insn_count
;
81 region
->op_count
= op_count
;
82 for (i
= 0; i
< op_count
; ++i
)
83 if ((ret
= intern_op (as
, a
, addr
, region
->op
+ i
, arg
)) < 0)
87 if ((ret
= intern_regions (as
, a
, &next_addr
, ®ion
->next
, arg
)) < 0)
95 free_regions (region
);
100 intern_array (unw_addr_space_t as
, unw_accessors_t
*a
,
101 unw_word_t
*addr
, unw_word_t table_len
, unw_word_t
**table_data
,
104 unw_word_t i
, *data
= calloc (table_len
, WSIZE
);
113 for (i
= 0; i
< table_len
; ++i
)
114 if (fetchw (as
, a
, addr
, data
+ i
, arg
) < 0)
127 free_dyn_info (unw_dyn_info_t
*di
)
131 case UNW_INFO_FORMAT_DYNAMIC
:
132 if (di
->u
.pi
.regions
)
134 free_regions (di
->u
.pi
.regions
);
135 di
->u
.pi
.regions
= NULL
;
139 case UNW_INFO_FORMAT_TABLE
:
140 if (di
->u
.ti
.table_data
)
142 free (di
->u
.ti
.table_data
);
143 di
->u
.ti
.table_data
= NULL
;
147 case UNW_INFO_FORMAT_REMOTE_TABLE
:
154 intern_dyn_info (unw_addr_space_t as
, unw_accessors_t
*a
,
155 unw_word_t
*addr
, unw_dyn_info_t
*di
, void *arg
)
157 unw_word_t first_region
;
162 case UNW_INFO_FORMAT_DYNAMIC
:
163 if ((ret
= fetchw (as
, a
, addr
, &di
->u
.pi
.name_ptr
, arg
)) < 0
164 || (ret
= fetchw (as
, a
, addr
, &di
->u
.pi
.handler
, arg
)) < 0
165 || (ret
= fetch32 (as
, a
, addr
,
166 (int32_t *) &di
->u
.pi
.flags
, arg
)) < 0)
168 *addr
+= 4; /* skip over pad0 */
169 if ((ret
= fetchw (as
, a
, addr
, &first_region
, arg
)) < 0
170 || (ret
= intern_regions (as
, a
, &first_region
, &di
->u
.pi
.regions
,
175 case UNW_INFO_FORMAT_TABLE
:
176 if ((ret
= fetchw (as
, a
, addr
, &di
->u
.ti
.name_ptr
, arg
)) < 0
177 || (ret
= fetchw (as
, a
, addr
, &di
->u
.ti
.segbase
, arg
)) < 0
178 || (ret
= fetchw (as
, a
, addr
, &di
->u
.ti
.table_len
, arg
)) < 0
179 || (ret
= intern_array (as
, a
, addr
, di
->u
.ti
.table_len
,
180 &di
->u
.ti
.table_data
, arg
)) < 0)
184 case UNW_INFO_FORMAT_REMOTE_TABLE
:
185 if ((ret
= fetchw (as
, a
, addr
, &di
->u
.rti
.name_ptr
, arg
)) < 0
186 || (ret
= fetchw (as
, a
, addr
, &di
->u
.rti
.segbase
, arg
)) < 0
187 || (ret
= fetchw (as
, a
, addr
, &di
->u
.rti
.table_len
, arg
)) < 0
188 || (ret
= fetchw (as
, a
, addr
, &di
->u
.rti
.table_data
, arg
)) < 0)
204 unwi_dyn_remote_find_proc_info (unw_addr_space_t as
, unw_word_t ip
,
206 int need_unwind_info
, void *arg
)
208 unw_accessors_t
*a
= unw_get_accessors (as
);
209 unw_word_t dyn_list_addr
, addr
, next_addr
, gen1
, gen2
, start_ip
, end_ip
;
210 unw_dyn_info_t
*di
= NULL
;
213 if (as
->dyn_info_list_addr
)
214 dyn_list_addr
= as
->dyn_info_list_addr
;
217 if ((*a
->get_dyn_info_list_addr
) (as
, &dyn_list_addr
, arg
) < 0)
219 if (as
->caching_policy
!= UNW_CACHE_NONE
)
220 as
->dyn_info_list_addr
= dyn_list_addr
;
225 addr
= dyn_list_addr
;
229 if (fetchw (as
, a
, &addr
, &gen1
, arg
) < 0
230 || fetchw (as
, a
, &addr
, &next_addr
, arg
) < 0)
233 for (addr
= next_addr
; addr
!= 0; addr
= next_addr
)
235 if (fetchw (as
, a
, &addr
, &next_addr
, arg
) < 0)
236 goto recheck
; /* only fail if generation # didn't change */
238 addr
+= WSIZE
; /* skip over prev_addr */
240 if (fetchw (as
, a
, &addr
, &start_ip
, arg
) < 0
241 || fetchw (as
, a
, &addr
, &end_ip
, arg
) < 0)
242 goto recheck
; /* only fail if generation # didn't change */
244 if (ip
>= start_ip
&& ip
< end_ip
)
247 di
= calloc (1, sizeof (*di
));
249 di
->start_ip
= start_ip
;
252 if (fetchw (as
, a
, &addr
, &di
->gp
, arg
) < 0
253 || fetch32 (as
, a
, &addr
, &di
->format
, arg
) < 0)
254 goto recheck
; /* only fail if generation # didn't change */
256 addr
+= 4; /* skip over padding */
259 && intern_dyn_info (as
, a
, &addr
, di
, arg
) < 0)
260 goto recheck
; /* only fail if generation # didn't change */
262 if (unwi_extract_dynamic_proc_info (as
, ip
, pi
, di
,
263 need_unwind_info
, arg
) < 0)
266 goto recheck
; /* only fail if generation # didn't change */
268 ret
= 0; /* OK, found it */
273 /* Re-check generation number to ensure the data we have is
276 addr
= dyn_list_addr
;
277 if (fetchw (as
, a
, &addr
, &gen2
, arg
) < 0)
280 while (gen1
!= gen2
);
289 unwi_dyn_remote_put_unwind_info (unw_addr_space_t as
, unw_proc_info_t
*pi
,
292 if (!pi
->unwind_info
)
295 free_dyn_info (pi
->unwind_info
);
296 free (pi
->unwind_info
);
297 pi
->unwind_info
= NULL
;
300 /* Returns 1 if the cache is up-to-date or -1 if the cache contained
301 stale data and had to be flushed. */
304 unwi_dyn_validate_cache (unw_addr_space_t as
, void *arg
)
306 unw_word_t addr
, gen
;
309 if (!as
->dyn_info_list_addr
)
310 /* If we don't have the dyn_info_list_addr, we don't have anything
314 a
= unw_get_accessors (as
);
315 addr
= as
->dyn_info_list_addr
;
317 if (fetchw (as
, a
, &addr
, &gen
, arg
) < 0)
320 if (gen
== as
->dyn_generation
)
323 unw_flush_cache (as
, 0, 0);
324 as
->dyn_generation
= gen
;