1 /* addrmap.c --- implementation of address map data structure.
3 Copyright (C) 2007-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbsupport/gdb_obstack.h"
23 #include "gdbsupport/selftest.h"
25 /* Make sure splay trees can actually hold the values we want to
27 gdb_static_assert (sizeof (splay_tree_key
) >= sizeof (CORE_ADDR
*));
28 gdb_static_assert (sizeof (splay_tree_value
) >= sizeof (void *));
31 /* Fixed address maps. */
34 addrmap_fixed::set_empty (CORE_ADDR start
, CORE_ADDR end_inclusive
,
37 internal_error (__FILE__
, __LINE__
,
38 "addrmap_fixed_set_empty: "
39 "fixed addrmaps can't be changed\n");
44 addrmap_fixed::find (CORE_ADDR addr
) const
46 const struct addrmap_transition
*bottom
= &transitions
[0];
47 const struct addrmap_transition
*top
= &transitions
[num_transitions
- 1];
51 /* This needs to round towards top, or else when top = bottom +
52 1 (i.e., two entries are under consideration), then mid ==
53 bottom, and then we may not narrow the range when (mid->addr
55 const addrmap_transition
*mid
= top
- (top
- bottom
) / 2;
57 if (mid
->addr
== addr
)
62 else if (mid
->addr
< addr
)
63 /* We don't eliminate mid itself here, since each transition
64 covers all subsequent addresses until the next. This is why
65 we must round up in computing the midpoint. */
76 addrmap_fixed::relocate (CORE_ADDR offset
)
80 for (i
= 0; i
< num_transitions
; i
++)
81 transitions
[i
].addr
+= offset
;
86 addrmap_fixed::foreach (addrmap_foreach_fn fn
)
90 for (i
= 0; i
< num_transitions
; i
++)
92 int res
= fn (transitions
[i
].addr
, transitions
[i
].value
);
103 /* Mutable address maps. */
105 /* Allocate a copy of CORE_ADDR. */
107 addrmap_mutable::allocate_key (CORE_ADDR addr
)
109 CORE_ADDR
*key
= XNEW (CORE_ADDR
);
112 return (splay_tree_key
) key
;
116 /* Type-correct wrappers for splay tree access. */
118 addrmap_mutable::splay_tree_lookup (CORE_ADDR addr
) const
120 return ::splay_tree_lookup (tree
, (splay_tree_key
) &addr
);
125 addrmap_mutable::splay_tree_predecessor (CORE_ADDR addr
) const
127 return ::splay_tree_predecessor (tree
, (splay_tree_key
) &addr
);
132 addrmap_mutable::splay_tree_successor (CORE_ADDR addr
)
134 return ::splay_tree_successor (tree
, (splay_tree_key
) &addr
);
139 addrmap_mutable::splay_tree_remove (CORE_ADDR addr
)
141 ::splay_tree_remove (tree
, (splay_tree_key
) &addr
);
146 addrmap_node_key (splay_tree_node node
)
148 return * (CORE_ADDR
*) node
->key
;
153 addrmap_node_value (splay_tree_node node
)
155 return (void *) node
->value
;
160 addrmap_node_set_value (splay_tree_node node
, void *value
)
162 node
->value
= (splay_tree_value
) value
;
167 addrmap_mutable::splay_tree_insert (CORE_ADDR key
, void *value
)
169 ::splay_tree_insert (tree
,
171 (splay_tree_value
) value
);
175 /* Without changing the mapping of any address, ensure that there is a
176 tree node at ADDR, even if it would represent a "transition" from
177 one value to the same value. */
179 addrmap_mutable::force_transition (CORE_ADDR addr
)
181 splay_tree_node n
= splay_tree_lookup (addr
);
185 n
= splay_tree_predecessor (addr
);
186 splay_tree_insert (addr
, n
? addrmap_node_value (n
) : NULL
);
192 addrmap_mutable::set_empty (CORE_ADDR start
, CORE_ADDR end_inclusive
,
195 splay_tree_node n
, next
;
198 /* If we're being asked to set all empty portions of the given
199 address range to empty, then probably the caller is confused.
200 (If that turns out to be useful in some cases, then we can change
201 this to simply return, since overriding NULL with NULL is a
205 /* We take a two-pass approach, for simplicity.
206 - Establish transitions where we think we might need them.
207 - First pass: change all NULL regions to OBJ.
208 - Second pass: remove any unnecessary transitions. */
210 /* Establish transitions at the start and end. */
211 force_transition (start
);
212 if (end_inclusive
< CORE_ADDR_MAX
)
213 force_transition (end_inclusive
+ 1);
215 /* Walk the area, changing all NULL regions to OBJ. */
216 for (n
= splay_tree_lookup (start
), gdb_assert (n
);
217 n
&& addrmap_node_key (n
) <= end_inclusive
;
218 n
= splay_tree_successor (addrmap_node_key (n
)))
220 if (! addrmap_node_value (n
))
221 addrmap_node_set_value (n
, obj
);
224 /* Walk the area again, removing transitions from any value to
225 itself. Be sure to visit both the transitions we forced
227 n
= splay_tree_predecessor (start
);
228 prior_value
= n
? addrmap_node_value (n
) : NULL
;
229 for (n
= splay_tree_lookup (start
), gdb_assert (n
);
230 n
&& (end_inclusive
== CORE_ADDR_MAX
231 || addrmap_node_key (n
) <= end_inclusive
+ 1);
234 next
= splay_tree_successor (addrmap_node_key (n
));
235 if (addrmap_node_value (n
) == prior_value
)
236 splay_tree_remove (addrmap_node_key (n
));
238 prior_value
= addrmap_node_value (n
);
244 addrmap_mutable::find (CORE_ADDR addr
) const
246 splay_tree_node n
= splay_tree_lookup (addr
);
249 gdb_assert (addrmap_node_key (n
) == addr
);
250 return addrmap_node_value (n
);
253 n
= splay_tree_predecessor (addr
);
256 gdb_assert (addrmap_node_key (n
) < addr
);
257 return addrmap_node_value (n
);
264 addrmap_fixed::addrmap_fixed (struct obstack
*obstack
, addrmap_mutable
*mut
)
266 size_t transition_count
= 0;
268 /* Count the number of transitions in the tree. */
269 mut
->foreach ([&] (CORE_ADDR start
, void *obj
)
275 /* Include an extra entry for the transition at zero (which fixed
276 maps have, but mutable maps do not.) */
280 transitions
= XOBNEWVEC (obstack
, struct addrmap_transition
,
282 transitions
[0].addr
= 0;
283 transitions
[0].value
= NULL
;
285 /* Copy all entries from the splay tree to the array, in order
286 of increasing address. */
287 mut
->foreach ([&] (CORE_ADDR start
, void *obj
)
289 transitions
[num_transitions
].addr
= start
;
290 transitions
[num_transitions
].value
= obj
;
295 /* We should have filled the array. */
296 gdb_assert (num_transitions
== transition_count
);
301 addrmap_mutable::relocate (CORE_ADDR offset
)
303 /* Not needed yet. */
304 internal_error (__FILE__
, __LINE__
,
305 _("addrmap_relocate is not implemented yet "
306 "for mutable addrmaps"));
310 /* This is a splay_tree_foreach_fn. */
313 addrmap_mutable_foreach_worker (splay_tree_node node
, void *data
)
315 addrmap_foreach_fn
*fn
= (addrmap_foreach_fn
*) data
;
317 return (*fn
) (addrmap_node_key (node
), addrmap_node_value (node
));
322 addrmap_mutable::foreach (addrmap_foreach_fn fn
)
324 return splay_tree_foreach (tree
, addrmap_mutable_foreach_worker
, &fn
);
328 /* Compare keys as CORE_ADDR * values. */
330 splay_compare_CORE_ADDR_ptr (splay_tree_key ak
, splay_tree_key bk
)
332 CORE_ADDR a
= * (CORE_ADDR
*) ak
;
333 CORE_ADDR b
= * (CORE_ADDR
*) bk
;
335 /* We can't just return a-b here, because of over/underflow. */
346 xfree_wrapper (splay_tree_key key
)
348 xfree ((void *) key
);
351 addrmap_mutable::addrmap_mutable ()
352 : tree (splay_tree_new (splay_compare_CORE_ADDR_ptr
, xfree_wrapper
,
353 nullptr /* no delete value */))
357 addrmap_mutable::~addrmap_mutable ()
359 splay_tree_delete (tree
);
366 addrmap_dump (struct addrmap
*map
, struct ui_file
*outfile
, void *payload
)
368 /* True if the previously printed addrmap entry was for PAYLOAD.
369 If so, we want to print the next one as well (since the next
370 addrmap entry defines the end of the range). */
371 bool previous_matched
= false;
373 auto callback
= [&] (CORE_ADDR start_addr
, void *obj
)
377 bool matches
= payload
== nullptr || payload
== obj
;
378 const char *addr_str
= nullptr;
380 addr_str
= host_address_to_string (obj
);
381 else if (previous_matched
)
382 addr_str
= "<ends here>";
384 if (matches
|| previous_matched
)
385 gdb_printf (outfile
, " %s%s %s\n",
386 payload
!= nullptr ? " " : "",
387 core_addr_to_string (start_addr
),
390 previous_matched
= matches
;
395 map
->foreach (callback
);
399 namespace selftests
{
401 /* Convert P to CORE_ADDR. */
406 return (CORE_ADDR
)(uintptr_t)p
;
409 /* Check that &ARRAY[LOW]..&ARRAY[HIGH] has VAL in MAP. */
411 #define CHECK_ADDRMAP_FIND(MAP, ARRAY, LOW, HIGH, VAL) \
414 for (unsigned i = LOW; i <= HIGH; ++i) \
415 SELF_CHECK (MAP->find (core_addr (&ARRAY[i])) == VAL); \
419 /* Entry point for addrmap unit tests. */
424 /* We'll verify using the addresses of the elements of this array. */
427 /* We'll verify using these values stored into the map. */
428 void *val1
= &array
[1];
429 void *val2
= &array
[2];
431 /* Create mutable addrmap. */
432 auto_obstack temp_obstack
;
433 std::unique_ptr
<struct addrmap_mutable
> map (new addrmap_mutable
);
434 SELF_CHECK (map
!= nullptr);
436 /* Check initial state. */
437 CHECK_ADDRMAP_FIND (map
, array
, 0, 19, nullptr);
439 /* Insert address range into mutable addrmap. */
440 map
->set_empty (core_addr (&array
[10]), core_addr (&array
[12]), val1
);
441 CHECK_ADDRMAP_FIND (map
, array
, 0, 9, nullptr);
442 CHECK_ADDRMAP_FIND (map
, array
, 10, 12, val1
);
443 CHECK_ADDRMAP_FIND (map
, array
, 13, 19, nullptr);
445 /* Create corresponding fixed addrmap. */
447 = new (&temp_obstack
) addrmap_fixed (&temp_obstack
, map
.get ());
448 SELF_CHECK (map2
!= nullptr);
449 CHECK_ADDRMAP_FIND (map2
, array
, 0, 9, nullptr);
450 CHECK_ADDRMAP_FIND (map2
, array
, 10, 12, val1
);
451 CHECK_ADDRMAP_FIND (map2
, array
, 13, 19, nullptr);
453 /* Iterate over both addrmaps. */
454 auto callback
= [&] (CORE_ADDR start_addr
, void *obj
)
456 if (start_addr
== core_addr (nullptr))
457 SELF_CHECK (obj
== nullptr);
458 else if (start_addr
== core_addr (&array
[10]))
459 SELF_CHECK (obj
== val1
);
460 else if (start_addr
== core_addr (&array
[13]))
461 SELF_CHECK (obj
== nullptr);
466 SELF_CHECK (map
->foreach (callback
) == 0);
467 SELF_CHECK (map2
->foreach (callback
) == 0);
469 /* Relocate fixed addrmap. */
471 CHECK_ADDRMAP_FIND (map2
, array
, 0, 10, nullptr);
472 CHECK_ADDRMAP_FIND (map2
, array
, 11, 13, val1
);
473 CHECK_ADDRMAP_FIND (map2
, array
, 14, 19, nullptr);
475 /* Insert partially overlapping address range into mutable addrmap. */
476 map
->set_empty (core_addr (&array
[11]), core_addr (&array
[13]), val2
);
477 CHECK_ADDRMAP_FIND (map
, array
, 0, 9, nullptr);
478 CHECK_ADDRMAP_FIND (map
, array
, 10, 12, val1
);
479 CHECK_ADDRMAP_FIND (map
, array
, 13, 13, val2
);
480 CHECK_ADDRMAP_FIND (map
, array
, 14, 19, nullptr);
483 } // namespace selftests
484 #endif /* GDB_SELF_TEST */
486 void _initialize_addrmap ();
488 _initialize_addrmap ()
491 selftests::register_test ("addrmap", selftests::test_addrmap
);
492 #endif /* GDB_SELF_TEST */