Skip to content

Commit

Permalink
Add mpers support
Browse files Browse the repository at this point in the history
Add a subsystem for semi-automatical definition of how parsers should
work with personality-dependent (mpers) types of tracee's data.  Create
auxiliary libraries containing mpers syscall parsers and printer
functions, one library for each possible nonnative target personality.

Currently some parsers do not handle differences in definitions of data
types between personalities, namely LP64 and ILP32.  When
this is the case, long integers, pointers, and all compound
types containing long and pointer members may be printed incorrectly,
because of differences in sizes, offsets and alignments.

Since in most cases these are the only differences in desired behaviour
of parsers and printers for different personalities, a correct way
would be to compile one source code into multiple parsers, differing
only in definitions of mpers types.

To get a definition of a given type for nonnative personality
a very basic .c file containing a declaration of a variable of this type
is being compiled for this personality (using -m32 or -mx32 compiler
flag).  Information about the type is then being extracted from
this binary's DWARF debug info with an awk script and put
into a corresponding header file.  Resulting headers are being used to
compile mpers variations of syscall parsers and printer functions.

In addition to syscall parsers, there can occur a need to create mpers
printing functions, which then can be called from many places
in the code (for example, printsiginfo_at).  Such functions (printers)
are marked in a special manner.

For each possible nonnative target personality a library is being
created, containing mpers variations of syscall parsers and printers.
Only syscall parsers from files marked in a special manner and specially
marked functions from such files are being recompiled and included
in these libraries.

generate_mpers_am.sh is called by bootstrap to find the files
from strace_SOURCES which include MPERS_DEFS.  During compilation,
these files are being inspected for inclusions of DEF_MPERS_TYPE,
and nonnative variations of each included type are being generated
by an awk script.

Mpers parser names are being modified during inclusions of syscallent
headers for nonnative personalities.  Pointers to printers are
being stored in structs struct_printers, and a master
pointer printers is being updated on every set_personality.

* README-mpers: New README explaining how to use mpers support.
* empty.h: New empty file.
* generate_mpers_am.sh: New file.
* mpers.awk: Likewise.
* mpers.sh: Likewise.
* mpers_test.sh: Likewise.
* mpers_type.h: Likewise.
* Makefile.am (strace_SOURCES): Add empty.h and mpers_type.h.
(strace_CPPFLAGS, strace_LDFLAGS, strace_LDADD): Move to the beginning
of the file.
(strace_LDADD, noinst_LIBRARIES): Add libmpers-%.a.
(EXTRA_DIST): Add mpers.awk, mpers.sh, mpers_test.sh.
(BUILT_SOURCES, CLEANFILES): Add new generated files:
native_printer_decls.h, native_printer_defs.h, printers.h,
[HAVE_M32_MPERS] $(mpers_m32_targets), and [HAVE_MX32_MPERS]
$(mpers_mx32_targets).
(mpers_NAME, mpers_PREFIX, mpers_DEFS, mpers_INCLUDES, mpers_CPPFLAGS,
mpers_sh_opts, libmpers_CPPFLAGS, libmpers_m[x]32_a_SOURCES,
libmpers_m[x]32_a_CPPFLAGS, mpers_m[x]32_targets): New variables.
(mpers-m[x]32.stamp, m[x]32_defs.h, m[x]32_funcs.h, printers.h,
%_printer_decls.h, %_printer_defs.h, clean-local,
native_printer_decls.h, native_printer_defs.h, $mpers_m[x]32_targets):
New targets.
* bootstrap: Add generate_mpers_am.sh.
* configure.ac: Add AC_PROG_RANLIB.
* m4/mpers.m4: Add HAVE_MPERS variable.  Add $st_cv_mpers checks.
* defs.h: Include mpers_type.h.
Include printers.h, native_printer_decls.h, define MPERS_PRINTER_NAME.
Redefine SYS_FUNC_NAME.  Define MPERS_PRINTER_DECL.
[HAVE_M32_MPERS]: define PERSONALITY1_INCLUDE_FUNCS,
PERSONALITY1_INCLUDE_PRINTERS_DECLS, PERSONALITY1_INCLUDE_PRINTERS_DEFS
for X86_64, X32.
[HAVE_MX32_MPERS]: define PERSONALITY2_INCLUDE_FUNCS,
PERSONALITY2_INCLUDE_PRINTERS_DECLS, PERSONALITY2_INCLUDE_PRINTERS_DEFS
for X86_64.
Add fallback definitions of
PERSONALITY1_INCLUDE_FUNCS, PERSONALITY2_INCLUDE_FUNCS,
PERSONALITY0_INCLUDE_PRINTERS_DECLS, PERSONALITY0_INCLUDE_PRINTERS_DEFS,
PERSONALITY1_INCLUDE_PRINTERS_DECLS, PERSONALITY1_INCLUDE_PRINTERS_DEFS,
PERSONALITY2_INCLUDE_PRINTERS_DECLS, PERSONALITY2_INCLUDE_PRINTERS_DEFS.
* syscall.c: Include PERSONALITY1_INCLUDE_FUNCS,
PERSONALITY2_INCLUDE_FUNCS,
PERSONALITY0_INCLUDE_PRINTERS_DECLS, PERSONALITY0_INCLUDE_PRINTERS_DEFS,
PERSONALITY1_INCLUDE_PRINTERS_DECLS, PERSONALITY1_INCLUDE_PRINTERS_DEFS,
PERSONALITY2_INCLUDE_PRINTERS_DECLS, PERSONALITY2_INCLUDE_PRINTERS_DEFS.
(printers): New struct.  Update it when needed.
* .gitignore: Add libmpers-m32.a, libmpers-mx32.a, m32_defs.h,
m32_funcs.h, m32_printer_decls.h, m32_printer_defs.h, mpers-m32,
mpers-m32.stamp, mpers-mx32, mpers-mx32.stamp, mpers.am, mx32_defs.h,
mx32_funcs.h, mx32_printer_decls.h, mx32_printer_defs.h,
native_printer_decls.h, native_printer_defs.h, and printers.h.
  • Loading branch information
parport0 authored and ldv-alt committed Aug 28, 2015
1 parent c633188 commit 0929422
Show file tree
Hide file tree
Showing 14 changed files with 483 additions and 13 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Expand Up @@ -45,3 +45,22 @@ Makefile.in

/tests-m32
/tests-mx32

/libmpers-m32.a
/libmpers-mx32.a
/m32_defs.h
/m32_funcs.h
/m32_printer_decls.h
/m32_printer_defs.h
/mpers-m32
/mpers-m32.stamp
/mpers-mx32
/mpers-mx32.stamp
/mpers.am
/mx32_defs.h
/mx32_funcs.h
/mx32_printer_decls.h
/mx32_printer_defs.h
/native_printer_decls.h
/native_printer_defs.h
/printers.h
123 changes: 113 additions & 10 deletions Makefile.am
Expand Up @@ -27,6 +27,11 @@ AM_CPPFLAGS = -I$(builddir)/$(OS)/$(ARCH) \

include xlat/Makemodule.am

strace_CPPFLAGS = $(AM_CPPFLAGS)
strace_LDFLAGS =
strace_LDADD =
noinst_LIBRARIES =

strace_SOURCES = \
access.c \
affinity.c \
Expand All @@ -44,6 +49,7 @@ strace_SOURCES = \
count.c \
desc.c \
dirent.c \
empty.h \
epoll.c \
evdev.c \
eventfd.c \
Expand Down Expand Up @@ -80,6 +86,7 @@ strace_SOURCES = \
memfd_create.c \
mknod.c \
mount.c \
mpers_type.h \
mq.c \
mtd.c \
net.c \
Expand Down Expand Up @@ -146,15 +153,14 @@ strace_SOURCES = \
vsprintf.c \
wait.c \
xattr.c \
xmalloc.c
xmalloc.c \
# end of strace_SOURCES

if USE_LIBUNWIND
strace_SOURCES += unwind.c
strace_CPPFLAGS = $(AM_CPPFLAGS) $(libunwind_CPPFLAGS)
strace_LDFLAGS = $(libunwind_LDFLAGS)
strace_LDADD = $(libunwind_LIBS)
else
strace_CPPFLAGS = $(AM_CPPFLAGS)
strace_CPPFLAGS += $(libunwind_CPPFLAGS)
strace_LDFLAGS += $(libunwind_LDFLAGS)
strace_LDADD += $(libunwind_LIBS)
endif

noinst_HEADERS = defs.h
Expand Down Expand Up @@ -575,6 +581,9 @@ EXTRA_DIST = \
maint/ioctls_hex.sh \
maint/ioctls_sym.sh \
maint/print_ioctlent.c \
mpers.awk \
mpers.sh \
mpers_test.sh \
signalent.sh \
strace-graph \
strace-log-merge \
Expand All @@ -589,8 +598,6 @@ EXTRA_DIST = \
srpm: dist-xz
rpmbuild --define '%_srcrpmdir .' -ts $(distdir).tar.xz

BUILT_SOURCES = .version sys_func.h sen.h

$(srcdir)/.version:
$(AM_V_GEN)echo $(VERSION) > $@-t && mv $@-t $@

Expand Down Expand Up @@ -635,8 +642,6 @@ ioctlsort_LDFLAGS = $(AM_LDFLAGS) $(LDFLAGS_FOR_BUILD)

ioctls_inc_h = $(wildcard $(srcdir)/$(OS)/$(ARCH)/ioctls_inc*.h)
ioctlent_h = $(patsubst $(srcdir)/$(OS)/$(ARCH)/ioctls_inc%,ioctlent%,$(ioctls_inc_h))
BUILT_SOURCES += $(ioctlent_h)
CLEANFILES = sys_func.h sen.h $(ioctlent_h)

ioctlent%.h: ioctlsort%
./$< > $@
Expand All @@ -650,6 +655,104 @@ ioctlsort%.o: ioctls_all%.h $(srcdir)/ioctlsort.c
ioctls_all%.h: $(srcdir)/$(OS)/$(ARCH)/ioctls_inc%.h $(srcdir)/$(OS)/$(ARCH)/ioctls_arch%.h
cat $^ > $@

BUILT_SOURCES = $(ioctlent_h) native_printer_decls.h native_printer_defs.h printers.h sen.h sys_func.h .version
CLEANFILES = $(ioctlent_h) native_printer_decls.h native_printer_defs.h printers.h sen.h sys_func.h

# defines mpers_source_files
include mpers.am
srcdir_mpers_source_files = $(patsubst %,$(srcdir)/%,$(mpers_source_files))

mpers_NAME =
mpers_PREFIX = $(mpers_NAME)_
mpers_DEFS = $(DEFS)
mpers_INCLUDES = $(DEFAULT_INCLUDES) $(INCLUDES)
mpers_CPPFLAGS = $(strace_CPPFLAGS) $(CPPFLAGS)
mpers_sh_opts = $(mpers_DEFS) $(mpers_INCLUDES) $(mpers_CPPFLAGS)
libmpers_CPPFLAGS = $(AM_CPPFLAGS) -DIN_MPERS

# mpers targets

mpers-m%.stamp: $(srcdir_mpers_source_files)
for f in $^; do \
CC="$(CC)" CFLAGS="$(mpers_sh_opts)" \
CPP="$(CPP)" CPPFLAGS="$(mpers_CPPFLAGS) $(mpers_INCLUDES) -DIN_MPERS -DMPERS_IS_$(mpers_NAME)" \
$(srcdir)/mpers.sh -$(mpers_NAME) $$f || exit; \
done
> $@

m%_defs.h: $(srcdir_mpers_source_files)
for f in $^; do \
sed -n 's/^#include DEF_MPERS_TYPE(\([^)]\+\))/#ifdef MPERS_$(mpers_PREFIX)\1\n# define \1 MPERS_$(mpers_PREFIX)\1\n#endif/p' $$f || exit; \
done > $@-t
mv $@-t $@

m%_funcs.h: $(srcdir_mpers_source_files)
for f in $^; do \
sed -n 's/^SYS_FUNC(\([^)]\+\))/#undef sys_\1\n#define sys_\1 $(mpers_PREFIX)sys_\1/p' $$f || exit; \
done > $@-t && \
echo '#include "sys_func.h"' >> $@-t
mv $@-t $@

# printers

printers.h: $(srcdir_mpers_source_files)
echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
echo 'typedef struct {' >> $@-t
for f in $^; do \
sed -n 's/^MPERS_PRINTER_DECL(\([^,]\+\),[[:space:]]*\([^)]\+\))\(.*\)/ \1 (*\2) \3;/p' $$f || exit; \
done >> $@-t
echo '} struct_printers;' >> $@-t
mv $@-t $@

%_printer_decls.h: $(srcdir_mpers_source_files)
echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
for f in $^; do \
sed -n 's/^MPERS_PRINTER_DECL(\([^,]\+\),[[:space:]]*\([^)]\+\))\(.*\)/extern \1 $(mpers_PREFIX)\2\3;/p' $$f || exit; \
done >> $@-t
mv $@-t $@

%_printer_defs.h: $(srcdir_mpers_source_files)
echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
for f in $^; do \
sed -n 's/^MPERS_PRINTER_DECL(\([^,]\+\),[[:space:]]*\([^)]\+\))\(.*\)/\.\2 = $(mpers_PREFIX)\2,/p' $$f || exit; \
done >> $@-t
mv $@-t $@

native_printer_decls.h native_printer_defs.h: mpers_PREFIX =

if HAVE_M32_MPERS

strace_LDADD += libmpers-m32.a
noinst_LIBRARIES += libmpers-m32.a
libmpers_m32_a_SOURCES = $(mpers_source_files)
libmpers_m32_a_CPPFLAGS = $(libmpers_CPPFLAGS) -DMPERS_IS_m32 -I$(builddir)/mpers-m32
mpers_m32_targets = mpers-m32.stamp m32_defs.h m32_funcs.h m32_printer_decls.h m32_printer_defs.h

BUILT_SOURCES += $(mpers_m32_targets)
CLEANFILES += $(mpers_m32_targets)

$(mpers_m32_targets): mpers_NAME = m32

endif # HAVE_M32_MPERS

if HAVE_MX32_MPERS

strace_LDADD += libmpers-mx32.a
noinst_LIBRARIES += libmpers-mx32.a
libmpers_mx32_a_SOURCES = $(mpers_source_files)
libmpers_mx32_a_CPPFLAGS = $(libmpers_CPPFLAGS) -DMPERS_IS_mx32 -I$(builddir)/mpers-mx32
mpers_mx32_targets = mpers-mx32.stamp mx32_defs.h mx32_funcs.h mx32_printer_decls.h mx32_printer_defs.h

BUILT_SOURCES += $(mpers_mx32_targets)
CLEANFILES += $(mpers_mx32_targets)

$(mpers_mx32_targets): mpers_NAME = mx32

endif # HAVE_MX32_MPERS

clean-local:
-rm -rf mpers-m32 mpers-mx32

if MAINTAINER_MODE

gen_changelog_start_date = 2009-07-08 20:00
Expand Down
12 changes: 12 additions & 0 deletions README-mpers
@@ -0,0 +1,12 @@
To use mpers functionality, one should:
* typedef all of the target types which are compound and not typedefed
already;
* for each target type, include DEF_MPERS_TYPE(target_type_t), these can
be included conditionally;
* include MPERS_DEFS once;
* before inclusion of MPERS_DEFS include all important headers
(containing definitions of these types or other behaviour-affecting
defines);
* printers should be defined
as MPERS_PRINTER_DECL(return type, function name)(args) and called
as MPERS_PRINTER_NAME(function name)(args).
1 change: 1 addition & 0 deletions bootstrap
Expand Up @@ -15,5 +15,6 @@ for m in -m32 -mx32; do
done

./xlat/gen.sh
./generate_mpers_am.sh

exec autoreconf -f -i "$@"
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -13,6 +13,7 @@ AC_CANONICAL_HOST
AC_PROG_CC
AX_PROG_CC_FOR_BUILD
AC_USE_SYSTEM_EXTENSIONS
AC_PROG_RANLIB

AC_MSG_CHECKING([for supported architecture])
case "$host_cpu" in
Expand Down
66 changes: 63 additions & 3 deletions defs.h
Expand Up @@ -54,6 +54,8 @@
#include <sys/time.h>
#include <sys/syscall.h>

#include "mpers_type.h"

#ifndef HAVE_STRERROR
const char *strerror(int);
#endif
Expand Down Expand Up @@ -200,12 +202,27 @@ extern char *stpcpy(char *dst, const char *src);
# define PERSONALITY0_WORDSIZE 8
# define PERSONALITY1_WORDSIZE 4
# define PERSONALITY2_WORDSIZE 4
# ifdef HAVE_M32_MPERS
# define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
# endif
# ifdef HAVE_MX32_MPERS
# define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
# define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
# define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
# endif
#endif

#ifdef X32
# define SUPPORTED_PERSONALITIES 2
# define PERSONALITY0_WORDSIZE 4
# define PERSONALITY1_WORDSIZE 4
# ifdef HAVE_M32_MPERS
# define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
# endif
#endif

#ifdef ARM
Expand Down Expand Up @@ -245,6 +262,34 @@ extern char *stpcpy(char *dst, const char *src);
# define PERSONALITY0_WORDSIZE SIZEOF_LONG
#endif

#ifndef PERSONALITY0_INCLUDE_PRINTERS_DECLS
# define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
#endif
#ifndef PERSONALITY0_INCLUDE_PRINTERS_DEFS
# define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
#endif

#ifndef PERSONALITY1_INCLUDE_PRINTERS_DECLS
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
#endif
#ifndef PERSONALITY1_INCLUDE_PRINTERS_DEFS
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
#endif

#ifndef PERSONALITY2_INCLUDE_PRINTERS_DECLS
# define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
#endif
#ifndef PERSONALITY2_INCLUDE_PRINTERS_DEFS
# define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
#endif

#ifndef PERSONALITY1_INCLUDE_FUNCS
# define PERSONALITY1_INCLUDE_FUNCS "empty.h"
#endif
#ifndef PERSONALITY2_INCLUDE_FUNCS
# define PERSONALITY2_INCLUDE_FUNCS "empty.h"
#endif

typedef struct sysent {
unsigned nargs;
int sys_flags;
Expand Down Expand Up @@ -694,6 +739,7 @@ extern const char *const signalent0[];
extern const struct_ioctlent ioctlent0[];
extern qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
#define qual_flags (qual_vec[current_personality])

#if SUPPORTED_PERSONALITIES > 1
extern const struct_sysent *sysent;
extern const char *const *errnoent;
Expand All @@ -705,12 +751,22 @@ extern const struct_ioctlent *ioctlent;
# define signalent signalent0
# define ioctlent ioctlent0
#endif

extern unsigned nsyscalls;
extern unsigned nerrnos;
extern unsigned nsignals;
extern unsigned nioctlents;
extern unsigned num_quals;

#if SUPPORTED_PERSONALITIES > 1
# include "printers.h"
extern const struct_printers *printers;
# define MPERS_PRINTER_NAME(printer_name) printers->printer_name
#else
# include "native_printer_decls.h"
# define MPERS_PRINTER_NAME(printer_name) printer_name
#endif

/*
* If you need non-NULL sysent[scno].sys_func and sysent[scno].sys_name
*/
Expand All @@ -721,8 +777,12 @@ extern unsigned num_quals;
#define SCNO_IN_RANGE(scno) \
((unsigned long)(scno) < nsyscalls)

#ifndef SYS_FUNC_NAME
# define SYS_FUNC_NAME(syscall_name) sys_ ## syscall_name
#endif
#define MPERS_FUNC_NAME__(prefix, name) prefix ## name
#define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
#define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)

#define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(sys_ ## syscall_name)

#define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(syscall_name)(struct tcb *tcp)

#define MPERS_PRINTER_DECL(type, name) type MPERS_FUNC_NAME(name)
Empty file added empty.h
Empty file.
11 changes: 11 additions & 0 deletions generate_mpers_am.sh
@@ -0,0 +1,11 @@
#!/bin/sh -e

exec > mpers.am

echo "# Generated by $0; do not edit."
echo -n 'mpers_source_files = '

sed -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*\([[:alnum:]][^.]*\.c\)[[:space:]]*\\$/\1/p' Makefile.am |
xargs -r grep -lx '#[[:space:]]*include[[:space:]]\+MPERS_DEFS' |
tr '\n' ' '
echo

0 comments on commit 0929422

Please sign in to comment.