From cce6dac16bbbdd3b20203f66f8aa022b46d413ea Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 30 May 2020 13:51:35 +0200 Subject: Print errno names and strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 1 + Makefile | 11 +++++++++-- sctrace.c | 52 ++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 511671e..aebf3a8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.o *.su /sctrace +/list-errnos.h diff --git a/Makefile b/Makefile index f24282a..1d032bc 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,16 @@ include $(CONFIGFILE) all: sctrace -sctrace: sctrace.c arg.h +sctrace: sctrace.c arg.h list-errnos.h $(CC) -o $@ $@.c $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) +list-errnos.h: + printf '#define LIST_ERRNOS(_)\\\n\t' > $@ + cat /usr/include/asm-generic/errno.h /usr/include/asm-generic/errno-base.h \ + | sed 's/\/\/.*$$//' | tr -d '$$' | sed 's/\*\//\$$/g' | sed 's/\/\*[^$$]*\$$//g' \ + | sed -n '/^[ \t]*#[ \t]*define[ \t].*[ \t][0-9]*[ \t]*$$/s/^[ \t#]*define[ \t]*\([^ \t]*\).*$$/_(\1)/p' \ + | sort | uniq | tr '\n' '#' | sed 's/#_/\\\n\t_/g' | tr '#' '\n' >> $@ + install: sctrace mkdir -p -- "$(DESTDIR)$(PREFIX)/bin" cp -- sctrace "$(DESTDIR)$(PREFIX)/bin" @@ -16,6 +23,6 @@ uninstall: -rm -f -- "$(DESTDIR)$(PREFIX)/bin/sctrace" clean: - -rm -f -- *.o sctrace + -rm -f -- *.o list-errnos.h sctrace .PHONY: all install uninstall clean diff --git a/sctrace.c b/sctrace.c index ed574e3..fa991d4 100644 --- a/sctrace.c +++ b/sctrace.c @@ -25,6 +25,7 @@ #include #include "arg.h" +#include "list-errnos.h" char *argv0; @@ -57,6 +58,20 @@ usage(void) } +static const char * +get_errno_name(int err) +{ + static char buf[3 * sizeof(err) + 2]; + +#define X(N) if (err == N) return #N; + LIST_ERRNOS(X) +#undef X + + sprintf(buf, "%i", err); + return buf; +} + + static char * get_string(pid_t pid, unsigned long int addr, size_t *lenp, const char **errorp) { @@ -162,7 +177,7 @@ utf8len(char *str) return 0; found: - code = (uint32_t)(s[0] & lookup[ext].mask); + code = s[0] & lookup[ext].mask; len = ext + 1; for (i = 1; i < len; i++) { if ((s[i] & 0xC0) != 0x80) @@ -781,6 +796,7 @@ main(int argc, char **argv) char *outfile = NULL; FILE *outfp = stderr; const char *num = NULL; + int err; /* TODO add option to trace children */ /* TODO add option to trace threads */ @@ -909,35 +925,39 @@ have_outfp: } /* Print system call result */ - /* TODO print error name (not one all system calls) */ if (ret_type == Int) - fprintf(outfp, " = %i\n", (int)regs.rax); + fprintf(outfp, " = %i", (int)regs.rax); else if (ret_type == UInt) - fprintf(outfp, " = %u\n", (unsigned int)regs.rax); + fprintf(outfp, " = %u", (unsigned int)regs.rax); else if (ret_type == OInt) - fprintf(outfp, " = %#o\n", (unsigned int)regs.rax); + fprintf(outfp, " = %#o", (unsigned int)regs.rax); else if (ret_type == XInt) - fprintf(outfp, " = %#x\n", (unsigned int)regs.rax); + fprintf(outfp, " = %#x", (unsigned int)regs.rax); else if (ret_type == Long) - fprintf(outfp, " = %li\n", (long int)regs.rax); + fprintf(outfp, " = %li", (long int)regs.rax); else if (ret_type == ULong) - fprintf(outfp, " = %lu\n", (unsigned long int)regs.rax); + fprintf(outfp, " = %lu", (unsigned long int)regs.rax); else if (ret_type == OLong) - fprintf(outfp, " = %#lo\n", (unsigned long int)regs.rax); + fprintf(outfp, " = %#lo", (unsigned long int)regs.rax); else if (ret_type == XLong) - fprintf(outfp, " = %#lx\n", (unsigned long int)regs.rax); + fprintf(outfp, " = %#lx", (unsigned long int)regs.rax); else if (ret_type == LLong) - fprintf(outfp, " = %lli\n", (long long int)regs.rax); + fprintf(outfp, " = %lli", (long long int)regs.rax); else if (ret_type == ULLong) - fprintf(outfp, " = %llu\n", (unsigned long long int)regs.rax); + fprintf(outfp, " = %llu", (unsigned long long int)regs.rax); else if (ret_type == OLLong) - fprintf(outfp, " = %#llo\n", (unsigned long long int)regs.rax); + fprintf(outfp, " = %#llo", (unsigned long long int)regs.rax); else if (ret_type == XLLong) - fprintf(outfp, " = %#llx\n", (unsigned long long int)regs.rax); + fprintf(outfp, " = %#llx", (unsigned long long int)regs.rax); else if (ret_type == Ptr && (long long int)regs.rax >= 0) - fprintf(outfp, " = %p\n", (void *)regs.rax); + fprintf(outfp, " = %p", (void *)regs.rax); else - fprintf(outfp, " = %li\n", (long int)regs.rax); + fprintf(outfp, " = %li", (long int)regs.rax); + if ((unsigned long long int)regs.rax > -(unsigned long long int)PAGE_SIZE) { + err = -(int)regs.rax; + fprintf(outfp, " (%s: %s)", get_errno_name(err), strerror(err)); + } + fprintf(outfp, "\n"); } fclose(outfp); -- cgit v1.2.3-70-g09d2