4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
40 #include <dt_program.h>
42 #include <dt_string.h>
44 #include <libproc_compat.h>
47 typedef struct dt_pid_probe {
48 dtrace_hdl_t *dpp_dtp;
51 struct ps_prochandle *dpp_pr;
60 uint64_t dpp_stret[4];
62 uint_t dpp_last_taken;
66 * Compose the lmid and object name into the canonical representation. We
67 * omit the lmid for the default link map for convenience.
70 dt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj)
73 if (lmid == LM_ID_BASE)
74 (void) strncpy(buf, obj, len);
76 (void) snprintf(buf, len, "LM%lx`%s", lmid, obj);
78 (void) strncpy(buf, obj, len);
83 dt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr,
84 fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...)
95 len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg),
98 if (dpr->dpr_errmsg[len - 2] == '\n')
99 dpr->dpr_errmsg[len - 2] = '\0';
101 dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region,
102 pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap);
110 dt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func)
112 dtrace_hdl_t *dtp = pp->dpp_dtp;
113 dt_pcb_t *pcb = pp->dpp_pcb;
114 dt_proc_t *dpr = pp->dpp_dpr;
115 fasttrap_probe_spec_t *ftp;
121 int isdash = strcmp("-", func) == 0;
125 pid = Pstatus(pp->dpp_pr)->pr_pid;
127 pid = proc_getpid(pp->dpp_pr);
130 dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj,
133 sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 :
134 (symp->st_size - 1) * sizeof (ftp->ftps_offs[0]));
136 if ((ftp = dt_alloc(dtp, sz)) == NULL) {
137 dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz);
138 return (1); /* errno is set for us */
142 (void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func));
144 dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid,
147 if (!isdash && gmatch("return", pp->dpp_name)) {
148 if (dt_pid_create_return_probe(pp->dpp_pr, dtp, ftp, symp,
149 pp->dpp_stret) < 0) {
150 return (dt_pid_error(dtp, pcb, dpr, ftp,
151 D_PROC_CREATEFAIL, "failed to create return probe "
152 "for '%s': %s", func,
153 dtrace_errmsg(dtp, dtrace_errno(dtp))));
159 if (!isdash && gmatch("entry", pp->dpp_name)) {
160 if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) {
161 return (dt_pid_error(dtp, pcb, dpr, ftp,
162 D_PROC_CREATEFAIL, "failed to create entry probe "
163 "for '%s': %s", func,
164 dtrace_errmsg(dtp, dtrace_errno(dtp))));
170 glob = strisglob(pp->dpp_name);
171 if (!glob && nmatches == 0) {
172 off = strtoull(pp->dpp_name, &end, 16);
174 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME,
175 "'%s' is an invalid probe name", pp->dpp_name));
178 if (off >= symp->st_size) {
179 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF,
180 "offset 0x%llx outside of function '%s'",
181 (u_longlong_t)off, func));
184 err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp,
187 if (err == DT_PROC_ERR) {
188 return (dt_pid_error(dtp, pcb, dpr, ftp,
189 D_PROC_CREATEFAIL, "failed to create probe at "
190 "'%s+0x%llx': %s", func, (u_longlong_t)off,
191 dtrace_errmsg(dtp, dtrace_errno(dtp))));
194 if (err == DT_PROC_ALIGN) {
195 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN,
196 "offset 0x%llx is not aligned on an instruction",
202 } else if (glob && !isdash) {
203 if (dt_pid_create_glob_offset_probes(pp->dpp_pr,
204 pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) {
205 return (dt_pid_error(dtp, pcb, dpr, ftp,
207 "failed to create offset probes in '%s': %s", func,
208 dtrace_errmsg(dtp, dtrace_errno(dtp))));
214 pp->dpp_nmatches += nmatches;
222 dt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func)
224 dt_pid_probe_t *pp = arg;
226 if (symp->st_shndx == SHN_UNDEF)
229 if (symp->st_size == 0) {
230 dt_dprintf("st_size of %s is zero\n", func);
234 if (pp->dpp_last_taken == 0 ||
235 symp->st_value != pp->dpp_last.st_value ||
236 symp->st_size != pp->dpp_last.st_size) {
238 * Due to 4524008, _init and _fini may have a bloated st_size.
239 * While this bug has been fixed for a while, old binaries
240 * may exist that still exhibit this problem. As a result, we
241 * don't match _init and _fini though we allow users to
242 * specify them explicitly.
244 if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0)
247 if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) {
248 pp->dpp_last = *symp;
249 return (dt_pid_per_sym(pp, symp, func));
257 dt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj)
259 dt_pid_probe_t *pp = arg;
260 dtrace_hdl_t *dtp = pp->dpp_dtp;
261 dt_pcb_t *pcb = pp->dpp_pcb;
262 dt_proc_t *dpr = pp->dpp_dpr;
269 (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
273 if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
278 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym,
280 pp->dpp_stret[0] = sym.st_value;
282 pp->dpp_stret[0] = 0;
284 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym,
286 pp->dpp_stret[1] = sym.st_value;
288 pp->dpp_stret[1] = 0;
290 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym,
292 pp->dpp_stret[2] = sym.st_value;
294 pp->dpp_stret[2] = 0;
296 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym,
298 pp->dpp_stret[3] = sym.st_value;
300 pp->dpp_stret[3] = 0;
302 pp->dpp_stret[0] = 0;
303 pp->dpp_stret[1] = 0;
304 pp->dpp_stret[2] = 0;
305 pp->dpp_stret[3] = 0;
308 dt_dprintf("%s stret %llx %llx %llx %llx\n", obj,
309 (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1],
310 (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]);
313 * If pp->dpp_func contains any globbing meta-characters, we need
314 * to iterate over the symbol table and compare each function name
315 * against the pattern.
317 if (!strisglob(pp->dpp_func)) {
319 * If we fail to lookup the symbol, try interpreting the
320 * function as the special "-" function that indicates that the
321 * probe name should be interpreted as a absolute virtual
322 * address. If that fails and we were matching a specific
323 * function in a specific module, report the error, otherwise
324 * just fail silently in the hopes that some other object will
325 * contain the desired symbol.
327 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj,
328 pp->dpp_func, &sym, NULL) != 0) {
329 if (strcmp("-", pp->dpp_func) == 0) {
332 GELF_ST_INFO(STB_LOCAL, STT_FUNC);
336 sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel ==
337 PR_MODEL_ILP32 ? -1U : -1ULL;
339 sym.st_size = ~((Elf64_Xword) 0);
342 } else if (!strisglob(pp->dpp_mod)) {
343 return (dt_pid_error(dtp, pcb, dpr, NULL,
345 "failed to lookup '%s' in module '%s'",
346 pp->dpp_func, pp->dpp_mod));
353 * Only match defined functions of non-zero size.
355 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
356 sym.st_shndx == SHN_UNDEF || sym.st_size == 0)
360 * We don't instrument PLTs -- they're dynamically rewritten,
361 * and, so, inherently dicey to instrument.
364 if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL)
368 (void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func,
369 DTRACE_FUNCNAMELEN, &sym);
371 return (dt_pid_per_sym(pp, &sym, pp->dpp_func));
373 uint_t nmatches = pp->dpp_nmatches;
375 if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB,
376 BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
379 if (nmatches == pp->dpp_nmatches) {
381 * If we didn't match anything in the PR_SYMTAB, try
384 if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM,
385 BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
394 dt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj)
396 char name[DTRACE_MODNAMELEN];
397 dt_pid_probe_t *pp = arg;
399 if (gmatch(obj, pp->dpp_mod))
400 return (dt_pid_per_mod(pp, pmp, obj));
403 (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
408 if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
413 if (gmatch(pp->dpp_obj, pp->dpp_mod))
414 return (dt_pid_per_mod(pp, pmp, obj));
417 (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
420 dt_pid_objname(name, sizeof (name), pp->dpp_lmid, pp->dpp_obj);
422 if (gmatch(name, pp->dpp_mod))
423 return (dt_pid_per_mod(pp, pmp, obj));
428 static const prmap_t *
429 dt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P)
433 Lmid_t lmid = PR_LMID_EVERY;
442 * Pick apart the link map from the library name.
444 if (strchr(pdp->dtpd_mod, '`') != NULL) {
447 if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 ||
448 !isdigit(pdp->dtpd_mod[2]))
451 lmid = strtoul(&pdp->dtpd_mod[2], &end, 16);
455 if (*end != '`' || strchr(obj, '`') != NULL)
465 if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL)
469 (void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m));
470 if ((obj = strrchr(m, '/')) == NULL)
475 (void) Plmid(P, pmp->pr_vaddr, &lmid);
478 dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj);
485 dt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
486 dt_pcb_t *pcb, dt_proc_t *dpr)
493 pp.dpp_pr = dpr->dpr_proc;
498 * We can only trace dynamically-linked executables (since we've
499 * hidden some magic in ld.so.1 as well as libc.so.1).
501 if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) {
502 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN,
503 "process %s is not a dynamically-linked executable",
504 &pdp->dtpd_provider[3]));
508 pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*";
509 pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*";
510 pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*";
511 pp.dpp_last_taken = 0;
513 if (strcmp(pp.dpp_func, "-") == 0) {
514 const prmap_t *aout, *pmp;
516 if (pdp->dtpd_mod[0] == '\0') {
517 pp.dpp_mod = pdp->dtpd_mod;
518 (void) strcpy(pdp->dtpd_mod, "a.out");
519 } else if (strisglob(pp.dpp_mod) ||
520 (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL ||
521 (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
522 aout->pr_vaddr != pmp->pr_vaddr) {
523 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB,
524 "only the a.out module is valid with the "
528 if (strisglob(pp.dpp_name)) {
529 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME,
530 "only individual addresses may be specified "
531 "with the '-' function"));
536 * If pp.dpp_mod contains any globbing meta-characters, we need
537 * to iterate over each module and compare its name against the
538 * pattern. An empty module name is treated as '*'.
540 if (strisglob(pp.dpp_mod)) {
541 ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp);
547 * If we can't find a matching module, don't sweat it -- either
548 * we'll fail the enabling because the probes don't exist or
549 * we'll wait for that module to come along.
551 if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) {
552 if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL)
557 ret = dt_pid_per_mod(&pp, pmp, obj);
565 dt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname)
567 struct ps_prochandle *P = data;
575 const char *syms[] = { "___SUNW_dof", "__SUNW_dof" };
579 * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
580 * __SUNW_dof is for actively-loaded DOF sections. We try to force
581 * in both types of DOF section since the process may not yet have
582 * run the code to instantiate these providers.
584 for (i = 0; i < 2; i++) {
585 if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym,
590 if ((mname = strrchr(oname, '/')) == NULL)
595 dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname);
597 if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr +
598 offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) {
599 dt_dprintf("read of ELF header failed");
603 dh.dofhp_dof = sym.st_value;
604 dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr;
606 dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod),
608 sip.prs_lmid, mname);
615 (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) {
616 dt_dprintf("pr_open of helper device failed: %s\n",
618 return (-1); /* errno is set for us */
621 if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0)
622 dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod);
628 (void) pr_close(P, fd);
635 dt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
636 dt_pcb_t *pcb, dt_proc_t *dpr)
638 struct ps_prochandle *P = dpr->dpr_proc;
641 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
643 (void) Pupdate_maps(P);
644 if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) {
646 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT,
647 "failed to instantiate probes for pid %d: %s",
649 (int)Pstatus(P)->pr_pid, strerror(errno));
651 (int)proc_getpid(P), strerror(errno));
659 * Put the module name in its canonical form.
661 (void) dt_pid_fix_mod(pdp, P);
667 dt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb,
671 char *c, *last = NULL, *end;
673 for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) {
678 if (last == NULL || (*(++last) == '\0')) {
679 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV,
680 "'%s' is not a valid provider", pdp->dtpd_provider);
685 pid = strtol(last, &end, 10);
687 if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) {
688 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID,
689 "'%s' does not contain a valid pid", pdp->dtpd_provider);
697 dt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb)
699 char provname[DTRACE_PROVNAMELEN];
700 struct ps_prochandle *P;
707 if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1)
710 if (dtp->dt_ftfd == -1) {
711 if (dtp->dt_fterr == ENOENT) {
712 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
713 "pid provider is not installed on this system");
715 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
716 "pid provider is not available: %s",
717 strerror(dtp->dt_fterr));
723 (void) snprintf(provname, sizeof (provname), "pid%d", (int)pid);
725 if (gmatch(provname, pdp->dtpd_provider) != 0) {
726 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE,
728 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
729 "failed to grab process %d", (int)pid);
733 dpr = dt_proc_lookup(dtp, P, 0);
735 (void) pthread_mutex_lock(&dpr->dpr_lock);
737 if ((err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr)) == 0) {
739 * Alert other retained enablings which may match
740 * against the newly created probes.
742 (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
745 (void) pthread_mutex_unlock(&dpr->dpr_lock);
746 dt_proc_release(dtp, P);
750 * If it's not strictly a pid provider, we might match a USDT provider.
752 if (strcmp(provname, pdp->dtpd_provider) != 0) {
753 if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) {
754 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
755 "failed to grab process %d", (int)pid);
759 dpr = dt_proc_lookup(dtp, P, 0);
761 (void) pthread_mutex_lock(&dpr->dpr_lock);
763 if (!dpr->dpr_usdt) {
764 err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr);
765 dpr->dpr_usdt = B_TRUE;
768 (void) pthread_mutex_unlock(&dpr->dpr_lock);
769 dt_proc_release(dtp, P);
772 return (err ? -1 : 0);
776 dt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr)
778 dtrace_enable_io_t args;
781 dtrace_probedesc_t *pdp, pd;
783 int ret = 0, found = B_FALSE;
784 char provname[DTRACE_PROVNAMELEN];
786 (void) snprintf(provname, sizeof (provname), "pid%d",
789 for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL;
790 pgp = dt_list_next(pgp)) {
792 for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL;
793 stp = dt_list_next(stp)) {
795 pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe;
796 pid = dt_pid_get_pid(pdp, dtp, NULL, dpr);
797 if (pid != dpr->dpr_pid)
804 if (gmatch(provname, pdp->dtpd_provider) != 0 &&
805 dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0)
809 * If it's not strictly a pid provider, we might match
812 if (strcmp(provname, pdp->dtpd_provider) != 0 &&
813 dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0)
820 * Give DTrace a shot to the ribs to get it to check
821 * out the newly created probes.
825 (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, &args);