From 2900d1904b371976904468fbbfdee241810b526e Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sat, 24 Oct 2009 00:27:16 +0000 Subject: [PATCH] Now also compiles on NetBSD (although untested) --- Makefile | 1 + src/Makefile | 7 +++++-- src/linux.c | 7 +++++-- src/linux.h | 15 ++++++++++---- src/netbsd.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/netbsd.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ src/project.conf | 8 +++++++- src/strace.c | 28 +++++++++++++------------ 8 files changed, 146 insertions(+), 22 deletions(-) create mode 100644 src/netbsd.c create mode 100644 src/netbsd.h diff --git a/Makefile b/Makefile index 2cbda41..69d5f61 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ dist: $(LN) -s . $(PACKAGE)-$(VERSION) @$(TAR) $(PACKAGE)-$(VERSION).tar.gz \ $(PACKAGE)-$(VERSION)/src/linux.c \ + $(PACKAGE)-$(VERSION)/src/netbsd.c \ $(PACKAGE)-$(VERSION)/src/strace.c \ $(PACKAGE)-$(VERSION)/src/Makefile \ $(PACKAGE)-$(VERSION)/src/project.conf \ diff --git a/src/Makefile b/src/Makefile index 2e21540..a79243e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -15,16 +15,19 @@ INSTALL = install all: $(TARGETS) -strace_OBJS = linux.o strace.o +strace_OBJS = linux.o netbsd.o strace.o strace_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) strace_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) strace: $(strace_OBJS) $(CC) -o strace $(strace_OBJS) $(strace_LDFLAGS) -linux.o: linux.c +linux.o: linux.c linux.h $(CC) $(strace_CFLAGS) -c linux.c +netbsd.o: netbsd.c netbsd.h + $(CC) $(strace_CFLAGS) -c netbsd.c + strace.o: strace.c $(CC) $(strace_CFLAGS) -c strace.c diff --git a/src/linux.c b/src/linux.c index f56ca0b..d433a48 100644 --- a/src/linux.c +++ b/src/linux.c @@ -1,5 +1,5 @@ /* $Id$ */ -/* Copyright (c) 2007 Pierre Pronchery */ +/* Copyright (c) 2009 Pierre Pronchery */ /* This file is part of DeforaOS Devel strace */ /* strace is not free software; you can redistribute it and/or modify it under * the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 @@ -20,7 +20,9 @@ #include "linux.h" -char const * _syscall[] = +#ifdef __linux__ +/* variables */ +char const * stracecall[] = { "exit", "fork", @@ -43,3 +45,4 @@ char const * _syscall[] = "lseek", "getpid" }; +#endif /* __linux__ */ diff --git a/src/linux.h b/src/linux.h index 66c19fe..34918ff 100644 --- a/src/linux.h +++ b/src/linux.h @@ -1,5 +1,5 @@ /* $Id$ */ -/* Copyright (c) 2007 Pierre Pronchery */ +/* Copyright (c) 2009 Pierre Pronchery */ /* This file is part of DeforaOS Devel strace */ /* strace is not free software; you can redistribute it and/or modify it under * the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 @@ -19,10 +19,17 @@ #ifndef STRACE_LINUX_H # define STRACE_LINUX_H -# include -# include +# ifdef __linux__ +# include +# include -extern char * syscall[__NR_getpid]; +/* types */ +typedef void * ptrace_data_t; + + +/* variables */ +extern char * stracecall[__NR_getpid]; +# endif /* __linux__ */ #endif /* !STRACE_LINUX_H */ diff --git a/src/netbsd.c b/src/netbsd.c new file mode 100644 index 0000000..c429cad --- /dev/null +++ b/src/netbsd.c @@ -0,0 +1,49 @@ +/* $Id$ */ +/* Copyright (c) 2009 Pierre Pronchery */ +/* This file is part of DeforaOS Devel strace */ +/* strace is not free software; you can redistribute it and/or modify it under + * the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 + * Unported as published by the Creative Commons organization. + * + * strace is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial- + * ShareAlike 3.0 Unported license for more details. + * + * You should have received a copy of the Creative Commons Attribution- + * NonCommercial-ShareAlike 3.0 along with strace; if not, browse to + * http://creativecommons.org/licenses/by-nc-sa/3.0/ */ + + + +#include +#include "netbsd.h" + + +#ifdef __NetBSD__ +/* variables */ +char const * stracecall[] = +{ + NULL, + "exit", + "fork", + "read", + "write", + "open", + "close", + "wait4", + "oldcreat", + "link", + "unlink", + "oldexecv", + "chdir", + "fchdir", + "mknod", + "chmod", + "chown", + "break", + NULL, + "oldlseek", + "getpid" +}; +#endif /* __NetBSD__ */ diff --git a/src/netbsd.h b/src/netbsd.h new file mode 100644 index 0000000..bd2067b --- /dev/null +++ b/src/netbsd.h @@ -0,0 +1,53 @@ +/* $Id$ */ +/* Copyright (c) 2009 Pierre Pronchery */ +/* This file is part of DeforaOS Devel strace */ +/* strace is not free software; you can redistribute it and/or modify it under + * the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 + * Unported as published by the Creative Commons organization. + * + * strace is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial- + * ShareAlike 3.0 Unported license for more details. + * + * You should have received a copy of the Creative Commons Attribution- + * NonCommercial-ShareAlike 3.0 along with strace; if not, browse to + * http://creativecommons.org/licenses/by-nc-sa/3.0/ */ + + + +#ifndef STRACE_NETBSD_H +# define STRACE_NETBSD_H + +# ifdef __NetBSD__ +# include +# include + + +/* types */ +typedef long ptrace_data_t; /* XXX really is int */ +struct user +{ + struct reg regs; +}; + + +/* constants */ +# define PTRACE_CONT PT_CONTINUE +# define PTRACE_GETREGS PT_GETREGS +# define PTRACE_SYSCALL PT_SYSCALL +# define PTRACE_TRACEME PT_TRACE_ME + +# if defined(__amd64__) +# define _REG32_EAX 11 /* XXX or #define _KERNEL */ +# define orig_eax regs[_REG32_EAX] +# elif defined(__i386__) +# define orig_eax r_eax +# endif + + +/* variables */ +extern char const * stracecall[SYS_getpid + 1]; +# endif /* __NetBSD__ */ + +#endif /* !STRACE_NETBSD_H */ diff --git a/src/project.conf b/src/project.conf index f7d5dcb..b0b8539 100644 --- a/src/project.conf +++ b/src/project.conf @@ -5,5 +5,11 @@ dist=Makefile [strace] type=binary -sources=linux.c,strace.c +sources=linux.c,netbsd.c,strace.c install=$(BINDIR) + +[linux.c] +depends=linux.h + +[netbsd.c] +depends=netbsd.h diff --git a/src/strace.c b/src/strace.c index ee8520f..724569d 100644 --- a/src/strace.c +++ b/src/strace.c @@ -1,5 +1,5 @@ /* $Id$ */ -/* Copyright (c) 2007 Pierre Pronchery */ +/* Copyright (c) 2009 Pierre Pronchery */ /* This file is part of DeforaOS Devel strace */ /* strace is not free software; you can redistribute it and/or modify it under * the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 @@ -17,17 +17,18 @@ #include -#include #include #include +#include #include -#ifdef __linux__ -# include "linux.h" -#endif +#include +#include "linux.h" +#include "netbsd.h" /* strace */ static int _strace_parent(pid_t pid); + static int _strace(char * argv[]) { pid_t pid; @@ -39,7 +40,7 @@ static int _strace(char * argv[]) } if(pid == 0) { - ptrace(PTRACE_TRACEME); + ptrace(PTRACE_TRACEME, -1, NULL, (ptrace_data_t)NULL); execvp(argv[0], argv); fprintf(stderr, "%s", "strace: "); perror(argv[0]); @@ -64,22 +65,23 @@ static int _strace_parent(pid_t pid) static int _handle(pid_t pid, int status) { struct user context; - int size = sizeof(_syscall) / 4; + int size = sizeof(stracecall) / 4; if(!WIFSTOPPED(status)) return -1; switch(WSTOPSIG(status)) { case SIGTRAP: - ptrace(PTRACE_GETREGS, pid, NULL, &context); + ptrace(PTRACE_GETREGS, pid, NULL, + (ptrace_data_t)&context); if(size >= context.regs.orig_eax) - fprintf(stderr, "%s();\n", - _syscall[context.regs.orig_eax - 1]); + fprintf(stderr, "%s();\n", stracecall[ + context.regs.orig_eax - 1]); else fprintf(stderr, "%ld\n", context.regs.orig_eax); - ptrace(PTRACE_SYSCALL, pid, NULL, NULL); + ptrace(PTRACE_SYSCALL, pid, NULL, (ptrace_data_t)NULL); wait(0); - ptrace(PTRACE_SYSCALL, pid, NULL, NULL); + ptrace(PTRACE_SYSCALL, pid, NULL, (ptrace_data_t)NULL); break; default: ptrace(PTRACE_CONT, pid, NULL, WSTOPSIG(status)); @@ -92,7 +94,7 @@ static int _handle(pid_t pid, int status) /* usage */ static int _usage(void) { - fprintf(stderr, "%s", "Usage: strace program [argument...]\n"); + fputs("Usage: strace program [argument...]\n", stderr); return 1; }