3 Copyright (C) 2022-2025 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/>. */
20 #include "dwarf2/cooked-index.h"
21 #include "dwarf2/read.h"
22 #include "dwarf2/stringify.h"
23 #include "event-top.h"
25 #include "observable.h"
26 #include "run-on-main-thread.h"
27 #include "gdbsupport/task-group.h"
28 #include "cli/cli-cmds.h"
30 /* We don't want gdb to exit while it is in the process of writing to
31 the index cache. So, all live cooked index vectors are stored
32 here, and then these are all waited for before exit proceeds. */
33 static gdb::unordered_set
<cooked_index
*> active_vectors
;
35 /* Return true if LANG requires canonicalization. This is used
36 primarily to work around an issue computing the name of "main".
37 This function must be kept in sync with
38 cooked_index_shard::finalize. */
41 language_requires_canonicalization (enum language lang
)
43 return (lang
== language_ada
45 || lang
== language_cplus
);
48 cooked_index::cooked_index (cooked_index_worker_up
&&worker
)
49 : m_state (std::move (worker
))
51 /* ACTIVE_VECTORS is not locked, and this assert ensures that this
52 will be caught if ever moved to the background. */
53 gdb_assert (is_main_thread ());
54 active_vectors
.insert (this);
58 cooked_index::start_reading ()
64 cooked_index::wait (cooked_state desired_state
, bool allow_quit
)
66 gdb_assert (desired_state
!= cooked_state::INITIAL
);
68 /* If the state object has been deleted, then that means waiting is
70 if (m_state
== nullptr)
73 if (m_state
->wait (desired_state
, allow_quit
))
75 /* Only the main thread can modify this. */
76 gdb_assert (is_main_thread ());
77 m_state
.reset (nullptr);
82 cooked_index::set_contents ()
84 gdb_assert (m_shards
.empty ());
85 m_shards
= m_state
->release_shards ();
87 m_state
->set (cooked_state::MAIN_AVAILABLE
);
89 /* This is run after finalization is done -- but not before. If
90 this task were submitted earlier, it would have to wait for
91 finalization. However, that would take a slot in the global
92 thread pool, and if enough such tasks were submitted at once, it
93 would cause a livelock. */
94 gdb::task_group
finalizers ([this] ()
96 m_state
->set (cooked_state::FINALIZED
);
97 m_state
->write_to_cache (index_for_writing ());
98 m_state
->set (cooked_state::CACHE_DONE
);
101 for (auto &shard
: m_shards
)
103 auto this_shard
= shard
.get ();
104 const parent_map_map
*parent_maps
= m_state
->get_parent_map_map ();
105 finalizers
.add_task ([=] ()
107 scoped_time_it
time_it ("DWARF finalize worker",
108 m_state
->m_per_command_time
);
109 this_shard
->finalize (parent_maps
);
116 cooked_index::~cooked_index ()
118 /* Wait for index-creation to be done, though this one must also
119 waited for by the per-BFD object to ensure the required data
121 wait (cooked_state::CACHE_DONE
);
123 /* Remove our entry from the global list. See the assert in the
124 constructor to understand this. */
125 gdb_assert (is_main_thread ());
126 active_vectors
.erase (this);
129 /* See cooked-index.h. */
132 cooked_index::lookup (unrelocated_addr addr
)
134 /* Ensure that the address maps are ready. */
135 wait (cooked_state::MAIN_AVAILABLE
, true);
136 for (const auto &shard
: m_shards
)
138 dwarf2_per_cu
*result
= shard
->lookup (addr
);
139 if (result
!= nullptr)
145 /* See cooked-index.h. */
147 std::vector
<const addrmap
*>
148 cooked_index::get_addrmaps ()
150 /* Ensure that the address maps are ready. */
151 wait (cooked_state::MAIN_AVAILABLE
, true);
152 std::vector
<const addrmap
*> result
;
153 for (const auto &shard
: m_shards
)
154 result
.push_back (shard
->m_addrmap
);
158 /* See cooked-index.h. */
161 cooked_index::find (const std::string
&name
, bool completing
)
163 wait (cooked_state::FINALIZED
, true);
164 std::vector
<cooked_index_shard::range
> result_range
;
165 result_range
.reserve (m_shards
.size ());
166 for (auto &shard
: m_shards
)
167 result_range
.push_back (shard
->find (name
, completing
));
168 return range (std::move (result_range
));
171 /* See cooked-index.h. */
174 cooked_index::get_main_name (struct obstack
*obstack
, enum language
*lang
)
177 const cooked_index_entry
*entry
= get_main ();
178 if (entry
== nullptr)
182 return entry
->full_name (obstack
, FOR_MAIN
);
185 /* See cooked_index.h. */
187 const cooked_index_entry
*
188 cooked_index::get_main () const
190 const cooked_index_entry
*best_entry
= nullptr;
191 for (const auto &shard
: m_shards
)
193 const cooked_index_entry
*entry
= shard
->get_main ();
194 /* Choose the first "main" we see. We only do this for names
195 not requiring canonicalization. At this point in the process
196 names might not have been canonicalized. However, currently,
197 languages that require this step also do not use
198 DW_AT_main_subprogram. An assert is appropriate here because
199 this filtering is done in get_main. */
200 if (entry
!= nullptr)
202 if ((entry
->flags
& IS_MAIN
) != 0)
204 if (!language_requires_canonicalization (entry
->lang
))
206 /* There won't be one better than this. */
212 /* This is one that is named "main". Here we don't care
213 if the language requires canonicalization, due to how
214 the entry is detected. Entries like this have worse
215 priority than IS_MAIN entries. */
216 if (best_entry
== nullptr)
225 quick_symbol_functions_up
226 cooked_index::make_quick_functions () const
228 return quick_symbol_functions_up (new cooked_index_functions
);
231 /* See cooked-index.h. */
234 cooked_index::dump (gdbarch
*arch
)
236 auto_obstack temp_storage
;
238 gdb_printf (" entries:\n");
242 for (const cooked_index_entry
*entry
: this->all_entries ())
246 gdb_printf (" [%zu] ((cooked_index_entry *) %p)\n", i
++, entry
);
247 gdb_printf (" name: %s\n", entry
->name
);
248 gdb_printf (" canonical: %s\n", entry
->canonical
);
249 gdb_printf (" qualified: %s\n",
250 entry
->full_name (&temp_storage
, 0, "::"));
251 gdb_printf (" DWARF tag: %s\n", dwarf_tag_name (entry
->tag
));
252 gdb_printf (" flags: %s\n", to_string (entry
->flags
).c_str ());
253 gdb_printf (" DIE offset: %s\n", sect_offset_str (entry
->die_offset
));
255 if ((entry
->flags
& IS_PARENT_DEFERRED
) != 0)
256 gdb_printf (" parent: deferred (%" PRIx64
")\n",
257 entry
->get_deferred_parent ());
258 else if (entry
->get_parent () != nullptr)
259 gdb_printf (" parent: ((cooked_index_entry *) %p) [%s]\n",
260 entry
->get_parent (), entry
->get_parent ()->name
);
262 gdb_printf (" parent: ((cooked_index_entry *) 0)\n");
267 const cooked_index_entry
*main_entry
= this->get_main ();
268 if (main_entry
!= nullptr)
269 gdb_printf (" main: ((cooked_index_entry *) %p) [%s]\n", main_entry
,
272 gdb_printf (" main: ((cooked_index_entry *) 0)\n");
275 gdb_printf (" address maps:\n");
278 std::vector
<const addrmap
*> addrmaps
= this->get_addrmaps ();
279 for (i
= 0; i
< addrmaps
.size (); ++i
)
281 const addrmap
*addrmap
= addrmaps
[i
];
283 gdb_printf (" [%zu] ((addrmap *) %p)\n", i
, addrmap
);
286 if (addrmap
== nullptr)
289 addrmap
->foreach ([arch
] (CORE_ADDR start_addr
, const void *obj
)
293 const char *start_addr_str
= paddress (arch
, start_addr
);
297 const dwarf2_per_cu
*per_cu
298 = static_cast<const dwarf2_per_cu
*> (obj
);
299 gdb_printf (" [%s] ((dwarf2_per_cu *) %p)\n",
300 start_addr_str
, per_cu
);
303 gdb_printf (" [%s] ((dwarf2_per_cu *) 0)\n", start_addr_str
);
312 /* Wait for all the index cache entries to be written before gdb
315 wait_for_index_cache (int)
317 gdb_assert (is_main_thread ());
318 for (cooked_index
*item
: active_vectors
)
319 item
->wait_completely ();
322 /* A maint command to wait for the cache. */
325 maintenance_wait_for_index_cache (const char *args
, int from_tty
)
327 wait_for_index_cache (0);
330 INIT_GDB_FILE (cooked_index
)
332 add_cmd ("wait-for-index-cache", class_maintenance
,
333 maintenance_wait_for_index_cache
, _("\
334 Wait until all pending writes to the index cache have completed.\n\
335 Usage: maintenance wait-for-index-cache"),
338 gdb::observers::gdb_exiting
.attach (wait_for_index_cache
, "cooked-index");