xref: /netbsd-src/external/gpl3/gdb/dist/gdbserver/dll.cc (revision f1c2b495c8d0ed769f039187bdd4f963026e012b)
1 /* Copyright (C) 2002-2024 Free Software Foundation, Inc.
2 
3    This file is part of GDB.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include "dll.h"
19 
20 #include <algorithm>
21 
22 /* An "unspecified" CORE_ADDR, for match_dll.  */
23 #define UNSPECIFIED_CORE_ADDR (~(CORE_ADDR) 0)
24 
25 /* Record a newly loaded DLL at BASE_ADDR for the current process.  */
26 
27 void
28 loaded_dll (const char *name, CORE_ADDR base_addr)
29 {
30   loaded_dll (current_process (), name, base_addr);
31 }
32 
33 /* Record a newly loaded DLL at BASE_ADDR for PROC.  */
34 
35 void
36 loaded_dll (process_info *proc, const char *name, CORE_ADDR base_addr)
37 {
38   gdb_assert (proc != nullptr);
39   proc->all_dlls.emplace_back (name != nullptr ? name : "", base_addr);
40   proc->dlls_changed = true;
41 }
42 
43 /* Record that the DLL with NAME and BASE_ADDR has been unloaded
44    from the current process.  */
45 
46 void
47 unloaded_dll (const char *name, CORE_ADDR base_addr)
48 {
49   unloaded_dll (current_process (), name, base_addr);
50 }
51 
52 /* Record that the DLL with NAME and BASE_ADDR has been unloaded
53    from PROC.  */
54 
55 void
56 unloaded_dll (process_info *proc, const char *name, CORE_ADDR base_addr)
57 {
58   gdb_assert (proc != nullptr);
59   auto pred = [&] (const dll_info &dll)
60     {
61       if (base_addr != UNSPECIFIED_CORE_ADDR
62 	  && base_addr == dll.base_addr)
63 	return true;
64 
65       if (name != NULL && dll.name == name)
66 	return true;
67 
68       return false;
69     };
70 
71   auto iter = std::find_if (proc->all_dlls.begin (), proc->all_dlls.end (),
72 			    pred);
73 
74   if (iter == proc->all_dlls.end ())
75     /* For some inferiors we might get unloaded_dll events without having
76        a corresponding loaded_dll.  In that case, the dll cannot be found
77        in ALL_DLL, and there is nothing further for us to do.
78 
79        This has been observed when running 32bit executables on Windows64
80        (i.e. through WOW64, the interface between the 32bits and 64bits
81        worlds).  In that case, the inferior always does some strange
82        unloading of unnamed dll.  */
83     return;
84   else
85     {
86       /* DLL has been found so remove the entry and free associated
87 	 resources.  */
88       proc->all_dlls.erase (iter);
89       proc->dlls_changed = true;
90     }
91 }
92 
93 void
94 clear_dlls (void)
95 {
96   for_each_process ([] (process_info *proc)
97     {
98       proc->all_dlls.clear ();
99     });
100 }
101