xref: /titanic_41/usr/src/lib/libast/common/misc/systrace.c (revision e5ba14ff435beeefdaa2e6649e175c74afe02c76)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * trace systems calls if possible
26  */
27 
28 #include <ast.h>
29 #include <error.h>
30 #include <proc.h>
31 #include <debug.h>
32 
33 void
34 systrace(const char* id)
35 {
36 	register int	n;
37 	register char*	out;
38 	char*		s;
39 	char		buf[PATH_MAX];
40 	char*		av[7];
41 	long		ov[2];
42 
43 	static char*	trace[] = { "trace", "truss", "strace", "traces" };
44 
45 	if (!(s = getenv("HOME")))
46 		return;
47 	if (!id && !(id = (const char*)error_info.id))
48 		id = (const char*)trace[0];
49 	out = buf;
50 	out += sfsprintf(out, sizeof(buf), "%s/.%s/%s", s, trace[0], id);
51 	if (access(buf, F_OK))
52 		return;
53 	av[1] = trace[0];
54 	av[2] = "-o";
55 	av[3] = buf;
56 	av[4] = "-p";
57 	av[5] = out + 1;
58 	av[6] = 0;
59 	ov[0] = PROC_FD_DUP(open("/dev/null", O_WRONLY), 2, PROC_FD_PARENT|PROC_FD_CHILD);
60 	ov[1] = 0;
61 	sfsprintf(out, &buf[sizeof(buf)] - out, ".%d", getpid());
62 	for (n = 0; n < elementsof(trace); n++)
63 		if (!procfree(procopen(trace[n], av + 1, NiL, ov, PROC_ARGMOD|PROC_GID|PROC_UID|(n == (elementsof(trace) - 1) ? PROC_CLEANUP : 0))))
64 		{
65 			sleep(1);
66 			break;
67 		}
68 }
69