Branch data Line data Source code
1 : : /* Track multiple Dwfl structs for multiple processes.
2 : : Copyright (C) 2025, Red Hat, Inc.
3 : : This file is part of elfutils.
4 : :
5 : : This file is free software; you can redistribute it and/or modify
6 : : it under the terms of either
7 : :
8 : : * the GNU Lesser General Public License as published by the Free
9 : : Software Foundation; either version 3 of the License, or (at
10 : : your option) any later version
11 : :
12 : : or
13 : :
14 : : * the GNU General Public License as published by the Free
15 : : Software Foundation; either version 2 of the License, or (at
16 : : your option) any later version
17 : :
18 : : or both in parallel, as here.
19 : :
20 : : elfutils is distributed in the hope that it will be useful, but
21 : : WITHOUT ANY WARRANTY; without even the implied warranty of
22 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 : : General Public License for more details.
24 : :
25 : : You should have received copies of the GNU General Public License and
26 : : the GNU Lesser General Public License along with this program. If
27 : : not, see <http://www.gnu.org/licenses/>. */
28 : :
29 : : #ifdef HAVE_CONFIG_H
30 : : # include <config.h>
31 : : #endif
32 : :
33 : : #include "libdwfl_stacktraceP.h"
34 : :
35 : : #define HTAB_DEFAULT_SIZE 1021
36 : :
37 : 0 : Dwflst_Process_Tracker *dwflst_tracker_begin (const Dwfl_Callbacks *callbacks)
38 : : {
39 : 0 : Dwflst_Process_Tracker *tracker = calloc (1, sizeof *tracker);
40 [ # # ]: 0 : if (tracker == NULL)
41 : : {
42 : 0 : __libdwfl_seterrno (DWFL_E_NOMEM);
43 : 0 : return tracker;
44 : : }
45 : :
46 : 0 : dwflst_tracker_elftab_init (&tracker->elftab, HTAB_DEFAULT_SIZE);
47 : 0 : rwlock_init (tracker->elftab_lock);
48 : 0 : dwflst_tracker_dwfltab_init (&tracker->dwfltab, HTAB_DEFAULT_SIZE);
49 : 0 : rwlock_init (tracker->dwfltab_lock);
50 : :
51 : 0 : tracker->callbacks = callbacks;
52 : 0 : return tracker;
53 : : }
54 : :
55 : 0 : Dwfl *dwflst_tracker_dwfl_begin (Dwflst_Process_Tracker *tracker)
56 : : {
57 : 0 : Dwfl *dwfl = INTUSE(dwfl_begin) (tracker->callbacks);
58 [ # # ]: 0 : if (dwfl == NULL)
59 : : return dwfl;
60 : :
61 : : /* TODO: Could also share dwfl->debuginfod, but thread-safely? */
62 : 0 : dwfl->tracker = tracker;
63 : :
64 : : /* XXX: dwfl added to dwfltab when dwfl->process set in dwfl_attach_state. */
65 : : /* XXX: dwfl removed from dwfltab in dwfl_end() */
66 : :
67 : 0 : return dwfl;
68 : : }
69 : :
70 : 0 : Dwfl *dwflst_tracker_find_pid (Dwflst_Process_Tracker *tracker,
71 : : pid_t pid,
72 : : Dwfl *(*callback) (Dwflst_Process_Tracker *,
73 : : pid_t, void *),
74 : : void *arg)
75 : : {
76 : 0 : Dwfl *dwfl = NULL;
77 : :
78 : 0 : rwlock_rdlock (tracker->dwfltab_lock);
79 : 0 : dwflst_tracker_dwfl_info *ent
80 : 0 : = dwflst_tracker_dwfltab_find(&tracker->dwfltab, pid);
81 : 0 : rwlock_unlock (tracker->dwfltab_lock);
82 : :
83 [ # # # # ]: 0 : if (ent != NULL && !ent->invalid)
84 : 0 : dwfl = ent->dwfl;
85 [ # # ]: 0 : if (dwfl == NULL && callback != NULL)
86 : 0 : dwfl = callback(tracker, pid, arg);
87 [ # # ]: 0 : if (dwfl != NULL)
88 : : {
89 [ # # ]: 0 : assert (dwfl->tracker == tracker);
90 : : /* XXX: dwfl added to dwfltab when dwfl->process set in dwfl_attach_state.
91 : : Prior to that, the pid is not confirmed. */
92 : : }
93 : :
94 : 0 : return dwfl;
95 : : }
96 : :
97 : : void
98 : : internal_function
99 : 0 : __libdwfl_stacktrace_add_dwfl_to_tracker (Dwfl *dwfl) {
100 : 0 : Dwflst_Process_Tracker *tracker = dwfl->tracker;
101 [ # # ]: 0 : assert (tracker != NULL);
102 : :
103 : : /* First try to find an existing entry to replace: */
104 : 0 : dwflst_tracker_dwfl_info *ent = NULL;
105 : 0 : unsigned long int hval = dwfl->process->pid;
106 : :
107 : 0 : rwlock_wrlock (tracker->dwfltab_lock);
108 : 0 : ent = dwflst_tracker_dwfltab_find(&tracker->dwfltab, hval);
109 [ # # ]: 0 : if (ent != NULL)
110 : : {
111 : : /* TODO: This is a bare-minimum solution. Ideally
112 : : we would clean up the existing ent->dwfl, but
113 : : this needs to be coordinated with any users of
114 : : the dwfl library that might still be holding it. */
115 : 0 : ent->dwfl = dwfl;
116 : 0 : ent->invalid = false;
117 : 0 : rwlock_unlock (tracker->dwfltab_lock);
118 : 0 : return;
119 : : }
120 : :
121 : : /* Only otherwise try to insert an entry: */
122 : 0 : ent = calloc (1, sizeof(dwflst_tracker_dwfl_info));
123 [ # # ]: 0 : if (ent == NULL)
124 : : {
125 : 0 : rwlock_unlock (tracker->dwfltab_lock);
126 : 0 : __libdwfl_seterrno (DWFL_E_NOMEM);
127 : 0 : return;
128 : : }
129 : 0 : ent->dwfl = dwfl;
130 : 0 : ent->invalid = false;
131 [ # # ]: 0 : if (dwflst_tracker_dwfltab_insert(&tracker->dwfltab, hval, ent) != 0)
132 : : {
133 : 0 : free(ent);
134 : 0 : rwlock_unlock (tracker->dwfltab_lock);
135 : 0 : assert(false); /* Should not occur due to the wrlock on dwfltab. */
136 : : }
137 : 0 : rwlock_unlock (tracker->dwfltab_lock);
138 : : }
139 : :
140 : : void
141 : : internal_function
142 : 0 : __libdwfl_stacktrace_remove_dwfl_from_tracker (Dwfl *dwfl) {
143 [ # # ]: 0 : if (dwfl->tracker == NULL)
144 : : return;
145 : 0 : Dwflst_Process_Tracker *tracker = dwfl->tracker;
146 : 0 : dwflst_tracker_dwfl_info *ent = NULL;
147 [ # # ]: 0 : if (dwfl->process == NULL)
148 : : return;
149 : 0 : unsigned long int hval = dwfl->process->pid;
150 : :
151 : 0 : rwlock_wrlock (tracker->dwfltab_lock);
152 : 0 : ent = dwflst_tracker_dwfltab_find(&tracker->dwfltab, hval);
153 [ # # # # ]: 0 : if (ent != NULL && ent->dwfl == dwfl)
154 : : {
155 : 0 : ent->dwfl = NULL;
156 : 0 : ent->invalid = true;
157 : : }
158 : 0 : rwlock_unlock (tracker->dwfltab_lock);
159 : : }
160 : :
161 : 0 : void dwflst_tracker_end (Dwflst_Process_Tracker *tracker)
162 : : {
163 [ # # ]: 0 : if (tracker == NULL)
164 : : return;
165 : :
166 : 0 : size_t idx;
167 : :
168 : : /* HACK to allow iteration of dynamicsizehash_concurrent. */
169 : : /* XXX Based on lib/dynamicsizehash_concurrent.c free(). */
170 : 0 : rwlock_fini (tracker->elftab_lock);
171 : 0 : pthread_rwlock_destroy(&tracker->elftab.resize_rwl);
172 [ # # ]: 0 : for (idx = 1; idx <= tracker->elftab.size; idx++)
173 : : {
174 : 0 : dwflst_tracker_elftab_ent *ent = &tracker->elftab.table[idx];
175 [ # # ]: 0 : if (ent->hashval == 0)
176 : 0 : continue;
177 : 0 : dwflst_tracker_elf_info *t =
178 : 0 : (dwflst_tracker_elf_info *) atomic_load_explicit (&ent->val_ptr,
179 : : memory_order_relaxed);
180 : 0 : free(t->module_name);
181 [ # # ]: 0 : if (t->fd >= 0)
182 : 0 : close(t->fd);
183 [ # # ]: 0 : if (t->elf != NULL)
184 : 0 : elf_end(t->elf);
185 : 0 : free(t); /* TODO: Check necessity. */
186 : : }
187 : 0 : free (tracker->elftab.table);
188 : :
189 : : /* XXX Based on lib/dynamicsizehash_concurrent.c free(). */
190 : 0 : rwlock_fini (tracker->dwfltab_lock);
191 : 0 : pthread_rwlock_destroy(&tracker->dwfltab.resize_rwl);
192 [ # # ]: 0 : for (idx = 1; idx <= tracker->dwfltab.size; idx++)
193 : : {
194 : 0 : dwflst_tracker_dwfltab_ent *ent = &tracker->dwfltab.table[idx];
195 [ # # ]: 0 : if (ent->hashval == 0)
196 : 0 : continue;
197 : 0 : dwflst_tracker_dwfl_info *t =
198 : 0 : (dwflst_tracker_dwfl_info *) atomic_load_explicit (&ent->val_ptr,
199 : : memory_order_relaxed);
200 [ # # ]: 0 : if (t->dwfl != NULL)
201 : 0 : INTUSE(dwfl_end) (t->dwfl);
202 : 0 : free(t);
203 : : }
204 : 0 : free (tracker->dwfltab.table);
205 : :
206 : 0 : free (tracker);
207 : : }
|