addrmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /* addrmap.c --- implementation of address map data structure.
  2. Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "splay-tree.h"
  16. #include "gdbsupport/gdb_obstack.h"
  17. #include "addrmap.h"
  18. #include "gdbsupport/selftest.h"
  19. /* Make sure splay trees can actually hold the values we want to
  20. store in them. */
  21. gdb_static_assert (sizeof (splay_tree_key) >= sizeof (CORE_ADDR *));
  22. gdb_static_assert (sizeof (splay_tree_value) >= sizeof (void *));
  23. /* The "abstract class". */
  24. /* Functions implementing the addrmap functions for a particular
  25. implementation. */
  26. struct addrmap_funcs
  27. {
  28. void (*set_empty) (struct addrmap *self,
  29. CORE_ADDR start, CORE_ADDR end_inclusive,
  30. void *obj);
  31. void *(*find) (struct addrmap *self, CORE_ADDR addr);
  32. struct addrmap *(*create_fixed) (struct addrmap *self,
  33. struct obstack *obstack);
  34. void (*relocate) (struct addrmap *self, CORE_ADDR offset);
  35. int (*foreach) (struct addrmap *self, addrmap_foreach_fn fn);
  36. };
  37. struct addrmap
  38. {
  39. const struct addrmap_funcs *funcs;
  40. };
  41. void
  42. addrmap_set_empty (struct addrmap *map,
  43. CORE_ADDR start, CORE_ADDR end_inclusive,
  44. void *obj)
  45. {
  46. map->funcs->set_empty (map, start, end_inclusive, obj);
  47. }
  48. void *
  49. addrmap_find (struct addrmap *map, CORE_ADDR addr)
  50. {
  51. return map->funcs->find (map, addr);
  52. }
  53. struct addrmap *
  54. addrmap_create_fixed (struct addrmap *original, struct obstack *obstack)
  55. {
  56. return original->funcs->create_fixed (original, obstack);
  57. }
  58. /* Relocate all the addresses in MAP by OFFSET. (This can be applied
  59. to either mutable or immutable maps.) */
  60. void
  61. addrmap_relocate (struct addrmap *map, CORE_ADDR offset)
  62. {
  63. map->funcs->relocate (map, offset);
  64. }
  65. int
  66. addrmap_foreach (struct addrmap *map, addrmap_foreach_fn fn)
  67. {
  68. return map->funcs->foreach (map, fn);
  69. }
  70. /* Fixed address maps. */
  71. /* A transition: a point in an address map where the value changes.
  72. The map maps ADDR to VALUE, but if ADDR > 0, it maps ADDR-1 to
  73. something else. */
  74. struct addrmap_transition
  75. {
  76. CORE_ADDR addr;
  77. void *value;
  78. };
  79. struct addrmap_fixed
  80. {
  81. struct addrmap addrmap;
  82. /* The number of transitions in TRANSITIONS. */
  83. size_t num_transitions;
  84. /* An array of transitions, sorted by address. For every point in
  85. the map where either ADDR == 0 or ADDR is mapped to one value and
  86. ADDR - 1 is mapped to something different, we have an entry here
  87. containing ADDR and VALUE. (Note that this means we always have
  88. an entry for address 0). */
  89. struct addrmap_transition transitions[1];
  90. };
  91. static void
  92. addrmap_fixed_set_empty (struct addrmap *self,
  93. CORE_ADDR start, CORE_ADDR end_inclusive,
  94. void *obj)
  95. {
  96. internal_error (__FILE__, __LINE__,
  97. "addrmap_fixed_set_empty: "
  98. "fixed addrmaps can't be changed\n");
  99. }
  100. static void *
  101. addrmap_fixed_find (struct addrmap *self, CORE_ADDR addr)
  102. {
  103. struct addrmap_fixed *map = (struct addrmap_fixed *) self;
  104. struct addrmap_transition *bottom = &map->transitions[0];
  105. struct addrmap_transition *top = &map->transitions[map->num_transitions - 1];
  106. while (bottom < top)
  107. {
  108. /* This needs to round towards top, or else when top = bottom +
  109. 1 (i.e., two entries are under consideration), then mid ==
  110. bottom, and then we may not narrow the range when (mid->addr
  111. < addr). */
  112. struct addrmap_transition *mid = top - (top - bottom) / 2;
  113. if (mid->addr == addr)
  114. {
  115. bottom = mid;
  116. break;
  117. }
  118. else if (mid->addr < addr)
  119. /* We don't eliminate mid itself here, since each transition
  120. covers all subsequent addresses until the next. This is why
  121. we must round up in computing the midpoint. */
  122. bottom = mid;
  123. else
  124. top = mid - 1;
  125. }
  126. return bottom->value;
  127. }
  128. static struct addrmap *
  129. addrmap_fixed_create_fixed (struct addrmap *self, struct obstack *obstack)
  130. {
  131. internal_error (__FILE__, __LINE__,
  132. _("addrmap_create_fixed is not implemented yet "
  133. "for fixed addrmaps"));
  134. }
  135. static void
  136. addrmap_fixed_relocate (struct addrmap *self, CORE_ADDR offset)
  137. {
  138. struct addrmap_fixed *map = (struct addrmap_fixed *) self;
  139. size_t i;
  140. for (i = 0; i < map->num_transitions; i++)
  141. map->transitions[i].addr += offset;
  142. }
  143. static int
  144. addrmap_fixed_foreach (struct addrmap *self, addrmap_foreach_fn fn)
  145. {
  146. struct addrmap_fixed *map = (struct addrmap_fixed *) self;
  147. size_t i;
  148. for (i = 0; i < map->num_transitions; i++)
  149. {
  150. int res = fn (map->transitions[i].addr, map->transitions[i].value);
  151. if (res != 0)
  152. return res;
  153. }
  154. return 0;
  155. }
  156. static const struct addrmap_funcs addrmap_fixed_funcs =
  157. {
  158. addrmap_fixed_set_empty,
  159. addrmap_fixed_find,
  160. addrmap_fixed_create_fixed,
  161. addrmap_fixed_relocate,
  162. addrmap_fixed_foreach
  163. };
  164. /* Mutable address maps. */
  165. struct addrmap_mutable
  166. {
  167. struct addrmap addrmap;
  168. /* The obstack to use for allocations for this map. */
  169. struct obstack *obstack;
  170. /* A splay tree, with a node for each transition; there is a
  171. transition at address T if T-1 and T map to different objects.
  172. Any addresses below the first node map to NULL. (Unlike
  173. fixed maps, we have no entry at (CORE_ADDR) 0; it doesn't
  174. simplify enough.)
  175. The last region is assumed to end at CORE_ADDR_MAX.
  176. Since we can't know whether CORE_ADDR is larger or smaller than
  177. splay_tree_key (unsigned long) --- I think both are possible,
  178. given all combinations of 32- and 64-bit hosts and targets ---
  179. our keys are pointers to CORE_ADDR values. Since the splay tree
  180. library doesn't pass any closure pointer to the key free
  181. function, we can't keep a freelist for keys. Since mutable
  182. addrmaps are only used temporarily right now, we just leak keys
  183. from deleted nodes; they'll be freed when the obstack is freed. */
  184. splay_tree tree;
  185. /* A freelist for splay tree nodes, allocated on obstack, and
  186. chained together by their 'right' pointers. */
  187. splay_tree_node free_nodes;
  188. };
  189. /* Allocate a copy of CORE_ADDR in MAP's obstack. */
  190. static splay_tree_key
  191. allocate_key (struct addrmap_mutable *map, CORE_ADDR addr)
  192. {
  193. CORE_ADDR *key = XOBNEW (map->obstack, CORE_ADDR);
  194. *key = addr;
  195. return (splay_tree_key) key;
  196. }
  197. /* Type-correct wrappers for splay tree access. */
  198. static splay_tree_node
  199. addrmap_splay_tree_lookup (struct addrmap_mutable *map, CORE_ADDR addr)
  200. {
  201. return splay_tree_lookup (map->tree, (splay_tree_key) &addr);
  202. }
  203. static splay_tree_node
  204. addrmap_splay_tree_predecessor (struct addrmap_mutable *map, CORE_ADDR addr)
  205. {
  206. return splay_tree_predecessor (map->tree, (splay_tree_key) &addr);
  207. }
  208. static splay_tree_node
  209. addrmap_splay_tree_successor (struct addrmap_mutable *map, CORE_ADDR addr)
  210. {
  211. return splay_tree_successor (map->tree, (splay_tree_key) &addr);
  212. }
  213. static void
  214. addrmap_splay_tree_remove (struct addrmap_mutable *map, CORE_ADDR addr)
  215. {
  216. splay_tree_remove (map->tree, (splay_tree_key) &addr);
  217. }
  218. static CORE_ADDR
  219. addrmap_node_key (splay_tree_node node)
  220. {
  221. return * (CORE_ADDR *) node->key;
  222. }
  223. static void *
  224. addrmap_node_value (splay_tree_node node)
  225. {
  226. return (void *) node->value;
  227. }
  228. static void
  229. addrmap_node_set_value (splay_tree_node node, void *value)
  230. {
  231. node->value = (splay_tree_value) value;
  232. }
  233. static void
  234. addrmap_splay_tree_insert (struct addrmap_mutable *map,
  235. CORE_ADDR key, void *value)
  236. {
  237. splay_tree_insert (map->tree,
  238. allocate_key (map, key),
  239. (splay_tree_value) value);
  240. }
  241. /* Without changing the mapping of any address, ensure that there is a
  242. tree node at ADDR, even if it would represent a "transition" from
  243. one value to the same value. */
  244. static void
  245. force_transition (struct addrmap_mutable *self, CORE_ADDR addr)
  246. {
  247. splay_tree_node n
  248. = addrmap_splay_tree_lookup (self, addr);
  249. if (! n)
  250. {
  251. n = addrmap_splay_tree_predecessor (self, addr);
  252. addrmap_splay_tree_insert (self, addr,
  253. n ? addrmap_node_value (n) : NULL);
  254. }
  255. }
  256. static void
  257. addrmap_mutable_set_empty (struct addrmap *self,
  258. CORE_ADDR start, CORE_ADDR end_inclusive,
  259. void *obj)
  260. {
  261. struct addrmap_mutable *map = (struct addrmap_mutable *) self;
  262. splay_tree_node n, next;
  263. void *prior_value;
  264. /* If we're being asked to set all empty portions of the given
  265. address range to empty, then probably the caller is confused.
  266. (If that turns out to be useful in some cases, then we can change
  267. this to simply return, since overriding NULL with NULL is a
  268. no-op.) */
  269. gdb_assert (obj);
  270. /* We take a two-pass approach, for simplicity.
  271. - Establish transitions where we think we might need them.
  272. - First pass: change all NULL regions to OBJ.
  273. - Second pass: remove any unnecessary transitions. */
  274. /* Establish transitions at the start and end. */
  275. force_transition (map, start);
  276. if (end_inclusive < CORE_ADDR_MAX)
  277. force_transition (map, end_inclusive + 1);
  278. /* Walk the area, changing all NULL regions to OBJ. */
  279. for (n = addrmap_splay_tree_lookup (map, start), gdb_assert (n);
  280. n && addrmap_node_key (n) <= end_inclusive;
  281. n = addrmap_splay_tree_successor (map, addrmap_node_key (n)))
  282. {
  283. if (! addrmap_node_value (n))
  284. addrmap_node_set_value (n, obj);
  285. }
  286. /* Walk the area again, removing transitions from any value to
  287. itself. Be sure to visit both the transitions we forced
  288. above. */
  289. n = addrmap_splay_tree_predecessor (map, start);
  290. prior_value = n ? addrmap_node_value (n) : NULL;
  291. for (n = addrmap_splay_tree_lookup (map, start), gdb_assert (n);
  292. n && (end_inclusive == CORE_ADDR_MAX
  293. || addrmap_node_key (n) <= end_inclusive + 1);
  294. n = next)
  295. {
  296. next = addrmap_splay_tree_successor (map, addrmap_node_key (n));
  297. if (addrmap_node_value (n) == prior_value)
  298. addrmap_splay_tree_remove (map, addrmap_node_key (n));
  299. else
  300. prior_value = addrmap_node_value (n);
  301. }
  302. }
  303. static void *
  304. addrmap_mutable_find (struct addrmap *self, CORE_ADDR addr)
  305. {
  306. struct addrmap_mutable *map = (struct addrmap_mutable *) self;
  307. splay_tree_node n = addrmap_splay_tree_lookup (map, addr);
  308. if (n != nullptr)
  309. {
  310. gdb_assert (addrmap_node_key (n) == addr);
  311. return addrmap_node_value (n);
  312. }
  313. n = addrmap_splay_tree_predecessor (map, addr);
  314. if (n != nullptr)
  315. {
  316. gdb_assert (addrmap_node_key (n) < addr);
  317. return addrmap_node_value (n);
  318. }
  319. return nullptr;
  320. }
  321. /* A function to pass to splay_tree_foreach to count the number of nodes
  322. in the tree. */
  323. static int
  324. splay_foreach_count (splay_tree_node n, void *closure)
  325. {
  326. size_t *count = (size_t *) closure;
  327. (*count)++;
  328. return 0;
  329. }
  330. /* A function to pass to splay_tree_foreach to copy entries into a
  331. fixed address map. */
  332. static int
  333. splay_foreach_copy (splay_tree_node n, void *closure)
  334. {
  335. struct addrmap_fixed *fixed = (struct addrmap_fixed *) closure;
  336. struct addrmap_transition *t = &fixed->transitions[fixed->num_transitions];
  337. t->addr = addrmap_node_key (n);
  338. t->value = addrmap_node_value (n);
  339. fixed->num_transitions++;
  340. return 0;
  341. }
  342. static struct addrmap *
  343. addrmap_mutable_create_fixed (struct addrmap *self, struct obstack *obstack)
  344. {
  345. struct addrmap_mutable *mutable_obj = (struct addrmap_mutable *) self;
  346. struct addrmap_fixed *fixed;
  347. size_t num_transitions;
  348. size_t alloc_len;
  349. /* Count the number of transitions in the tree. */
  350. num_transitions = 0;
  351. splay_tree_foreach (mutable_obj->tree, splay_foreach_count, &num_transitions);
  352. /* Include an extra entry for the transition at zero (which fixed
  353. maps have, but mutable maps do not.) */
  354. num_transitions++;
  355. alloc_len = sizeof (*fixed)
  356. + (num_transitions * sizeof (fixed->transitions[0]));
  357. fixed = (struct addrmap_fixed *) obstack_alloc (obstack, alloc_len);
  358. fixed->addrmap.funcs = &addrmap_fixed_funcs;
  359. fixed->num_transitions = 1;
  360. fixed->transitions[0].addr = 0;
  361. fixed->transitions[0].value = NULL;
  362. /* Copy all entries from the splay tree to the array, in order
  363. of increasing address. */
  364. splay_tree_foreach (mutable_obj->tree, splay_foreach_copy, fixed);
  365. /* We should have filled the array. */
  366. gdb_assert (fixed->num_transitions == num_transitions);
  367. return (struct addrmap *) fixed;
  368. }
  369. static void
  370. addrmap_mutable_relocate (struct addrmap *self, CORE_ADDR offset)
  371. {
  372. /* Not needed yet. */
  373. internal_error (__FILE__, __LINE__,
  374. _("addrmap_relocate is not implemented yet "
  375. "for mutable addrmaps"));
  376. }
  377. /* This is a splay_tree_foreach_fn. */
  378. static int
  379. addrmap_mutable_foreach_worker (splay_tree_node node, void *data)
  380. {
  381. addrmap_foreach_fn *fn = (addrmap_foreach_fn *) data;
  382. return (*fn) (addrmap_node_key (node), addrmap_node_value (node));
  383. }
  384. static int
  385. addrmap_mutable_foreach (struct addrmap *self, addrmap_foreach_fn fn)
  386. {
  387. struct addrmap_mutable *mutable_obj = (struct addrmap_mutable *) self;
  388. return splay_tree_foreach (mutable_obj->tree, addrmap_mutable_foreach_worker,
  389. &fn);
  390. }
  391. static const struct addrmap_funcs addrmap_mutable_funcs =
  392. {
  393. addrmap_mutable_set_empty,
  394. addrmap_mutable_find,
  395. addrmap_mutable_create_fixed,
  396. addrmap_mutable_relocate,
  397. addrmap_mutable_foreach
  398. };
  399. static void *
  400. splay_obstack_alloc (int size, void *closure)
  401. {
  402. struct addrmap_mutable *map = (struct addrmap_mutable *) closure;
  403. splay_tree_node n;
  404. /* We should only be asked to allocate nodes and larger things.
  405. (If, at some point in the future, this is no longer true, we can
  406. just round up the size to sizeof (*n).) */
  407. gdb_assert (size >= sizeof (*n));
  408. if (map->free_nodes)
  409. {
  410. n = map->free_nodes;
  411. map->free_nodes = n->right;
  412. return n;
  413. }
  414. else
  415. return obstack_alloc (map->obstack, size);
  416. }
  417. static void
  418. splay_obstack_free (void *obj, void *closure)
  419. {
  420. struct addrmap_mutable *map = (struct addrmap_mutable *) closure;
  421. splay_tree_node n = (splay_tree_node) obj;
  422. /* We've asserted in the allocation function that we only allocate
  423. nodes or larger things, so it should be safe to put whatever
  424. we get passed back on the free list. */
  425. n->right = map->free_nodes;
  426. map->free_nodes = n;
  427. }
  428. /* Compare keys as CORE_ADDR * values. */
  429. static int
  430. splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk)
  431. {
  432. CORE_ADDR a = * (CORE_ADDR *) ak;
  433. CORE_ADDR b = * (CORE_ADDR *) bk;
  434. /* We can't just return a-b here, because of over/underflow. */
  435. if (a < b)
  436. return -1;
  437. else if (a == b)
  438. return 0;
  439. else
  440. return 1;
  441. }
  442. struct addrmap *
  443. addrmap_create_mutable (struct obstack *obstack)
  444. {
  445. struct addrmap_mutable *map = XOBNEW (obstack, struct addrmap_mutable);
  446. map->addrmap.funcs = &addrmap_mutable_funcs;
  447. map->obstack = obstack;
  448. /* splay_tree_new_with_allocator uses the provided allocation
  449. function to allocate the main splay_tree structure itself, so our
  450. free list has to be initialized before we create the tree. */
  451. map->free_nodes = NULL;
  452. map->tree = splay_tree_new_with_allocator (splay_compare_CORE_ADDR_ptr,
  453. NULL, /* no delete key */
  454. NULL, /* no delete value */
  455. splay_obstack_alloc,
  456. splay_obstack_free,
  457. map);
  458. return (struct addrmap *) map;
  459. }
  460. /* See addrmap.h. */
  461. void
  462. addrmap_dump (struct addrmap *map, struct ui_file *outfile, void *payload)
  463. {
  464. /* True if the previously printed addrmap entry was for PAYLOAD.
  465. If so, we want to print the next one as well (since the next
  466. addrmap entry defines the end of the range). */
  467. bool previous_matched = false;
  468. auto callback = [&] (CORE_ADDR start_addr, void *obj)
  469. {
  470. QUIT;
  471. bool matches = payload == nullptr || payload == obj;
  472. const char *addr_str = nullptr;
  473. if (matches)
  474. addr_str = host_address_to_string (obj);
  475. else if (previous_matched)
  476. addr_str = "<ends here>";
  477. if (matches || previous_matched)
  478. gdb_printf (outfile, " %s%s %s\n",
  479. payload != nullptr ? " " : "",
  480. core_addr_to_string (start_addr),
  481. addr_str);
  482. previous_matched = matches;
  483. return 0;
  484. };
  485. addrmap_foreach (map, callback);
  486. }
  487. #if GDB_SELF_TEST
  488. namespace selftests {
  489. /* Convert P to CORE_ADDR. */
  490. static CORE_ADDR
  491. core_addr (void *p)
  492. {
  493. return (CORE_ADDR)(uintptr_t)p;
  494. }
  495. /* Check that &ARRAY[LOW]..&ARRAY[HIGH] has VAL in MAP. */
  496. #define CHECK_ADDRMAP_FIND(MAP, ARRAY, LOW, HIGH, VAL) \
  497. do \
  498. { \
  499. for (unsigned i = LOW; i <= HIGH; ++i) \
  500. SELF_CHECK (addrmap_find (MAP, core_addr (&ARRAY[i])) == VAL); \
  501. } \
  502. while (0)
  503. /* Entry point for addrmap unit tests. */
  504. static void
  505. test_addrmap ()
  506. {
  507. /* We'll verify using the addresses of the elements of this array. */
  508. char array[20];
  509. /* We'll verify using these values stored into the map. */
  510. void *val1 = &array[1];
  511. void *val2 = &array[2];
  512. /* Create mutable addrmap. */
  513. struct obstack temp_obstack;
  514. obstack_init (&temp_obstack);
  515. struct addrmap *map = addrmap_create_mutable (&temp_obstack);
  516. SELF_CHECK (map != nullptr);
  517. /* Check initial state. */
  518. CHECK_ADDRMAP_FIND (map, array, 0, 19, nullptr);
  519. /* Insert address range into mutable addrmap. */
  520. addrmap_set_empty (map, core_addr (&array[10]), core_addr (&array[12]),
  521. val1);
  522. CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr);
  523. CHECK_ADDRMAP_FIND (map, array, 10, 12, val1);
  524. CHECK_ADDRMAP_FIND (map, array, 13, 19, nullptr);
  525. /* Create corresponding fixed addrmap. */
  526. struct addrmap *map2 = addrmap_create_fixed (map, &temp_obstack);
  527. SELF_CHECK (map2 != nullptr);
  528. CHECK_ADDRMAP_FIND (map2, array, 0, 9, nullptr);
  529. CHECK_ADDRMAP_FIND (map2, array, 10, 12, val1);
  530. CHECK_ADDRMAP_FIND (map2, array, 13, 19, nullptr);
  531. /* Iterate over both addrmaps. */
  532. auto callback = [&] (CORE_ADDR start_addr, void *obj)
  533. {
  534. if (start_addr == core_addr (nullptr))
  535. SELF_CHECK (obj == nullptr);
  536. else if (start_addr == core_addr (&array[10]))
  537. SELF_CHECK (obj == val1);
  538. else if (start_addr == core_addr (&array[13]))
  539. SELF_CHECK (obj == nullptr);
  540. else
  541. SELF_CHECK (false);
  542. return 0;
  543. };
  544. SELF_CHECK (addrmap_foreach (map, callback) == 0);
  545. SELF_CHECK (addrmap_foreach (map2, callback) == 0);
  546. /* Relocate fixed addrmap. */
  547. addrmap_relocate (map2, 1);
  548. CHECK_ADDRMAP_FIND (map2, array, 0, 10, nullptr);
  549. CHECK_ADDRMAP_FIND (map2, array, 11, 13, val1);
  550. CHECK_ADDRMAP_FIND (map2, array, 14, 19, nullptr);
  551. /* Insert partially overlapping address range into mutable addrmap. */
  552. addrmap_set_empty (map, core_addr (&array[11]), core_addr (&array[13]),
  553. val2);
  554. CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr);
  555. CHECK_ADDRMAP_FIND (map, array, 10, 12, val1);
  556. CHECK_ADDRMAP_FIND (map, array, 13, 13, val2);
  557. CHECK_ADDRMAP_FIND (map, array, 14, 19, nullptr);
  558. /* Cleanup. */
  559. obstack_free (&temp_obstack, NULL);
  560. }
  561. } // namespace selftests
  562. #endif /* GDB_SELF_TEST */
  563. void _initialize_addrmap ();
  564. void
  565. _initialize_addrmap ()
  566. {
  567. #if GDB_SELF_TEST
  568. selftests::register_test ("addrmap", selftests::test_addrmap);
  569. #endif /* GDB_SELF_TEST */
  570. }