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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include <dt_program.h>
44 #include <dt_string.h>
46 typedef struct dt_pid_probe {
47 dtrace_hdl_t *dpp_dtp;
50 struct ps_prochandle *dpp_pr;
59 uint64_t dpp_stret[4];
61 uint_t dpp_last_taken;
65 * Compose the lmid and object name into the canonical representation. We
66 * omit the lmid for the default link map for convenience.
69 dt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj)
72 if (lmid == LM_ID_BASE)
73 (void) strncpy(buf, obj, len);
75 (void) snprintf(buf, len, "LM%lx`%s", lmid, obj);
77 (void) strncpy(buf, obj, len);
82 dt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr,
83 fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...)
94 len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg),
97 if (dpr->dpr_errmsg[len - 2] == '\n')
98 dpr->dpr_errmsg[len - 2] = '\0';
100 dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region,
101 pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap);
109 dt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func)
111 dtrace_hdl_t *dtp = pp->dpp_dtp;
112 dt_pcb_t *pcb = pp->dpp_pcb;
113 dt_proc_t *dpr = pp->dpp_dpr;
114 fasttrap_probe_spec_t *ftp;
120 int isdash = strcmp("-", func) == 0;
124 pid = Pstatus(pp->dpp_pr)->pr_pid;
126 pid = proc_getpid(pp->dpp_pr);
129 dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj,
132 sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 :
133 (symp->st_size - 1) * sizeof (ftp->ftps_offs[0]));
135 if ((ftp = dt_alloc(dtp, sz)) == NULL) {
136 dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz);
137 return (1); /* errno is set for us */
141 (void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func));
143 dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid,
146 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))));
160 if (!isdash && gmatch("entry", pp->dpp_name)) {
162 if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) {
163 return (dt_pid_error(dtp, pcb, dpr, ftp,
164 D_PROC_CREATEFAIL, "failed to create entry probe "
165 "for '%s': %s", func,
166 dtrace_errmsg(dtp, dtrace_errno(dtp))));
173 glob = strisglob(pp->dpp_name);
174 if (!glob && nmatches == 0) {
175 off = strtoull(pp->dpp_name, &end, 16);
177 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME,
178 "'%s' is an invalid probe name", pp->dpp_name));
181 if (off >= symp->st_size) {
182 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF,
183 "offset 0x%llx outside of function '%s'",
184 (u_longlong_t)off, func));
188 err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp,
192 if (err == DT_PROC_ERR) {
193 return (dt_pid_error(dtp, pcb, dpr, ftp,
194 D_PROC_CREATEFAIL, "failed to create probe at "
195 "'%s+0x%llx': %s", func, (u_longlong_t)off,
196 dtrace_errmsg(dtp, dtrace_errno(dtp))));
199 if (err == DT_PROC_ALIGN) {
200 return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN,
201 "offset 0x%llx is not aligned on an instruction",
207 } else if (glob && !isdash) {
209 if (dt_pid_create_glob_offset_probes(pp->dpp_pr,
210 pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) {
211 return (dt_pid_error(dtp, pcb, dpr, ftp,
213 "failed to create offset probes in '%s': %s", func,
214 dtrace_errmsg(dtp, dtrace_errno(dtp))));
221 pp->dpp_nmatches += nmatches;
229 dt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func)
231 dt_pid_probe_t *pp = arg;
233 if (symp->st_shndx == SHN_UNDEF)
236 if (symp->st_size == 0) {
237 dt_dprintf("st_size of %s is zero\n", func);
241 if (pp->dpp_last_taken == 0 ||
242 symp->st_value != pp->dpp_last.st_value ||
243 symp->st_size != pp->dpp_last.st_size) {
245 * Due to 4524008, _init and _fini may have a bloated st_size.
246 * While this bug has been fixed for a while, old binaries
247 * may exist that still exhibit this problem. As a result, we
248 * don't match _init and _fini though we allow users to
249 * specify them explicitly.
251 if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0)
254 if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) {
255 pp->dpp_last = *symp;
256 return (dt_pid_per_sym(pp, symp, func));
264 dt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj)
266 dt_pid_probe_t *pp = arg;
267 dtrace_hdl_t *dtp = pp->dpp_dtp;
268 dt_pcb_t *pcb = pp->dpp_pcb;
269 dt_proc_t *dpr = pp->dpp_dpr;
276 (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
280 if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
286 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym,
288 pp->dpp_stret[0] = sym.st_value;
290 pp->dpp_stret[0] = 0;
292 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym,
294 pp->dpp_stret[1] = sym.st_value;
296 pp->dpp_stret[1] = 0;
298 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym,
300 pp->dpp_stret[2] = sym.st_value;
302 pp->dpp_stret[2] = 0;
304 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym,
306 pp->dpp_stret[3] = sym.st_value;
308 pp->dpp_stret[3] = 0;
310 if (proc_name2sym(pp->dpp_pr, obj, ".stret1", &sym) == 0)
311 pp->dpp_stret[0] = sym.st_value;
313 pp->dpp_stret[0] = 0;
315 if (proc_name2sym(pp->dpp_pr, obj, ".stret2", &sym) == 0)
316 pp->dpp_stret[1] = sym.st_value;
318 pp->dpp_stret[1] = 0;
320 if (proc_name2sym(pp->dpp_pr, obj, ".stret4", &sym) == 0)
321 pp->dpp_stret[2] = sym.st_value;
323 pp->dpp_stret[2] = 0;
325 if (proc_name2sym(pp->dpp_pr, obj, ".stret8", &sym) == 0)
326 pp->dpp_stret[3] = sym.st_value;
328 pp->dpp_stret[3] = 0;
331 dt_dprintf("%s stret %llx %llx %llx %llx\n", obj,
332 (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1],
333 (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]);
336 * If pp->dpp_func contains any globbing meta-characters, we need
337 * to iterate over the symbol table and compare each function name
338 * against the pattern.
340 if (!strisglob(pp->dpp_func)) {
342 * If we fail to lookup the symbol, try interpreting the
343 * function as the special "-" function that indicates that the
344 * probe name should be interpreted as a absolute virtual
345 * address. If that fails and we were matching a specific
346 * function in a specific module, report the error, otherwise
347 * just fail silently in the hopes that some other object will
348 * contain the desired symbol.
351 if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj,
352 pp->dpp_func, &sym, NULL) != 0) {
354 if (proc_name2sym(pp->dpp_pr, obj, pp->dpp_func, &sym) != 0) {
356 if (strcmp("-", pp->dpp_func) == 0) {
359 GELF_ST_INFO(STB_LOCAL, STT_FUNC);
363 sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel ==
364 PR_MODEL_ILP32 ? -1U : -1ULL;
366 sym.st_size = ~((Elf64_Xword) 0);
369 } else if (!strisglob(pp->dpp_mod)) {
370 return (dt_pid_error(dtp, pcb, dpr, NULL,
372 "failed to lookup '%s' in module '%s'",
373 pp->dpp_func, pp->dpp_mod));
380 * Only match defined functions of non-zero size.
382 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
383 sym.st_shndx == SHN_UNDEF || sym.st_size == 0)
387 * We don't instrument PLTs -- they're dynamically rewritten,
388 * and, so, inherently dicey to instrument.
391 if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL)
396 (void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func,
398 (void) proc_addr2sym(pp->dpp_pr, sym.st_value, pp->dpp_func,
400 DTRACE_FUNCNAMELEN, &sym);
402 return (dt_pid_per_sym(pp, &sym, pp->dpp_func));
405 uint_t nmatches = pp->dpp_nmatches;
407 if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB,
408 BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
411 if (nmatches == pp->dpp_nmatches) {
413 * If we didn't match anything in the PR_SYMTAB, try
416 if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM,
417 BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
427 dt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj)
429 char name[DTRACE_MODNAMELEN];
430 dt_pid_probe_t *pp = arg;
432 if (gmatch(obj, pp->dpp_mod))
433 return (dt_pid_per_mod(pp, pmp, obj));
436 (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
441 if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
446 dt_pid_objname(name, sizeof (name), pp->dpp_lmid, obj);
448 if (gmatch(name, pp->dpp_mod))
449 return (dt_pid_per_mod(pp, pmp, obj));
454 static const prmap_t *
455 dt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P)
459 Lmid_t lmid = PR_LMID_EVERY;
466 * Pick apart the link map from the library name.
468 if (strchr(pdp->dtpd_mod, '`') != NULL) {
471 if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 ||
472 !isdigit(pdp->dtpd_mod[2]))
475 lmid = strtoul(&pdp->dtpd_mod[2], &end, 16);
479 if (*end != '`' || strchr(obj, '`') != NULL)
486 if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL)
489 (void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m));
490 if ((obj = strrchr(m, '/')) == NULL)
495 (void) Plmid(P, pmp->pr_vaddr, &lmid);
497 dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj);
507 dt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
508 dt_pcb_t *pcb, dt_proc_t *dpr)
515 pp.dpp_pr = dpr->dpr_proc;
520 * We can only trace dynamically-linked executables (since we've
521 * hidden some magic in ld.so.1 as well as libc.so.1).
523 if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) {
524 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN,
525 "process %s is not a dynamically-linked executable",
526 &pdp->dtpd_provider[3]));
530 pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*";
531 pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*";
532 pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*";
533 pp.dpp_last_taken = 0;
535 if (strcmp(pp.dpp_func, "-") == 0) {
536 const prmap_t *aout, *pmp;
538 if (pdp->dtpd_mod[0] == '\0') {
539 pp.dpp_mod = pdp->dtpd_mod;
540 (void) strcpy(pdp->dtpd_mod, "a.out");
541 } else if (strisglob(pp.dpp_mod) ||
543 (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL ||
544 (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
546 (aout = proc_name2map(pp.dpp_pr, "a.out")) == NULL ||
547 (pmp = proc_name2map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
549 aout->pr_vaddr != pmp->pr_vaddr) {
550 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB,
551 "only the a.out module is valid with the "
555 if (strisglob(pp.dpp_name)) {
556 return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME,
557 "only individual addresses may be specified "
558 "with the '-' function"));
563 * If pp.dpp_mod contains any globbing meta-characters, we need
564 * to iterate over each module and compare its name against the
565 * pattern. An empty module name is treated as '*'.
568 if (strisglob(pp.dpp_mod)) {
569 ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp);
575 * If we can't find a matching module, don't sweat it -- either
576 * we'll fail the enabling because the probes don't exist or
577 * we'll wait for that module to come along.
579 if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) {
580 if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL)
585 ret = dt_pid_per_mod(&pp, pmp, obj);
594 dt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname)
596 struct ps_prochandle *P = data;
604 const char *syms[] = { "___SUNW_dof", "__SUNW_dof" };
608 * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
609 * __SUNW_dof is for actively-loaded DOF sections. We try to force
610 * in both types of DOF section since the process may not yet have
611 * run the code to instantiate these providers.
613 for (i = 0; i < 2; i++) {
615 if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym,
618 if (proc_name2sym(P, oname, syms[i], &sym) != 0) {
623 if ((mname = strrchr(oname, '/')) == NULL)
628 dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname);
631 if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr +
632 offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) {
633 dt_dprintf("read of ELF header failed");
638 dh.dofhp_dof = sym.st_value;
639 dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr;
641 dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod),
643 sip.prs_lmid, mname);
650 (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) {
651 dt_dprintf("pr_open of helper device failed: %s\n",
653 return (-1); /* errno is set for us */
656 if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0)
657 dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod);
663 (void) pr_close(P, fd);
670 dt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
671 dt_pcb_t *pcb, dt_proc_t *dpr)
673 struct ps_prochandle *P = dpr->dpr_proc;
676 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
679 (void) Pupdate_maps(P);
680 if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) {
682 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT,
683 "failed to instantiate probes for pid %d: %s",
685 (int)Pstatus(P)->pr_pid, strerror(errno));
687 (int)proc_getpid(P), strerror(errno));
693 * Put the module name in its canonical form.
695 (void) dt_pid_fix_mod(pdp, P);
701 dt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb,
705 char *c, *last = NULL, *end;
707 for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) {
712 if (last == NULL || (*(++last) == '\0')) {
713 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV,
714 "'%s' is not a valid provider", pdp->dtpd_provider);
719 pid = strtol(last, &end, 10);
721 if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) {
722 (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID,
723 "'%s' does not contain a valid pid", pdp->dtpd_provider);
731 dt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb)
733 char provname[DTRACE_PROVNAMELEN];
734 struct ps_prochandle *P;
741 if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1)
744 if (dtp->dt_ftfd == -1) {
745 if (dtp->dt_fterr == ENOENT) {
746 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
747 "pid provider is not installed on this system");
749 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
750 "pid provider is not available: %s",
751 strerror(dtp->dt_fterr));
757 (void) snprintf(provname, sizeof (provname), "pid%d", (int)pid);
759 if (gmatch(provname, pdp->dtpd_provider) != 0) {
760 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE,
762 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
763 "failed to grab process %d", (int)pid);
767 dpr = dt_proc_lookup(dtp, P, 0);
769 (void) pthread_mutex_lock(&dpr->dpr_lock);
771 if ((err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr)) == 0) {
773 * Alert other retained enablings which may match
774 * against the newly created probes.
776 (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
779 (void) pthread_mutex_unlock(&dpr->dpr_lock);
780 dt_proc_release(dtp, P);
784 * If it's not strictly a pid provider, we might match a USDT provider.
786 if (strcmp(provname, pdp->dtpd_provider) != 0) {
787 if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) {
788 (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
789 "failed to grab process %d", (int)pid);
793 dpr = dt_proc_lookup(dtp, P, 0);
795 (void) pthread_mutex_lock(&dpr->dpr_lock);
797 if (!dpr->dpr_usdt) {
798 err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr);
799 dpr->dpr_usdt = B_TRUE;
802 (void) pthread_mutex_unlock(&dpr->dpr_lock);
803 dt_proc_release(dtp, P);
806 return (err ? -1 : 0);
810 dt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr)
812 dtrace_enable_io_t args;
815 dtrace_probedesc_t *pdp, pd;
817 int ret = 0, found = B_FALSE;
818 char provname[DTRACE_PROVNAMELEN];
820 (void) snprintf(provname, sizeof (provname), "pid%d",
823 for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL;
824 pgp = dt_list_next(pgp)) {
826 for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL;
827 stp = dt_list_next(stp)) {
829 pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe;
830 pid = dt_pid_get_pid(pdp, dtp, NULL, dpr);
831 if (pid != dpr->dpr_pid)
838 if (gmatch(provname, pdp->dtpd_provider) != 0 &&
839 dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0)
843 * If it's not strictly a pid provider, we might match
846 if (strcmp(provname, pdp->dtpd_provider) != 0 &&
847 dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0)
854 * Give DTrace a shot to the ribs to get it to check
855 * out the newly created probes.
859 (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, &args);