Print this page
4474 DTrace Userland CTF Support
4475 DTrace userland Keyword
4476 DTrace tests should be better citizens
4479 pid provider types
4480 dof emulation missing checks
Reviewed by: Bryan Cantrill <bryan@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdtrace/common/dt_pid.c
+++ new/usr/src/lib/libdtrace/common/dt_pid.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 +/*
27 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 + */
26 29
27 30 #include <assert.h>
28 31 #include <strings.h>
29 32 #include <stdlib.h>
30 33 #include <stdio.h>
31 34 #include <errno.h>
32 35 #include <ctype.h>
33 36 #include <alloca.h>
34 37 #include <libgen.h>
35 38 #include <stddef.h>
39 +#include <sys/sysmacros.h>
36 40
37 41 #include <dt_impl.h>
38 42 #include <dt_program.h>
39 43 #include <dt_pid.h>
40 44 #include <dt_string.h>
45 +#include <dt_module.h>
41 46
42 47 typedef struct dt_pid_probe {
43 48 dtrace_hdl_t *dpp_dtp;
44 49 dt_pcb_t *dpp_pcb;
45 50 dt_proc_t *dpp_dpr;
46 51 struct ps_prochandle *dpp_pr;
47 52 const char *dpp_mod;
48 53 char *dpp_func;
49 54 const char *dpp_name;
50 55 const char *dpp_obj;
51 56 uintptr_t dpp_pc;
52 57 size_t dpp_size;
53 58 Lmid_t dpp_lmid;
54 59 uint_t dpp_nmatches;
55 60 uint64_t dpp_stret[4];
56 61 GElf_Sym dpp_last;
57 62 uint_t dpp_last_taken;
58 63 } dt_pid_probe_t;
59 64
60 65 /*
61 66 * Compose the lmid and object name into the canonical representation. We
62 67 * omit the lmid for the default link map for convenience.
63 68 */
64 69 static void
65 70 dt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj)
66 71 {
67 72 if (lmid == LM_ID_BASE)
68 73 (void) strncpy(buf, obj, len);
69 74 else
70 75 (void) snprintf(buf, len, "LM%lx`%s", lmid, obj);
71 76 }
72 77
73 78 static int
74 79 dt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr,
75 80 fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...)
76 81 {
77 82 va_list ap;
78 83 int len;
79 84
80 85 if (ftp != NULL)
81 86 dt_free(dtp, ftp);
82 87
83 88 va_start(ap, fmt);
84 89 if (pcb == NULL) {
85 90 assert(dpr != NULL);
86 91 len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg),
87 92 fmt, ap);
88 93 assert(len >= 2);
89 94 if (dpr->dpr_errmsg[len - 2] == '\n')
90 95 dpr->dpr_errmsg[len - 2] = '\0';
91 96 } else {
92 97 dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region,
93 98 pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap);
94 99 }
95 100 va_end(ap);
96 101
97 102 return (1);
98 103 }
99 104
100 105 static int
101 106 dt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func)
102 107 {
103 108 dtrace_hdl_t *dtp = pp->dpp_dtp;
104 109 dt_pcb_t *pcb = pp->dpp_pcb;
105 110 dt_proc_t *dpr = pp->dpp_dpr;
106 111 fasttrap_probe_spec_t *ftp;
107 112 uint64_t off;
108 113 char *end;
109 114 uint_t nmatches = 0;
110 115 ulong_t sz;
111 116 int glob, err;
112 117 int isdash = strcmp("-", func) == 0;
113 118 pid_t pid;
114 119
115 120 pid = Pstatus(pp->dpp_pr)->pr_pid;
116 121
117 122 dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj,
118 123 func, pp->dpp_name);
119 124
120 125 sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 :
121 126 (symp->st_size - 1) * sizeof (ftp->ftps_offs[0]));
122 127
123 128 if ((ftp = dt_alloc(dtp, sz)) == NULL) {
124 129 dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz);
125 130 return (1); /* errno is set for us */
126 131 }
127 132
128 133 ftp->ftps_pid = pid;
129 134 (void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func));
130 135
131 136 dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid,
132 137 pp->dpp_obj);
133 138
134 139 if (!isdash && gmatch("return", pp->dpp_name)) {
135 140 if (dt_pid_create_return_probe(pp->dpp_pr, dtp, ftp, symp,
136 141 pp->dpp_stret) < 0) {
137 142 return (dt_pid_error(dtp, pcb, dpr, ftp,
138 143 D_PROC_CREATEFAIL, "failed to create return probe "
139 144 "for '%s': %s", func,
140 145 dtrace_errmsg(dtp, dtrace_errno(dtp))));
141 146 }
142 147
143 148 nmatches++;
144 149 }
145 150
146 151 if (!isdash && gmatch("entry", pp->dpp_name)) {
147 152 if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) {
148 153 return (dt_pid_error(dtp, pcb, dpr, ftp,
149 154 D_PROC_CREATEFAIL, "failed to create entry probe "
150 155 "for '%s': %s", func,
151 156 dtrace_errmsg(dtp, dtrace_errno(dtp))));
152 157 }
153 158
154 159 nmatches++;
155 160 }
156 161
157 162 glob = strisglob(pp->dpp_name);
158 163 if (!glob && nmatches == 0) {
159 164 off = strtoull(pp->dpp_name, &end, 16);
160 165 if (*end != '\0') {
161 166 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME,
162 167 "'%s' is an invalid probe name", pp->dpp_name));
163 168 }
164 169
165 170 if (off >= symp->st_size) {
166 171 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF,
167 172 "offset 0x%llx outside of function '%s'",
168 173 (u_longlong_t)off, func));
169 174 }
170 175
171 176 err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp,
172 177 symp, off);
173 178
174 179 if (err == DT_PROC_ERR) {
175 180 return (dt_pid_error(dtp, pcb, dpr, ftp,
176 181 D_PROC_CREATEFAIL, "failed to create probe at "
177 182 "'%s+0x%llx': %s", func, (u_longlong_t)off,
178 183 dtrace_errmsg(dtp, dtrace_errno(dtp))));
179 184 }
180 185
181 186 if (err == DT_PROC_ALIGN) {
182 187 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN,
183 188 "offset 0x%llx is not aligned on an instruction",
184 189 (u_longlong_t)off));
185 190 }
186 191
187 192 nmatches++;
188 193
189 194 } else if (glob && !isdash) {
190 195 if (dt_pid_create_glob_offset_probes(pp->dpp_pr,
191 196 pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) {
192 197 return (dt_pid_error(dtp, pcb, dpr, ftp,
193 198 D_PROC_CREATEFAIL,
194 199 "failed to create offset probes in '%s': %s", func,
195 200 dtrace_errmsg(dtp, dtrace_errno(dtp))));
196 201 }
197 202
198 203 nmatches++;
199 204 }
200 205
201 206 pp->dpp_nmatches += nmatches;
202 207
203 208 dt_free(dtp, ftp);
204 209
205 210 return (0);
206 211 }
207 212
208 213 static int
209 214 dt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func)
210 215 {
211 216 dt_pid_probe_t *pp = arg;
212 217
213 218 if (symp->st_shndx == SHN_UNDEF)
214 219 return (0);
215 220
216 221 if (symp->st_size == 0) {
217 222 dt_dprintf("st_size of %s is zero\n", func);
218 223 return (0);
219 224 }
220 225
221 226 if (pp->dpp_last_taken == 0 ||
222 227 symp->st_value != pp->dpp_last.st_value ||
223 228 symp->st_size != pp->dpp_last.st_size) {
224 229 /*
225 230 * Due to 4524008, _init and _fini may have a bloated st_size.
226 231 * While this bug has been fixed for a while, old binaries
227 232 * may exist that still exhibit this problem. As a result, we
228 233 * don't match _init and _fini though we allow users to
229 234 * specify them explicitly.
230 235 */
231 236 if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0)
232 237 return (0);
233 238
234 239 if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) {
235 240 pp->dpp_last = *symp;
236 241 return (dt_pid_per_sym(pp, symp, func));
237 242 }
238 243 }
239 244
240 245 return (0);
241 246 }
242 247
243 248 static int
244 249 dt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj)
245 250 {
246 251 dt_pid_probe_t *pp = arg;
247 252 dtrace_hdl_t *dtp = pp->dpp_dtp;
248 253 dt_pcb_t *pcb = pp->dpp_pcb;
249 254 dt_proc_t *dpr = pp->dpp_dpr;
250 255 GElf_Sym sym;
251 256
252 257 if (obj == NULL)
253 258 return (0);
254 259
255 260 (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
256 261
257 262 if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
258 263 pp->dpp_obj = obj;
259 264 else
260 265 pp->dpp_obj++;
261 266
262 267 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym,
263 268 NULL) == 0)
264 269 pp->dpp_stret[0] = sym.st_value;
265 270 else
266 271 pp->dpp_stret[0] = 0;
267 272
268 273 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym,
269 274 NULL) == 0)
270 275 pp->dpp_stret[1] = sym.st_value;
271 276 else
272 277 pp->dpp_stret[1] = 0;
273 278
274 279 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym,
275 280 NULL) == 0)
276 281 pp->dpp_stret[2] = sym.st_value;
277 282 else
278 283 pp->dpp_stret[2] = 0;
279 284
280 285 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym,
281 286 NULL) == 0)
282 287 pp->dpp_stret[3] = sym.st_value;
283 288 else
284 289 pp->dpp_stret[3] = 0;
285 290
286 291 dt_dprintf("%s stret %llx %llx %llx %llx\n", obj,
287 292 (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1],
288 293 (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]);
289 294
290 295 /*
291 296 * If pp->dpp_func contains any globbing meta-characters, we need
292 297 * to iterate over the symbol table and compare each function name
293 298 * against the pattern.
294 299 */
295 300 if (!strisglob(pp->dpp_func)) {
296 301 /*
297 302 * If we fail to lookup the symbol, try interpreting the
298 303 * function as the special "-" function that indicates that the
299 304 * probe name should be interpreted as a absolute virtual
300 305 * address. If that fails and we were matching a specific
301 306 * function in a specific module, report the error, otherwise
302 307 * just fail silently in the hopes that some other object will
303 308 * contain the desired symbol.
304 309 */
305 310 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj,
306 311 pp->dpp_func, &sym, NULL) != 0) {
307 312 if (strcmp("-", pp->dpp_func) == 0) {
308 313 sym.st_name = 0;
309 314 sym.st_info =
310 315 GELF_ST_INFO(STB_LOCAL, STT_FUNC);
311 316 sym.st_other = 0;
312 317 sym.st_value = 0;
313 318 sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel ==
314 319 PR_MODEL_ILP32 ? -1U : -1ULL;
315 320
316 321 } else if (!strisglob(pp->dpp_mod)) {
317 322 return (dt_pid_error(dtp, pcb, dpr, NULL,
318 323 D_PROC_FUNC,
319 324 "failed to lookup '%s' in module '%s'",
320 325 pp->dpp_func, pp->dpp_mod));
321 326 } else {
322 327 return (0);
323 328 }
324 329 }
325 330
326 331 /*
327 332 * Only match defined functions of non-zero size.
328 333 */
329 334 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
330 335 sym.st_shndx == SHN_UNDEF || sym.st_size == 0)
331 336 return (0);
332 337
333 338 /*
334 339 * We don't instrument PLTs -- they're dynamically rewritten,
335 340 * and, so, inherently dicey to instrument.
336 341 */
337 342 if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL)
338 343 return (0);
339 344
340 345 (void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func,
341 346 DTRACE_FUNCNAMELEN, &sym);
342 347
343 348 return (dt_pid_per_sym(pp, &sym, pp->dpp_func));
344 349 } else {
345 350 uint_t nmatches = pp->dpp_nmatches;
346 351
347 352 if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB,
348 353 BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
349 354 return (1);
350 355
351 356 if (nmatches == pp->dpp_nmatches) {
352 357 /*
353 358 * If we didn't match anything in the PR_SYMTAB, try
354 359 * the PR_DYNSYM.
355 360 */
356 361 if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM,
357 362 BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
358 363 return (1);
359 364 }
360 365 }
361 366
362 367 return (0);
363 368 }
364 369
365 370 static int
366 371 dt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj)
367 372 {
368 373 char name[DTRACE_MODNAMELEN];
369 374 dt_pid_probe_t *pp = arg;
370 375
371 376 if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
372 377 pp->dpp_obj = obj;
373 378 else
374 379 pp->dpp_obj++;
375 380
376 381 if (gmatch(pp->dpp_obj, pp->dpp_mod))
377 382 return (dt_pid_per_mod(pp, pmp, obj));
378 383
379 384 (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
380 385
381 386 dt_pid_objname(name, sizeof (name), pp->dpp_lmid, pp->dpp_obj);
382 387
383 388 if (gmatch(name, pp->dpp_mod))
384 389 return (dt_pid_per_mod(pp, pmp, obj));
385 390
386 391 return (0);
387 392 }
388 393
389 394 static const prmap_t *
390 395 dt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P)
391 396 {
392 397 char m[MAXPATHLEN];
393 398 Lmid_t lmid = PR_LMID_EVERY;
394 399 const char *obj;
395 400 const prmap_t *pmp;
396 401
397 402 /*
398 403 * Pick apart the link map from the library name.
399 404 */
400 405 if (strchr(pdp->dtpd_mod, '`') != NULL) {
401 406 char *end;
402 407
403 408 if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 ||
404 409 !isdigit(pdp->dtpd_mod[2]))
405 410 return (NULL);
406 411
407 412 lmid = strtoul(&pdp->dtpd_mod[2], &end, 16);
408 413
409 414 obj = end + 1;
410 415
411 416 if (*end != '`' || strchr(obj, '`') != NULL)
412 417 return (NULL);
413 418
414 419 } else {
415 420 obj = pdp->dtpd_mod;
416 421 }
417 422
418 423 if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL)
419 424 return (NULL);
420 425
421 426 (void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m));
422 427 if ((obj = strrchr(m, '/')) == NULL)
423 428 obj = &m[0];
424 429 else
425 430 obj++;
426 431
427 432 (void) Plmid(P, pmp->pr_vaddr, &lmid);
428 433 dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj);
429 434
430 435 return (pmp);
431 436 }
432 437
433 438
434 439 static int
435 440 dt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
436 441 dt_pcb_t *pcb, dt_proc_t *dpr)
437 442 {
438 443 dt_pid_probe_t pp;
439 444 int ret = 0;
440 445
441 446 pp.dpp_dtp = dtp;
442 447 pp.dpp_dpr = dpr;
443 448 pp.dpp_pr = dpr->dpr_proc;
444 449 pp.dpp_pcb = pcb;
445 450
446 451 /*
447 452 * We can only trace dynamically-linked executables (since we've
448 453 * hidden some magic in ld.so.1 as well as libc.so.1).
449 454 */
450 455 if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) {
451 456 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN,
452 457 "process %s is not a dynamically-linked executable",
453 458 &pdp->dtpd_provider[3]));
454 459 }
455 460
456 461 pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*";
457 462 pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*";
458 463 pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*";
459 464 pp.dpp_last_taken = 0;
460 465
461 466 if (strcmp(pp.dpp_func, "-") == 0) {
462 467 const prmap_t *aout, *pmp;
463 468
464 469 if (pdp->dtpd_mod[0] == '\0') {
465 470 pp.dpp_mod = pdp->dtpd_mod;
466 471 (void) strcpy(pdp->dtpd_mod, "a.out");
467 472 } else if (strisglob(pp.dpp_mod) ||
468 473 (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL ||
469 474 (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
470 475 aout->pr_vaddr != pmp->pr_vaddr) {
471 476 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB,
472 477 "only the a.out module is valid with the "
473 478 "'-' function"));
474 479 }
475 480
476 481 if (strisglob(pp.dpp_name)) {
477 482 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME,
478 483 "only individual addresses may be specified "
479 484 "with the '-' function"));
480 485 }
481 486 }
482 487
483 488 /*
484 489 * If pp.dpp_mod contains any globbing meta-characters, we need
485 490 * to iterate over each module and compare its name against the
486 491 * pattern. An empty module name is treated as '*'.
487 492 */
488 493 if (strisglob(pp.dpp_mod)) {
489 494 ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp);
490 495 } else {
491 496 const prmap_t *pmp;
492 497 char *obj;
493 498
494 499 /*
495 500 * If we can't find a matching module, don't sweat it -- either
496 501 * we'll fail the enabling because the probes don't exist or
497 502 * we'll wait for that module to come along.
498 503 */
499 504 if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) {
500 505 if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL)
501 506 obj = pdp->dtpd_mod;
502 507 else
503 508 obj++;
504 509
505 510 ret = dt_pid_per_mod(&pp, pmp, obj);
506 511 }
507 512 }
508 513
509 514 return (ret);
510 515 }
511 516
512 517 static int
513 518 dt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname)
514 519 {
515 520 struct ps_prochandle *P = data;
516 521 GElf_Sym sym;
517 522 prsyminfo_t sip;
518 523 dof_helper_t dh;
519 524 GElf_Half e_type;
520 525 const char *mname;
521 526 const char *syms[] = { "___SUNW_dof", "__SUNW_dof" };
522 527 int i, fd = -1;
523 528
524 529 /*
525 530 * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
526 531 * __SUNW_dof is for actively-loaded DOF sections. We try to force
527 532 * in both types of DOF section since the process may not yet have
528 533 * run the code to instantiate these providers.
529 534 */
530 535 for (i = 0; i < 2; i++) {
531 536 if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym,
532 537 &sip) != 0) {
533 538 continue;
534 539 }
535 540
536 541 if ((mname = strrchr(oname, '/')) == NULL)
537 542 mname = oname;
538 543 else
539 544 mname++;
540 545
541 546 dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname);
542 547
543 548 if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr +
544 549 offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) {
545 550 dt_dprintf("read of ELF header failed");
546 551 continue;
547 552 }
548 553
549 554 dh.dofhp_dof = sym.st_value;
550 555 dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr;
551 556
552 557 dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod),
553 558 sip.prs_lmid, mname);
554 559
555 560 if (fd == -1 &&
556 561 (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) {
557 562 dt_dprintf("pr_open of helper device failed: %s\n",
558 563 strerror(errno));
559 564 return (-1); /* errno is set for us */
560 565 }
561 566
562 567 if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0)
563 568 dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod);
564 569 }
565 570
566 571 if (fd != -1)
567 572 (void) pr_close(P, fd);
568 573
569 574 return (0);
570 575 }
571 576
572 577 static int
573 578 dt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
574 579 dt_pcb_t *pcb, dt_proc_t *dpr)
575 580 {
576 581 struct ps_prochandle *P = dpr->dpr_proc;
577 582 int ret = 0;
578 583
579 584 assert(MUTEX_HELD(&dpr->dpr_lock));
580 585
581 586 (void) Pupdate_maps(P);
582 587 if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) {
583 588 ret = -1;
584 589 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT,
585 590 "failed to instantiate probes for pid %d: %s",
586 591 (int)Pstatus(P)->pr_pid, strerror(errno));
587 592 }
588 593
589 594 /*
590 595 * Put the module name in its canonical form.
591 596 */
592 597 (void) dt_pid_fix_mod(pdp, P);
593 598
594 599 return (ret);
595 600 }
596 601
597 602 static pid_t
598 603 dt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb,
599 604 dt_proc_t *dpr)
600 605 {
601 606 pid_t pid;
602 607 char *c, *last = NULL, *end;
603 608
604 609 for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) {
605 610 if (!isdigit(*c))
606 611 last = c;
607 612 }
608 613
609 614 if (last == NULL || (*(++last) == '\0')) {
610 615 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV,
611 616 "'%s' is not a valid provider", pdp->dtpd_provider);
612 617 return (-1);
613 618 }
614 619
615 620 errno = 0;
616 621 pid = strtol(last, &end, 10);
617 622
618 623 if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) {
619 624 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID,
620 625 "'%s' does not contain a valid pid", pdp->dtpd_provider);
621 626 return (-1);
622 627 }
623 628
624 629 return (pid);
625 630 }
626 631
627 632 int
628 633 dt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb)
629 634 {
630 635 char provname[DTRACE_PROVNAMELEN];
631 636 struct ps_prochandle *P;
632 637 dt_proc_t *dpr;
633 638 pid_t pid;
634 639 int err = 0;
635 640
636 641 assert(pcb != NULL);
637 642
638 643 if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1)
639 644 return (-1);
640 645
641 646 if (dtp->dt_ftfd == -1) {
642 647 if (dtp->dt_fterr == ENOENT) {
643 648 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
644 649 "pid provider is not installed on this system");
645 650 } else {
646 651 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
647 652 "pid provider is not available: %s",
648 653 strerror(dtp->dt_fterr));
649 654 }
650 655
651 656 return (-1);
652 657 }
653 658
654 659 (void) snprintf(provname, sizeof (provname), "pid%d", (int)pid);
655 660
656 661 if (gmatch(provname, pdp->dtpd_provider) != 0) {
657 662 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE,
658 663 0)) == NULL) {
659 664 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
660 665 "failed to grab process %d", (int)pid);
661 666 return (-1);
662 667 }
663 668
664 669 dpr = dt_proc_lookup(dtp, P, 0);
665 670 assert(dpr != NULL);
666 671 (void) pthread_mutex_lock(&dpr->dpr_lock);
667 672
668 673 if ((err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr)) == 0) {
669 674 /*
670 675 * Alert other retained enablings which may match
671 676 * against the newly created probes.
672 677 */
673 678 (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
674 679 }
675 680
676 681 (void) pthread_mutex_unlock(&dpr->dpr_lock);
677 682 dt_proc_release(dtp, P);
678 683 }
679 684
680 685 /*
681 686 * If it's not strictly a pid provider, we might match a USDT provider.
682 687 */
683 688 if (strcmp(provname, pdp->dtpd_provider) != 0) {
684 689 if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) {
685 690 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
686 691 "failed to grab process %d", (int)pid);
687 692 return (-1);
688 693 }
689 694
690 695 dpr = dt_proc_lookup(dtp, P, 0);
691 696 assert(dpr != NULL);
692 697 (void) pthread_mutex_lock(&dpr->dpr_lock);
693 698
694 699 if (!dpr->dpr_usdt) {
695 700 err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr);
696 701 dpr->dpr_usdt = B_TRUE;
697 702 }
698 703
699 704 (void) pthread_mutex_unlock(&dpr->dpr_lock);
700 705 dt_proc_release(dtp, P);
701 706 }
702 707
703 708 return (err ? -1 : 0);
704 709 }
705 710
706 711 int
707 712 dt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr)
708 713 {
709 714 dtrace_prog_t *pgp;
710 715 dt_stmt_t *stp;
711 716 dtrace_probedesc_t *pdp, pd;
712 717 pid_t pid;
713 718 int ret = 0, found = B_FALSE;
714 719 char provname[DTRACE_PROVNAMELEN];
715 720
716 721 (void) snprintf(provname, sizeof (provname), "pid%d",
717 722 (int)dpr->dpr_pid);
718 723
719 724 for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL;
720 725 pgp = dt_list_next(pgp)) {
721 726
722 727 for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL;
723 728 stp = dt_list_next(stp)) {
724 729
725 730 pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe;
726 731 pid = dt_pid_get_pid(pdp, dtp, NULL, dpr);
727 732 if (pid != dpr->dpr_pid)
728 733 continue;
729 734
730 735 found = B_TRUE;
731 736
732 737 pd = *pdp;
733 738
734 739 if (gmatch(provname, pdp->dtpd_provider) != 0 &&
735 740 dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0)
736 741 ret = 1;
737 742
738 743 /*
739 744 * If it's not strictly a pid provider, we might match
740 745 * a USDT provider.
741 746 */
742 747 if (strcmp(provname, pdp->dtpd_provider) != 0 &&
743 748 dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0)
744 749 ret = 1;
745 750 }
746 751 }
747 752
↓ open down ↓ |
697 lines elided |
↑ open up ↑ |
748 753 if (found) {
749 754 /*
750 755 * Give DTrace a shot to the ribs to get it to check
751 756 * out the newly created probes.
752 757 */
753 758 (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
754 759 }
755 760
756 761 return (ret);
757 762 }
763 +
764 +/*
765 + * libdtrace has a backroom deal with us to ask us for type information on
766 + * behalf of pid provider probes when fasttrap doesn't return any type
767 + * information. Instead we'll look up the module and see if there is type
768 + * information available. However, if there is no type information available due
769 + * to a lack of CTF data, then we want to make sure that DTrace still carries on
770 + * in face of that. As such we don't have a meaningful exit code about failure.
771 + * We emit information about why we failed to the dtrace debug log so someone
772 + * can figure it out by asking nicely for DTRACE_DEBUG.
773 + */
774 +void
775 +dt_pid_get_types(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp,
776 + dtrace_argdesc_t *adp, int *nargs)
777 +{
778 + dt_module_t *dmp;
779 + ctf_file_t *fp;
780 + ctf_funcinfo_t f;
781 + ctf_id_t argv[32];
782 + GElf_Sym sym;
783 + prsyminfo_t si;
784 + struct ps_prochandle *p;
785 + int i, args;
786 + char buf[DTRACE_ARGTYPELEN];
787 + const char *mptr;
788 + char *eptr;
789 + int ret = 0;
790 + int argc = sizeof (argv) / sizeof (ctf_id_t);
791 + Lmid_t lmid;
792 +
793 + /* Set up a potential outcome */
794 + args = *nargs;
795 + *nargs = 0;
796 +
797 + /*
798 + * If we don't have an entry or return probe then we can just stop right
799 + * now as we don't have arguments for offset probes.
800 + */
801 + if (strcmp(pdp->dtpd_name, "entry") != 0 &&
802 + strcmp(pdp->dtpd_name, "return") != 0)
803 + return;
804 +
805 + dmp = dt_module_create(dtp, pdp->dtpd_provider);
806 + if (dmp == NULL) {
807 + dt_dprintf("failed to find module for %s\n",
808 + pdp->dtpd_provider);
809 + return;
810 + }
811 + if (dt_module_load(dtp, dmp) != 0) {
812 + dt_dprintf("failed to load module for %s\n",
813 + pdp->dtpd_provider);
814 + return;
815 + }
816 +
817 + /*
818 + * We may be working with a module that doesn't have ctf. If that's the
819 + * case then we just return now and move on with life.
820 + */
821 + fp = dt_module_getctflib(dtp, dmp, pdp->dtpd_mod);
822 + if (fp == NULL) {
823 + dt_dprintf("no ctf container for %s\n",
824 + pdp->dtpd_mod);
825 + return;
826 + }
827 + p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);
828 + if (p == NULL) {
829 + dt_dprintf("failed to grab pid\n");
830 + return;
831 + }
832 + dt_proc_lock(dtp, p);
833 +
834 + /*
835 + * Check to see if the D module has a link map ID and separate that out
836 + * for properly interrogating libproc.
837 + */
838 + if ((mptr = strchr(pdp->dtpd_mod, '`')) != NULL) {
839 + if (strlen(pdp->dtpd_mod) < 3) {
840 + dt_dprintf("found weird modname with linkmap, "
841 + "aborting: %s\n", pdp->dtpd_mod);
842 + goto out;
843 + }
844 + if (pdp->dtpd_mod[0] != 'L' || pdp->dtpd_mod[1] != 'M') {
845 + dt_dprintf("missing leading 'LM', "
846 + "aborting: %s\n", pdp->dtpd_mod);
847 + goto out;
848 + }
849 + errno = 0;
850 + lmid = strtol(pdp->dtpd_mod + 2, &eptr, 16);
851 + if (errno == ERANGE || eptr != mptr) {
852 + dt_dprintf("failed to parse out lmid, aborting: %s\n",
853 + pdp->dtpd_mod);
854 + goto out;
855 + }
856 + mptr++;
857 + } else {
858 + mptr = pdp->dtpd_mod;
859 + lmid = 0;
860 + }
861 +
862 + if (Pxlookup_by_name(p, lmid, mptr, pdp->dtpd_func,
863 + &sym, &si) != 0) {
864 + dt_dprintf("failed to find function %s in %s`%s\n",
865 + pdp->dtpd_func, pdp->dtpd_provider, pdp->dtpd_mod);
866 + goto out;
867 + }
868 + if (ctf_func_info(fp, si.prs_id, &f) == CTF_ERR) {
869 + dt_dprintf("failed to get ctf information for %s in %s`%s\n",
870 + pdp->dtpd_func, pdp->dtpd_provider, pdp->dtpd_mod);
871 + goto out;
872 + }
873 +
874 + (void) snprintf(buf, sizeof (buf), "%s`%s", pdp->dtpd_provider,
875 + pdp->dtpd_mod);
876 +
877 + if (strcmp(pdp->dtpd_name, "return") == 0) {
878 + if (args < 2)
879 + goto out;
880 +
881 + bzero(adp, sizeof (dtrace_argdesc_t));
882 + adp->dtargd_ndx = 0;
883 + adp->dtargd_id = pdp->dtpd_id;
884 + adp->dtargd_mapping = adp->dtargd_ndx;
885 + /*
886 + * We explicitly leave out the library here, we only care that
887 + * it is some int. We are assuming that there is no ctf
888 + * container in here that is lying about what an int is.
889 + */
890 + (void) snprintf(adp->dtargd_native, DTRACE_ARGTYPELEN,
891 + "user %s`%s", pdp->dtpd_provider, "int");
892 + adp++;
893 + bzero(adp, sizeof (dtrace_argdesc_t));
894 + adp->dtargd_ndx = 1;
895 + adp->dtargd_id = pdp->dtpd_id;
896 + adp->dtargd_mapping = adp->dtargd_ndx;
897 + ret = snprintf(adp->dtargd_native, DTRACE_ARGTYPELEN,
898 + "userland ");
899 + (void) ctf_type_qname(fp, f.ctc_return, adp->dtargd_native +
900 + ret, DTRACE_ARGTYPELEN - ret, buf);
901 + *nargs = 2;
902 + } else {
903 + if (ctf_func_args(fp, si.prs_id, argc, argv) == CTF_ERR)
904 + goto out;
905 +
906 + *nargs = MIN(args, f.ctc_argc);
907 + for (i = 0; i < *nargs; i++, adp++) {
908 + bzero(adp, sizeof (dtrace_argdesc_t));
909 + adp->dtargd_ndx = i;
910 + adp->dtargd_id = pdp->dtpd_id;
911 + adp->dtargd_mapping = adp->dtargd_ndx;
912 + ret = snprintf(adp->dtargd_native, DTRACE_ARGTYPELEN,
913 + "userland ");
914 + (void) ctf_type_qname(fp, argv[i], adp->dtargd_native +
915 + ret, DTRACE_ARGTYPELEN - ret, buf);
916 + }
917 + }
918 +out:
919 + dt_proc_unlock(dtp, p);
920 + dt_proc_release(dtp, p);
921 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX