Gdyn-remote.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include <stdlib.h>
  22. #include "libunwind_i.h"
  23. #include "remote.h"
  24. static void
  25. free_regions (unw_dyn_region_info_t *region)
  26. {
  27. if (region->next)
  28. free_regions (region->next);
  29. free (region);
  30. }
  31. static int
  32. intern_op (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
  33. unw_dyn_op_t *op, void *arg)
  34. {
  35. int ret;
  36. if ((ret = fetch8 (as, a, addr, &op->tag, arg)) < 0
  37. || (ret = fetch8 (as, a, addr, &op->qp, arg)) < 0
  38. || (ret = fetch16 (as, a, addr, &op->reg, arg)) < 0
  39. || (ret = fetch32 (as, a, addr, &op->when, arg)) < 0
  40. || (ret = fetchw (as, a, addr, &op->val, arg)) < 0)
  41. return ret;
  42. return 0;
  43. }
  44. static int
  45. intern_regions (unw_addr_space_t as, unw_accessors_t *a,
  46. unw_word_t *addr, unw_dyn_region_info_t **regionp, void *arg)
  47. {
  48. uint32_t insn_count, op_count, i;
  49. unw_dyn_region_info_t *region;
  50. unw_word_t next_addr;
  51. int ret;
  52. *regionp = NULL;
  53. if (!*addr)
  54. return 0; /* NULL region-list */
  55. if ((ret = fetchw (as, a, addr, &next_addr, arg)) < 0
  56. || (ret = fetch32 (as, a, addr, (int32_t *) &insn_count, arg)) < 0
  57. || (ret = fetch32 (as, a, addr, (int32_t *) &op_count, arg)) < 0)
  58. return ret;
  59. region = calloc (1, _U_dyn_region_info_size (op_count));
  60. if (!region)
  61. {
  62. ret = -UNW_ENOMEM;
  63. goto out;
  64. }
  65. region->insn_count = insn_count;
  66. region->op_count = op_count;
  67. for (i = 0; i < op_count; ++i)
  68. if ((ret = intern_op (as, a, addr, region->op + i, arg)) < 0)
  69. goto out;
  70. if (next_addr)
  71. if ((ret = intern_regions (as, a, &next_addr, &region->next, arg)) < 0)
  72. goto out;
  73. *regionp = region;
  74. return 0;
  75. out:
  76. if (region)
  77. free_regions (region);
  78. return ret;
  79. }
  80. static int
  81. intern_array (unw_addr_space_t as, unw_accessors_t *a,
  82. unw_word_t *addr, unw_word_t table_len, unw_word_t **table_data,
  83. void *arg)
  84. {
  85. unw_word_t i, *data = calloc (table_len, WSIZE);
  86. int ret = 0;
  87. if (!data)
  88. {
  89. ret = -UNW_ENOMEM;
  90. goto out;
  91. }
  92. for (i = 0; i < table_len; ++i)
  93. if (fetchw (as, a, addr, data + i, arg) < 0)
  94. goto out;
  95. *table_data = data;
  96. return 0;
  97. out:
  98. if (data)
  99. free (data);
  100. return ret;
  101. }
  102. static void
  103. free_dyn_info (unw_dyn_info_t *di)
  104. {
  105. switch (di->format)
  106. {
  107. case UNW_INFO_FORMAT_DYNAMIC:
  108. if (di->u.pi.regions)
  109. {
  110. free_regions (di->u.pi.regions);
  111. di->u.pi.regions = NULL;
  112. }
  113. break;
  114. case UNW_INFO_FORMAT_TABLE:
  115. if (di->u.ti.table_data)
  116. {
  117. free (di->u.ti.table_data);
  118. di->u.ti.table_data = NULL;
  119. }
  120. break;
  121. case UNW_INFO_FORMAT_REMOTE_TABLE:
  122. default:
  123. break;
  124. }
  125. }
  126. static int
  127. intern_dyn_info (unw_addr_space_t as, unw_accessors_t *a,
  128. unw_word_t *addr, unw_dyn_info_t *di, void *arg)
  129. {
  130. unw_word_t first_region;
  131. int ret;
  132. switch (di->format)
  133. {
  134. case UNW_INFO_FORMAT_DYNAMIC:
  135. if ((ret = fetchw (as, a, addr, &di->u.pi.name_ptr, arg)) < 0
  136. || (ret = fetchw (as, a, addr, &di->u.pi.handler, arg)) < 0
  137. || (ret = fetch32 (as, a, addr,
  138. (int32_t *) &di->u.pi.flags, arg)) < 0)
  139. goto out;
  140. *addr += 4; /* skip over pad0 */
  141. if ((ret = fetchw (as, a, addr, &first_region, arg)) < 0
  142. || (ret = intern_regions (as, a, &first_region, &di->u.pi.regions,
  143. arg)) < 0)
  144. goto out;
  145. break;
  146. case UNW_INFO_FORMAT_TABLE:
  147. if ((ret = fetchw (as, a, addr, &di->u.ti.name_ptr, arg)) < 0
  148. || (ret = fetchw (as, a, addr, &di->u.ti.segbase, arg)) < 0
  149. || (ret = fetchw (as, a, addr, &di->u.ti.table_len, arg)) < 0
  150. || (ret = intern_array (as, a, addr, di->u.ti.table_len,
  151. &di->u.ti.table_data, arg)) < 0)
  152. goto out;
  153. break;
  154. case UNW_INFO_FORMAT_REMOTE_TABLE:
  155. if ((ret = fetchw (as, a, addr, &di->u.rti.name_ptr, arg)) < 0
  156. || (ret = fetchw (as, a, addr, &di->u.rti.segbase, arg)) < 0
  157. || (ret = fetchw (as, a, addr, &di->u.rti.table_len, arg)) < 0
  158. || (ret = fetchw (as, a, addr, &di->u.rti.table_data, arg)) < 0)
  159. goto out;
  160. break;
  161. default:
  162. ret = -UNW_ENOINFO;
  163. goto out;
  164. }
  165. return 0;
  166. out:
  167. free_dyn_info (di);
  168. return ret;
  169. }
  170. HIDDEN int
  171. unwi_dyn_remote_find_proc_info (unw_addr_space_t as, unw_word_t ip,
  172. unw_proc_info_t *pi,
  173. int need_unwind_info, void *arg)
  174. {
  175. unw_accessors_t *a = unw_get_accessors (as);
  176. unw_word_t dyn_list_addr, addr, next_addr, gen1, gen2, start_ip, end_ip;
  177. unw_dyn_info_t *di = NULL;
  178. int ret;
  179. if (as->dyn_info_list_addr)
  180. dyn_list_addr = as->dyn_info_list_addr;
  181. else
  182. {
  183. if ((*a->get_dyn_info_list_addr) (as, &dyn_list_addr, arg) < 0)
  184. return -UNW_ENOINFO;
  185. if (as->caching_policy != UNW_CACHE_NONE)
  186. as->dyn_info_list_addr = dyn_list_addr;
  187. }
  188. do
  189. {
  190. addr = dyn_list_addr;
  191. ret = -UNW_ENOINFO;
  192. if (fetchw (as, a, &addr, &gen1, arg) < 0
  193. || fetchw (as, a, &addr, &next_addr, arg) < 0)
  194. break;
  195. for (addr = next_addr; addr != 0; addr = next_addr)
  196. {
  197. if (fetchw (as, a, &addr, &next_addr, arg) < 0)
  198. goto recheck; /* only fail if generation # didn't change */
  199. addr += WSIZE; /* skip over prev_addr */
  200. if (fetchw (as, a, &addr, &start_ip, arg) < 0
  201. || fetchw (as, a, &addr, &end_ip, arg) < 0)
  202. goto recheck; /* only fail if generation # didn't change */
  203. if (ip >= start_ip && ip < end_ip)
  204. {
  205. if (!di)
  206. {
  207. di = calloc (1, sizeof (*di));
  208. if (!di)
  209. return -UNW_ENOMEM;
  210. }
  211. di->start_ip = start_ip;
  212. di->end_ip = end_ip;
  213. if (fetchw (as, a, &addr, &di->gp, arg) < 0
  214. || fetch32 (as, a, &addr, &di->format, arg) < 0)
  215. goto recheck; /* only fail if generation # didn't change */
  216. addr += 4; /* skip over padding */
  217. if (need_unwind_info
  218. && intern_dyn_info (as, a, &addr, di, arg) < 0)
  219. goto recheck; /* only fail if generation # didn't change */
  220. if (unwi_extract_dynamic_proc_info (as, ip, pi, di,
  221. need_unwind_info, arg) < 0)
  222. {
  223. free_dyn_info (di);
  224. goto recheck; /* only fail if generation # didn't change */
  225. }
  226. ret = 0; /* OK, found it */
  227. break;
  228. }
  229. }
  230. /* Re-check generation number to ensure the data we have is
  231. consistent. */
  232. recheck:
  233. addr = dyn_list_addr;
  234. if (fetchw (as, a, &addr, &gen2, arg) < 0)
  235. break;
  236. }
  237. while (gen1 != gen2);
  238. if (ret < 0 && di)
  239. free (di);
  240. return ret;
  241. }
  242. HIDDEN void
  243. unwi_dyn_remote_put_unwind_info (unw_addr_space_t as, unw_proc_info_t *pi,
  244. void *arg)
  245. {
  246. if (!pi->unwind_info)
  247. return;
  248. free_dyn_info (pi->unwind_info);
  249. free (pi->unwind_info);
  250. pi->unwind_info = NULL;
  251. }
  252. /* Returns 1 if the cache is up-to-date or -1 if the cache contained
  253. stale data and had to be flushed. */
  254. HIDDEN int
  255. unwi_dyn_validate_cache (unw_addr_space_t as, void *arg)
  256. {
  257. unw_word_t addr, gen;
  258. unw_accessors_t *a;
  259. if (!as->dyn_info_list_addr)
  260. /* If we don't have the dyn_info_list_addr, we don't have anything
  261. in the cache. */
  262. return 0;
  263. a = unw_get_accessors (as);
  264. addr = as->dyn_info_list_addr;
  265. if (fetchw (as, a, &addr, &gen, arg) < 0)
  266. return 1;
  267. if (gen == as->dyn_generation)
  268. return 1;
  269. unw_flush_cache (as, 0, 0);
  270. as->dyn_generation = gen;
  271. return -1;
  272. }