FDD-book/examples/part-05/ch23-debug/labs/lab23_3_ktrace.sh
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

51 lines
1 KiB
Bash
Executable file

#!/bin/sh
#
# lab23_3_ktrace.sh - Lab 23.3: User-Side Tracing with ktrace
#
# Compiles a small test program, traces it with ktrace, and shows the
# resulting kdump output.
set -e
WORKDIR=$(mktemp -d)
trap "rm -rf '$WORKDIR'" EXIT
cat > "$WORKDIR/myftest.c" <<'CEOF'
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int
main(void)
{
int fd;
char buf[64];
int arg = 0;
fd = open("/dev/myfirst0", O_RDONLY);
if (fd < 0) {
perror("open");
return (1);
}
/* A harmless ioctl that may or may not be supported */
(void)ioctl(fd, _IOR('f', 99, int), &arg);
read(fd, buf, sizeof(buf));
close(fd);
return (0);
}
CEOF
echo "=== Lab 23.3: User-Side Tracing with ktrace ==="
echo
echo "Compiling myftest.c in $WORKDIR"
cc -o "$WORKDIR/myftest" "$WORKDIR/myftest.c"
echo "Tracing the program..."
sudo ktrace -di -f "$WORKDIR/mytrace.out" "$WORKDIR/myftest"
echo
echo "Dumping the trace (relevant syscalls):"
kdump -f "$WORKDIR/mytrace.out" | grep -E 'myfirst0|CALL|RET' | head -40
echo
echo "=== End of Lab 23.3 ==="