xref: /onnv-gate/usr/src/lib/libast/common/misc/systrace.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin /*
254887Schin  * trace systems calls if possible
264887Schin  */
274887Schin 
284887Schin #include <ast.h>
294887Schin #include <error.h>
304887Schin #include <proc.h>
314887Schin #include <debug.h>
324887Schin 
334887Schin void
systrace(const char * id)344887Schin systrace(const char* id)
354887Schin {
364887Schin 	register int	n;
374887Schin 	register char*	out;
384887Schin 	char*		s;
394887Schin 	char		buf[PATH_MAX];
404887Schin 	char*		av[7];
414887Schin 	long		ov[2];
424887Schin 
434887Schin 	static char*	trace[] = { "trace", "truss", "strace", "traces" };
444887Schin 
454887Schin 	if (!(s = getenv("HOME")))
464887Schin 		return;
474887Schin 	if (!id && !(id = (const char*)error_info.id))
484887Schin 		id = (const char*)trace[0];
494887Schin 	out = buf;
504887Schin 	out += sfsprintf(out, sizeof(buf), "%s/.%s/%s", s, trace[0], id);
514887Schin 	if (access(buf, F_OK))
524887Schin 		return;
534887Schin 	av[1] = trace[0];
544887Schin 	av[2] = "-o";
554887Schin 	av[3] = buf;
564887Schin 	av[4] = "-p";
574887Schin 	av[5] = out + 1;
584887Schin 	av[6] = 0;
594887Schin 	ov[0] = PROC_FD_DUP(open("/dev/null", O_WRONLY), 2, PROC_FD_PARENT|PROC_FD_CHILD);
604887Schin 	ov[1] = 0;
614887Schin 	sfsprintf(out, &buf[sizeof(buf)] - out, ".%d", getpid());
624887Schin 	for (n = 0; n < elementsof(trace); n++)
634887Schin 		if (!procfree(procopen(trace[n], av + 1, NiL, ov, PROC_ARGMOD|PROC_GID|PROC_UID|(n == (elementsof(trace) - 1) ? PROC_CLEANUP : 0))))
644887Schin 		{
654887Schin 			sleep(1);
664887Schin 			break;
674887Schin 		}
684887Schin }
69