Commit 74d753ac authored by Mika Westerberg's avatar Mika Westerberg Committed by Riku Voipio

linux-user: strace now handles guest strings correctly [v2]

- to not to break strace with GUEST_BASE is set:
- Strace now can load and print guest strings correctly.
- Added printing support for commonly used flags in some syscalls
  (e.g open, creat, mmap etc.)

v2:
- fix strace.c build on etch
- add futex print to strace
Signed-off-by: default avatarMika Westerberg <mika.westerberg@iki.fi>
Signed-off-by: default avatarRiku Voipio <riku.voipio@iki.fi>
parent 9edc5d79
......@@ -6,6 +6,9 @@
#include <sys/shm.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/mman.h>
#include <linux/futex.h>
#include <unistd.h>
#include "qemu.h"
......@@ -21,6 +24,47 @@ struct syscallname {
void (*result)(const struct syscallname *, abi_long);
};
#ifdef __GNUC__
/*
* It is possible that target doesn't have syscall that uses
* following flags but we don't want the compiler to warn
* us about them being unused. Same applies to utility print
* functions. It is ok to keep them while not used.
*/
#define UNUSED __attribute__ ((unused))
#else
#define UNUSED
#endif
/*
* Structure used to translate flag values into strings. This is
* similar that is in the actual strace tool.
*/
struct flags {
abi_long f_value; /* flag */
const char *f_string; /* stringified flag */
};
/* common flags for all architectures */
#define FLAG_GENERIC(name) { name, #name }
/* target specific flags (syscall_defs.h has TARGET_<flag>) */
#define FLAG_TARGET(name) { TARGET_ ## name, #name }
/* end of flags array */
#define FLAG_END { 0, NULL }
UNUSED static const char *get_comma(int);
UNUSED static void print_pointer(abi_long, int);
UNUSED static void print_flags(const struct flags *, abi_long, int);
UNUSED static void print_at_dirfd(abi_long, int);
UNUSED static void print_file_mode(abi_long, int);
UNUSED static void print_open_flags(abi_long, int);
UNUSED static void print_syscall_prologue(const struct syscallname *);
UNUSED static void print_syscall_epilogue(const struct syscallname *);
UNUSED static void print_string(abi_long, int);
UNUSED static void print_raw_param(const char *, abi_long, int);
UNUSED static void print_timeval(abi_ulong, int);
UNUSED static void print_number(abi_long, int);
/*
* Utility functions
*/
......@@ -100,22 +144,6 @@ print_fdset(int n, abi_ulong target_fds_addr)
}
gemu_log("]");
}
static void
print_timeval(abi_ulong tv_addr)
{
if( tv_addr ) {
struct target_timeval *tv;
tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
if (!tv)
return;
gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}",
tv->tv_sec, tv->tv_usec);
unlock_user(tv, tv_addr, 0);
} else
gemu_log("NULL");
}
#endif
/*
......@@ -142,7 +170,7 @@ print_newselect(const struct syscallname *name,
gemu_log(",");
print_fdset(arg1, arg4);
gemu_log(",");
print_timeval(arg5);
print_timeval(arg5, 1);
gemu_log(")");
/* save for use in the return output function below */
......@@ -250,11 +278,1019 @@ print_syscall_ret_newselect(const struct syscallname *name, abi_long ret)
gemu_log(",");
print_fdset(newselect_arg1,newselect_arg4);
gemu_log(",");
print_timeval(newselect_arg5);
print_timeval(newselect_arg5, 1);
gemu_log(")\n");
}
#endif
UNUSED static struct flags access_flags[] = {
FLAG_GENERIC(F_OK),
FLAG_GENERIC(R_OK),
FLAG_GENERIC(W_OK),
FLAG_GENERIC(X_OK),
FLAG_END,
};
UNUSED static struct flags at_file_flags[] = {
#ifdef AT_EACCESS
FLAG_GENERIC(AT_EACCESS),
#endif
#ifdef AT_SYMLINK_NOFOLLOW
FLAG_GENERIC(AT_SYMLINK_NOFOLLOW),
#endif
FLAG_END,
};
UNUSED static struct flags unlinkat_flags[] = {
#ifdef AT_REMOVEDIR
FLAG_GENERIC(AT_REMOVEDIR),
#endif
FLAG_END,
};
UNUSED static struct flags mode_flags[] = {
FLAG_GENERIC(S_IFSOCK),
FLAG_GENERIC(S_IFLNK),
FLAG_GENERIC(S_IFREG),
FLAG_GENERIC(S_IFBLK),
FLAG_GENERIC(S_IFDIR),
FLAG_GENERIC(S_IFCHR),
FLAG_GENERIC(S_IFIFO),
FLAG_END,
};
UNUSED static struct flags open_access_flags[] = {
FLAG_TARGET(O_RDONLY),
FLAG_TARGET(O_WRONLY),
FLAG_TARGET(O_RDWR),
FLAG_END,
};
UNUSED static struct flags open_flags[] = {
FLAG_TARGET(O_APPEND),
FLAG_TARGET(O_CREAT),
FLAG_TARGET(O_DIRECTORY),
FLAG_TARGET(O_EXCL),
FLAG_TARGET(O_LARGEFILE),
FLAG_TARGET(O_NOCTTY),
FLAG_TARGET(O_NOFOLLOW),
FLAG_TARGET(O_NONBLOCK), /* also O_NDELAY */
FLAG_TARGET(O_SYNC),
FLAG_TARGET(O_TRUNC),
#ifdef O_DIRECT
FLAG_TARGET(O_DIRECT),
#endif
FLAG_END,
};
UNUSED static struct flags mount_flags[] = {
#ifdef MS_BIND
FLAG_GENERIC(MS_BIND),
#endif
#ifdef MS_DIRSYNC
FLAG_GENERIC(MS_DIRSYNC),
#endif
FLAG_GENERIC(MS_MANDLOCK),
#ifdef MS_MOVE
FLAG_GENERIC(MS_MOVE),
#endif
FLAG_GENERIC(MS_NOATIME),
FLAG_GENERIC(MS_NODEV),
FLAG_GENERIC(MS_NODIRATIME),
FLAG_GENERIC(MS_NOEXEC),
FLAG_GENERIC(MS_NOSUID),
FLAG_GENERIC(MS_RDONLY),
#ifdef MS_RELATIME
FLAG_GENERIC(MS_RELATIME),
#endif
FLAG_GENERIC(MS_REMOUNT),
FLAG_GENERIC(MS_SYNCHRONOUS),
FLAG_END,
};
UNUSED static struct flags umount2_flags[] = {
#ifdef MNT_FORCE
FLAG_GENERIC(MNT_FORCE),
#endif
#ifdef MNT_DETACH
FLAG_GENERIC(MNT_DETACH),
#endif
#ifdef MNT_EXPIRE
FLAG_GENERIC(MNT_EXPIRE),
#endif
FLAG_END,
};
UNUSED static struct flags mmap_prot_flags[] = {
FLAG_GENERIC(PROT_NONE),
FLAG_GENERIC(PROT_EXEC),
FLAG_GENERIC(PROT_READ),
FLAG_GENERIC(PROT_WRITE),
FLAG_END,
};
UNUSED static struct flags mmap_flags[] = {
FLAG_TARGET(MAP_SHARED),
FLAG_TARGET(MAP_PRIVATE),
FLAG_TARGET(MAP_ANONYMOUS),
FLAG_TARGET(MAP_DENYWRITE),
FLAG_TARGET(MAP_FIXED),
FLAG_TARGET(MAP_GROWSDOWN),
#ifdef MAP_LOCKED
FLAG_TARGET(MAP_LOCKED),
#endif
#ifdef MAP_NONBLOCK
FLAG_TARGET(MAP_NONBLOCK),
#endif
FLAG_TARGET(MAP_NORESERVE),
#ifdef MAP_POPULATE
FLAG_TARGET(MAP_POPULATE),
#endif
FLAG_END,
};
UNUSED static struct flags fcntl_flags[] = {
FLAG_TARGET(F_DUPFD),
FLAG_TARGET(F_GETFD),
FLAG_TARGET(F_SETFD),
FLAG_TARGET(F_GETFL),
FLAG_TARGET(F_SETFL),
FLAG_TARGET(F_GETLK),
FLAG_TARGET(F_SETLK),
FLAG_TARGET(F_SETLKW),
FLAG_END,
};
/*
* print_xxx utility functions. These are used to print syscall
* parameters in certain format. All of these have parameter
* named 'last'. This parameter is used to add comma to output
* when last == 0.
*/
static const char *
get_comma(int last)
{
return ((last) ? "" : ",");
}
static void
print_flags(const struct flags *f, abi_long tflags, int last)
{
const char *sep = "";
int flags;
int n;
flags = (int)tswap32(tflags);
if ((flags == 0) && (f->f_value == 0)) {
gemu_log("%s%s", f->f_string, get_comma(last));
return;
}
for (n = 0; f->f_string != NULL; f++) {
if ((f->f_value != 0) && ((flags & f->f_value) == f->f_value)) {
gemu_log("%s%s", sep, f->f_string);
flags &= ~f->f_value;
sep = "|";
n++;
}
}
if (n > 0) {
/* print rest of the flags as numeric */
if (flags != 0) {
gemu_log("%s%#x%s", sep, flags, get_comma(last));
} else {
gemu_log("%s", get_comma(last));
}
} else {
/* no string version of flags found, print them in hex then */
gemu_log("%#x%s", flags, get_comma(last));
}
}
static void
print_at_dirfd(abi_long tdirfd, int last)
{
int dirfd = tswap32(tdirfd);
#ifdef AT_FDCWD
if (dirfd == AT_FDCWD) {
gemu_log("AT_FDCWD%s", get_comma(last));
return;
}
#endif
gemu_log("%d%s", dirfd, get_comma(last));
}
static void
print_file_mode(abi_long tmode, int last)
{
const char *sep = "";
const struct flags *m;
mode_t mode = (mode_t)tswap32(tmode);
for (m = &mode_flags[0]; m->f_string != NULL; m++) {
if ((m->f_value & mode) == m->f_value) {
gemu_log("%s%s", m->f_string, sep);
sep = "|";
mode &= ~m->f_value;
break;
}
}
mode &= ~S_IFMT;
/* print rest of the mode as octal */
if (mode != 0)
gemu_log("%s%#o", sep, mode);
gemu_log("%s", get_comma(last));
}
static void
print_open_flags(abi_long tflags, int last)
{
int flags = tswap32(tflags);
print_flags(open_access_flags, flags & TARGET_O_ACCMODE, 1);
flags &= ~TARGET_O_ACCMODE;
if (flags == 0) {
gemu_log("%s", get_comma(last));
return;
}
gemu_log("|");
print_flags(open_flags, flags, last);
}
static void
print_syscall_prologue(const struct syscallname *sc)
{
gemu_log("%s(", sc->name);
}
/*ARGSUSED*/
static void
print_syscall_epilogue(const struct syscallname *sc)
{
(void)sc;
gemu_log(")");
}
static void
print_string(abi_long addr, int last)
{
char *s;
if ((s = lock_user_string(addr)) != NULL) {
gemu_log("\"%s\"%s", s, get_comma(last));
unlock_user(s, addr, 0);
} else {
/* can't get string out of it, so print it as pointer */
print_pointer(addr, last);
}
}
/*
* Prints out raw parameter using given format. Caller needs
* to do byte swapping if needed.
*/
static void
print_raw_param(const char *fmt, abi_long param, int last)
{
char format[64];
(void) snprintf(format, sizeof (format), "%s%s", fmt, get_comma(last));
gemu_log(format, param);
}
static void
print_pointer(abi_long p, int last)
{
if (p == 0)
gemu_log("NULL%s", get_comma(last));
else
gemu_log("0x" TARGET_ABI_FMT_lx "%s", p, get_comma(last));
}
/*
* Reads 32-bit (int) number from guest address space from
* address 'addr' and prints it.
*/
static void
print_number(abi_long addr, int last)
{
if (addr == 0) {
gemu_log("NULL%s", get_comma(last));
} else {
int num;
get_user_s32(num, addr);
gemu_log("[%d]%s", num, get_comma(last));
}
}
static void
print_timeval(abi_ulong tv_addr, int last)
{
if( tv_addr ) {
struct target_timeval *tv;
tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
if (!tv)
return;
gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}%s",
tv->tv_sec, tv->tv_usec, get_comma(last));
unlock_user(tv, tv_addr, 0);
} else
gemu_log("NULL%s", get_comma(last));
}
#undef UNUSED
#ifdef TARGET_NR_accept
static void
print_accept(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_raw_param("%d", tswap32(arg0), 0);
print_pointer(arg1, 0);
print_number(arg2, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_access
static void
print_access(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 0);
print_flags(access_flags, arg1, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_brk
static void
print_brk(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_pointer(arg0, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_chdir
static void
print_chdir(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_chmod
static void
print_chmod(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 0);
print_file_mode(arg1, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_creat
static void
print_creat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 0);
print_file_mode(arg1, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_execv
static void
print_execv(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 0);
print_raw_param("0x" TARGET_ABI_FMT_lx, tswapl(arg1), 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_faccessat
static void
print_faccessat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_at_dirfd(arg0, 0);
print_string(arg1, 0);
print_flags(access_flags, arg2, 0);
print_flags(at_file_flags, arg3, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_fchmodat
static void
print_fchmodat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_at_dirfd(arg0, 0);
print_string(arg1, 0);
print_file_mode(arg2, 0);
print_flags(at_file_flags, arg3, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_fchownat
static void
print_fchownat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_at_dirfd(arg0, 0);
print_string(arg1, 0);
#ifdef USE_UID16
print_raw_param("%d", tswap16(arg2), 0);
print_raw_param("%d", tswap16(arg3), 0);
#else
print_raw_param("%d", tswap32(arg2), 0);
print_raw_param("%d", tswap32(arg3), 0);
#endif
print_flags(at_file_flags, arg4, 1);
print_syscall_epilogue(name);
}
#endif
#if defined(TARGET_NR_fcntl) || defined(TARGET_NR_fcntl64)
static void
print_fcntl(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_raw_param("%d", tswap32(arg0), 0);
print_flags(fcntl_flags, arg1, 0);
/*
* TODO: check flags and print following argument only
* when needed.
*/
print_pointer(arg2, 1);
print_syscall_epilogue(name);
}
#define print_fcntl64 print_fcntl
#endif
#ifdef TARGET_NR_futimesat
static void
print_futimesat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_at_dirfd(arg0, 0);
print_string(arg1, 0);
print_timeval(arg2, 0);
print_timeval(arg2 + sizeof (struct target_timeval), 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_link
static void
print_link(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 0);
print_string(arg1, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_linkat
static void
print_linkat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_at_dirfd(arg0, 0);
print_string(arg1, 0);
print_at_dirfd(arg2, 0);
print_string(arg3, 0);
print_flags(at_file_flags, arg4, 1);
print_syscall_epilogue(name);
}
#endif
#if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) || \
defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64)
static void
print_stat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 0);
print_pointer(arg1, 1);
print_syscall_epilogue(name);
}
#define print_lstat print_stat
#define print_stat64 print_stat
#define print_lstat64 print_stat
#endif
#if defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64)
static void
print_fstat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_raw_param("%d", tswap32(arg0), 0);
print_pointer(arg1, 1);
print_syscall_epilogue(name);
}
#define print_fstat64 print_fstat
#endif
#ifdef TARGET_NR_mkdir
static void
print_mkdir(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_string(arg0, 0);
print_file_mode(arg1, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_mkdirat
static void
print_mkdirat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
print_syscall_prologue(name);
print_at_dirfd(arg0, 0);
print_string(arg1, 0);
print_file_mode(arg2, 1);
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_mknod
static void
print_mknod(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
int hasdev = (tswapl(arg1) & (S_IFCHR|S_IFBLK));
print_syscall_prologue(name);
print_string(arg0, 0);
print_file_mode(arg1, (hasdev == 0));
if (hasdev) {
print_raw_param("makedev(%d", major(tswapl(arg2)), 0);
print_raw_param("%d)", minor(tswapl(arg2)), 1);
}
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_mknodat
static void
print_mknodat(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
int hasdev = (tswapl(arg2) & (S_IFCHR|S_IFBLK));
print_syscall_prologue(name);
print_at_dirfd(arg0, 0);
print_string(arg1, 0);
print_file_mode(arg2, (hasdev == 0));
if (hasdev) {
print_raw_param("makedev(%d", major(tswapl(arg3)), 0);
print_raw_param("%d)", minor(tswapl(arg3)), 1);
}
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_mq_open
static void
print_mq_open(const struct syscallname *name,
abi_long arg0, abi_long arg1, abi_long arg2,
abi_long arg3, abi_long arg4, abi_long arg5)
{
int is_creat = (tswapl(arg1) & TARGET_O_CREAT);
print_syscall_prologue(name);
print_string(arg0, 0);
print_open_flags(arg1, (is_creat == 0));
if (is_creat) {
print_file_mode(arg2, 0);
print_pointer(arg3, 1);
}
print_syscall_epilogue(name);
}
#endif
#ifdef TARGET_NR_open
static void
print_open(const struct syscallname *name,