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