/*-
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
if (ktracefile)
(void)unlink(ktracefile);
(void)errx(EXIT_FAILURE,
"ktrace(2) system call not supported in the running"
" kernel; re-compile kernel with `options KTRACE'");
}
static int
do_ktrace(const char *tracefile, int vers, int ops, int trpoints, int pid,
int block)
{
int ret;
ops |= vers << KTRFAC_VER_SHIFT;
if (KTROP(ops) == KTROP_SET &&
(!tracefile || strcmp(tracefile, "-") == 0)) {
int pi[2], dofork;
if (pipe2(pi, O_CLOEXEC) == -1)
err(EXIT_FAILURE, "pipe(2)");
dofork = (pid == getpid());
if (dofork) {
#ifdef KTRUSS
/*
* Create a child process and trace it.
*/
pid = fork();
if (pid == -1)
err(EXIT_FAILURE, "fork");
else if (pid == 0) {
pid = getpid();
goto trace_and_exec;
}
#else
int fpid;
/*
* Create a dumper process and we will be
* traced.
*/
fpid = fork();
if (fpid == -1)
err(EXIT_FAILURE, "fork");
else if (fpid != 0)
goto trace_and_exec;
#endif
(void)close(pi[1]);
} else {
ret = fktrace(pi[1], ops, trpoints, pid);
if (ret == -1)
err(EXIT_FAILURE, "fd %d, pid %d",
pi[1], pid);
if (block)
fclear(pi[1], O_NONBLOCK);
}
#ifdef KTRUSS
dumpfile(NULL, pi[0], trpoints);
waitpid(pid, NULL, 0);
#else
{
char buf[BUFSIZ];
int n;
while ((n =
read(pi[0], buf, sizeof(buf))) > 0)
if (write(STDOUT_FILENO, buf, (size_t)n) == -1)
warn("write failed");
}
if (dofork)
_exit(0);
#endif
return 0;