-
Notifications
You must be signed in to change notification settings - Fork 28
/
usdt.c
321 lines (262 loc) · 8.76 KB
/
usdt.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (c) 2012, Chris Andrews. All rights reserved.
*/
#include "usdt_internal.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
char *usdt_errors[] = {
"failed to allocate memory",
"failed to allocate page-aligned memory",
"no probes defined",
"failed to load DOF: %s",
"provider is already enabled",
"failed to unload DOF: %s",
"probe named %s:%s:%s:%s already exists",
"failed to remove probe %s:%s:%s:%s"
};
static void
free_probedef(usdt_probedef_t *pd)
{
int i;
switch (pd->refcnt) {
case 1:
free((char *)pd->function);
free((char *)pd->name);
if (pd->probe) {
usdt_free_tracepoints(pd->probe);
free(pd->probe);
}
for (i = 0; i < pd->argc; i++)
free(pd->types[i]);
free(pd);
break;
case 2:
pd->refcnt = 1;
break;
default:
break;
}
}
usdt_provider_t *
usdt_create_provider(const char *name, const char *module)
{
usdt_provider_t *provider;
if ((provider = malloc(sizeof *provider)) == NULL)
return NULL;
provider->name = strdup(name);
provider->module = strdup(module);
provider->probedefs = NULL;
provider->enabled = 0;
return provider;
}
usdt_probedef_t *
usdt_create_probe(const char *func, const char *name, size_t argc, const char **types)
{
int i;
usdt_probedef_t *p;
if (argc > USDT_ARG_MAX)
argc = USDT_ARG_MAX;
if ((p = malloc(sizeof *p)) == NULL)
return (NULL);
p->refcnt = 2;
p->function = strdup(func);
p->name = strdup(name);
p->argc = argc;
p->probe = NULL;
for (i = 0; i < argc; i++)
p->types[i] = strdup(types[i]);
return (p);
}
void
usdt_probe_release(usdt_probedef_t *probedef)
{
free_probedef(probedef);
}
int
usdt_provider_add_probe(usdt_provider_t *provider, usdt_probedef_t *probedef)
{
usdt_probedef_t *pd;
if (provider->probedefs != NULL) {
for (pd = provider->probedefs; (pd != NULL); pd = pd->next) {
if ((strcmp(pd->name, probedef->name) == 0) &&
(strcmp(pd->function, probedef->function) == 0)) {
usdt_error(provider, USDT_ERROR_DUP_PROBE,
provider->name, provider->module,
probedef->function, probedef->name);
return (-1);
}
}
}
probedef->next = NULL;
if (provider->probedefs == NULL)
provider->probedefs = probedef;
else {
for (pd = provider->probedefs; (pd->next != NULL); pd = pd->next) ;
pd->next = probedef;
}
return (0);
}
int
usdt_provider_remove_probe(usdt_provider_t *provider, usdt_probedef_t *probedef)
{
usdt_probedef_t *pd, *prev_pd = NULL;
if (provider->probedefs == NULL) {
usdt_error(provider, USDT_ERROR_NOPROBES);
return (-1);
}
for (pd = provider->probedefs; (pd != NULL);
prev_pd = pd, pd = pd->next) {
if ((strcmp(pd->name, probedef->name) == 0) &&
(strcmp(pd->function, probedef->function) == 0)) {
if (prev_pd == NULL)
provider->probedefs = pd->next;
else
prev_pd->next = pd->next;
return (0);
}
}
usdt_error(provider, USDT_ERROR_REMOVE_PROBE,
provider->name, provider->module,
probedef->function, probedef->name);
return (-1);
}
int
usdt_provider_enable(usdt_provider_t *provider)
{
usdt_strtab_t strtab;
usdt_dof_file_t *file;
usdt_probedef_t *pd;
int i;
size_t size;
usdt_dof_section_t sects[5];
if (provider->enabled == 1) {
usdt_error(provider, USDT_ERROR_ALREADYENABLED);
return (0); /* not fatal */
}
if (provider->probedefs == NULL) {
usdt_error(provider, USDT_ERROR_NOPROBES);
return (-1);
}
for (pd = provider->probedefs; pd != NULL; pd = pd->next) {
if ((pd->probe = malloc(sizeof(*pd->probe))) == NULL) {
usdt_error(provider, USDT_ERROR_MALLOC);
return (-1);
}
}
if ((usdt_strtab_init(&strtab, 0)) < 0) {
usdt_error(provider, USDT_ERROR_MALLOC);
return (-1);
}
if ((usdt_strtab_add(&strtab, provider->name)) == 0) {
usdt_error(provider, USDT_ERROR_MALLOC);
return (-1);
}
if ((usdt_dof_probes_sect(§s[0], provider, &strtab)) < 0)
return (-1);
if ((usdt_dof_prargs_sect(§s[1], provider)) < 0)
return (-1);
size = usdt_provider_dof_size(provider, &strtab);
if ((file = usdt_dof_file_init(provider, size)) == NULL)
return (-1);
if ((usdt_dof_proffs_sect(§s[2], provider, file->dof)) < 0)
return (-1);
if ((usdt_dof_prenoffs_sect(§s[3], provider, file->dof)) < 0)
return (-1);
if ((usdt_dof_provider_sect(§s[4], provider)) < 0)
return (-1);
for (i = 0; i < 5; i++)
usdt_dof_file_append_section(file, §s[i]);
usdt_dof_file_generate(file, &strtab);
usdt_dof_section_free((usdt_dof_section_t *)&strtab);
for (i = 0; i < 5; i++)
usdt_dof_section_free(§s[i]);
if ((usdt_dof_file_load(file, provider->module)) < 0) {
usdt_error(provider, USDT_ERROR_LOADDOF, strerror(errno));
return (-1);
}
provider->enabled = 1;
provider->file = file;
return (0);
}
int
usdt_provider_disable(usdt_provider_t *provider)
{
usdt_probedef_t *pd;
if (provider->enabled == 0)
return (0);
if ((usdt_dof_file_unload((usdt_dof_file_t *)provider->file)) < 0) {
usdt_error(provider, USDT_ERROR_UNLOADDOF, strerror(errno));
return (-1);
}
usdt_dof_file_free(provider->file);
provider->file = NULL;
/* We would like to free the tracepoints here too, but OS X
* (and to a lesser extent Illumos) struggle with this:
*
* If a provider is repeatedly disabled and re-enabled, and is
* allowed to reuse the same memory for its tracepoints, *and*
* there's a DTrace consumer running with enablings for these
* probes, tracepoints are not always cleaned up sufficiently
* that the newly-created probes work.
*
* Here, then, we will leak the memory holding the
* tracepoints, which serves to stop us reusing the same
* memory address for new tracepoints, avoiding the bug.
*/
for (pd = provider->probedefs; (pd != NULL); pd = pd->next) {
/* may have an as yet never-enabled probe on an
otherwise enabled provider */
if (pd->probe) {
/* usdt_free_tracepoints(pd->probe); */
free(pd->probe);
pd->probe = NULL;
}
}
provider->enabled = 0;
return (0);
}
void
usdt_provider_free(usdt_provider_t *provider)
{
usdt_probedef_t *pd, *next;
for (pd = provider->probedefs; pd != NULL; pd = next) {
next = pd->next;
free_probedef(pd);
}
free((char *)provider->name);
free((char *)provider->module);
free(provider);
}
int
usdt_is_enabled(usdt_probe_t *probe)
{
if (probe != NULL)
return (*probe->isenabled_addr)();
else
return 0;
}
void
usdt_fire_probe(usdt_probe_t *probe, size_t argc, void **nargv)
{
if (probe != NULL)
usdt_probe_args(probe->probe_addr, argc, nargv);
}
static void
usdt_verror(usdt_provider_t *provider, usdt_error_t error, va_list argp)
{
vasprintf(&provider->error, usdt_errors[error], argp);
}
void
usdt_error(usdt_provider_t *provider, usdt_error_t error, ...)
{
va_list argp;
va_start(argp, error);
usdt_verror(provider, error, argp);
va_end(argp);
}
char *
usdt_errstr(usdt_provider_t *provider)
{
return (provider->error);
}