File: dwflst_process_tracker.c

package info (click to toggle)
elfutils 0.194-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,680 kB
  • sloc: ansic: 114,970; sh: 35,537; cpp: 4,998; makefile: 1,986; yacc: 1,388; lex: 130; asm: 77; sed: 39; awk: 35
file content (207 lines) | stat: -rw-r--r-- 6,129 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Track multiple Dwfl structs for multiple processes.
   Copyright (C) 2025, Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version

   or both in parallel, as here.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "libdwfl_stacktraceP.h"

#define HTAB_DEFAULT_SIZE 1021

Dwflst_Process_Tracker *dwflst_tracker_begin (const Dwfl_Callbacks *callbacks)
{
  Dwflst_Process_Tracker *tracker = calloc (1, sizeof *tracker);
  if (tracker == NULL)
    {
      __libdwfl_seterrno (DWFL_E_NOMEM);
      return tracker;
    }

  dwflst_tracker_elftab_init (&tracker->elftab, HTAB_DEFAULT_SIZE);
  rwlock_init (tracker->elftab_lock);
  dwflst_tracker_dwfltab_init (&tracker->dwfltab, HTAB_DEFAULT_SIZE);
  rwlock_init (tracker->dwfltab_lock);

  tracker->callbacks = callbacks;
  return tracker;
}

Dwfl *dwflst_tracker_dwfl_begin (Dwflst_Process_Tracker *tracker)
{
  Dwfl *dwfl = INTUSE(dwfl_begin) (tracker->callbacks);
  if (dwfl == NULL)
    return dwfl;

  /* TODO: Could also share dwfl->debuginfod, but thread-safely? */
  dwfl->tracker = tracker;

  /* XXX: dwfl added to dwfltab when dwfl->process set in dwfl_attach_state. */
  /* XXX: dwfl removed from dwfltab in dwfl_end() */

  return dwfl;
}

Dwfl *dwflst_tracker_find_pid (Dwflst_Process_Tracker *tracker,
			       pid_t pid,
			       Dwfl *(*callback) (Dwflst_Process_Tracker *,
						  pid_t, void *),
			       void *arg)
{
  Dwfl *dwfl = NULL;

  rwlock_rdlock (tracker->dwfltab_lock);
  dwflst_tracker_dwfl_info *ent
    = dwflst_tracker_dwfltab_find(&tracker->dwfltab, pid);
  rwlock_unlock (tracker->dwfltab_lock);

  if (ent != NULL && !ent->invalid)
    dwfl = ent->dwfl;
  if (dwfl == NULL && callback != NULL)
    dwfl = callback(tracker, pid, arg);
  if (dwfl != NULL)
    {
      assert (dwfl->tracker == tracker);
      /* XXX: dwfl added to dwfltab when dwfl->process set in dwfl_attach_state.
         Prior to that, the pid is not confirmed. */
    }

  return dwfl;
}

void
internal_function
__libdwfl_stacktrace_add_dwfl_to_tracker (Dwfl *dwfl) {
  Dwflst_Process_Tracker *tracker = dwfl->tracker;
  assert (tracker != NULL);

  /* First try to find an existing entry to replace: */
  dwflst_tracker_dwfl_info *ent = NULL;
  unsigned long int hval = dwfl->process->pid;

  rwlock_wrlock (tracker->dwfltab_lock);
  ent = dwflst_tracker_dwfltab_find(&tracker->dwfltab, hval);
  if (ent != NULL)
    {
      /* TODO: This is a bare-minimum solution. Ideally
         we would clean up the existing ent->dwfl, but
         this needs to be coordinated with any users of
         the dwfl library that might still be holding it. */
      ent->dwfl = dwfl;
      ent->invalid = false;
      rwlock_unlock (tracker->dwfltab_lock);
      return;
    }

  /* Only otherwise try to insert an entry: */
  ent = calloc (1, sizeof(dwflst_tracker_dwfl_info));
  if (ent == NULL)
    {
      rwlock_unlock (tracker->dwfltab_lock);
      __libdwfl_seterrno (DWFL_E_NOMEM);
      return;
    }
  ent->dwfl = dwfl;
  ent->invalid = false;
  if (dwflst_tracker_dwfltab_insert(&tracker->dwfltab, hval, ent) != 0)
    {
      free(ent);
      rwlock_unlock (tracker->dwfltab_lock);
      assert(false); /* Should not occur due to the wrlock on dwfltab. */
    }
  rwlock_unlock (tracker->dwfltab_lock);
}

void
internal_function
__libdwfl_stacktrace_remove_dwfl_from_tracker (Dwfl *dwfl) {
  if (dwfl->tracker == NULL)
    return;
  Dwflst_Process_Tracker *tracker = dwfl->tracker;
  dwflst_tracker_dwfl_info *ent = NULL;
  if (dwfl->process == NULL)
    return;
  unsigned long int hval = dwfl->process->pid;

  rwlock_wrlock (tracker->dwfltab_lock);
  ent = dwflst_tracker_dwfltab_find(&tracker->dwfltab, hval);
  if (ent != NULL && ent->dwfl == dwfl)
    {
      ent->dwfl = NULL;
      ent->invalid = true;
    }
  rwlock_unlock (tracker->dwfltab_lock);
}

void dwflst_tracker_end (Dwflst_Process_Tracker *tracker)
{
  if (tracker == NULL)
    return;

  size_t idx;

  /* HACK to allow iteration of dynamicsizehash_concurrent.  */
  /* XXX Based on lib/dynamicsizehash_concurrent.c free().  */
  rwlock_fini (tracker->elftab_lock);
  pthread_rwlock_destroy(&tracker->elftab.resize_rwl);
  for (idx = 1; idx <= tracker->elftab.size; idx++)
    {
      dwflst_tracker_elftab_ent *ent = &tracker->elftab.table[idx];
      if (ent->hashval == 0)
	continue;
      dwflst_tracker_elf_info *t =
	(dwflst_tracker_elf_info *) atomic_load_explicit (&ent->val_ptr,
							  memory_order_relaxed);
      free(t->module_name);
      if (t->fd >= 0)
	close(t->fd);
      if (t->elf != NULL)
	elf_end(t->elf);
      free(t); /* TODO: Check necessity. */
    }
  free (tracker->elftab.table);

  /* XXX Based on lib/dynamicsizehash_concurrent.c free().  */
  rwlock_fini (tracker->dwfltab_lock);
  pthread_rwlock_destroy(&tracker->dwfltab.resize_rwl);
  for (idx = 1; idx <= tracker->dwfltab.size; idx++)
    {
      dwflst_tracker_dwfltab_ent *ent = &tracker->dwfltab.table[idx];
      if (ent->hashval == 0)
	continue;
      dwflst_tracker_dwfl_info *t =
	(dwflst_tracker_dwfl_info *) atomic_load_explicit (&ent->val_ptr,
							   memory_order_relaxed);
      if (t->dwfl != NULL)
	INTUSE(dwfl_end) (t->dwfl);
      free(t);
    }
  free (tracker->dwfltab.table);

  free (tracker);
}