Skip to content

Commit 0929422

Browse files
parport0ldv-alt
authored andcommitted
Add mpers support
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.
1 parent c633188 commit 0929422

14 files changed

+483
-13
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ Makefile.in
4545

4646
/tests-m32
4747
/tests-mx32
48+
49+
/libmpers-m32.a
50+
/libmpers-mx32.a
51+
/m32_defs.h
52+
/m32_funcs.h
53+
/m32_printer_decls.h
54+
/m32_printer_defs.h
55+
/mpers-m32
56+
/mpers-m32.stamp
57+
/mpers-mx32
58+
/mpers-mx32.stamp
59+
/mpers.am
60+
/mx32_defs.h
61+
/mx32_funcs.h
62+
/mx32_printer_decls.h
63+
/mx32_printer_defs.h
64+
/native_printer_decls.h
65+
/native_printer_defs.h
66+
/printers.h

Makefile.am

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ AM_CPPFLAGS = -I$(builddir)/$(OS)/$(ARCH) \
2727

2828
include xlat/Makemodule.am
2929

30+
strace_CPPFLAGS = $(AM_CPPFLAGS)
31+
strace_LDFLAGS =
32+
strace_LDADD =
33+
noinst_LIBRARIES =
34+
3035
strace_SOURCES = \
3136
access.c \
3237
affinity.c \
@@ -44,6 +49,7 @@ strace_SOURCES = \
4449
count.c \
4550
desc.c \
4651
dirent.c \
52+
empty.h \
4753
epoll.c \
4854
evdev.c \
4955
eventfd.c \
@@ -80,6 +86,7 @@ strace_SOURCES = \
8086
memfd_create.c \
8187
mknod.c \
8288
mount.c \
89+
mpers_type.h \
8390
mq.c \
8491
mtd.c \
8592
net.c \
@@ -146,15 +153,14 @@ strace_SOURCES = \
146153
vsprintf.c \
147154
wait.c \
148155
xattr.c \
149-
xmalloc.c
156+
xmalloc.c \
157+
# end of strace_SOURCES
150158

151159
if USE_LIBUNWIND
152160
strace_SOURCES += unwind.c
153-
strace_CPPFLAGS = $(AM_CPPFLAGS) $(libunwind_CPPFLAGS)
154-
strace_LDFLAGS = $(libunwind_LDFLAGS)
155-
strace_LDADD = $(libunwind_LIBS)
156-
else
157-
strace_CPPFLAGS = $(AM_CPPFLAGS)
161+
strace_CPPFLAGS += $(libunwind_CPPFLAGS)
162+
strace_LDFLAGS += $(libunwind_LDFLAGS)
163+
strace_LDADD += $(libunwind_LIBS)
158164
endif
159165

160166
noinst_HEADERS = defs.h
@@ -575,6 +581,9 @@ EXTRA_DIST = \
575581
maint/ioctls_hex.sh \
576582
maint/ioctls_sym.sh \
577583
maint/print_ioctlent.c \
584+
mpers.awk \
585+
mpers.sh \
586+
mpers_test.sh \
578587
signalent.sh \
579588
strace-graph \
580589
strace-log-merge \
@@ -589,8 +598,6 @@ EXTRA_DIST = \
589598
srpm: dist-xz
590599
rpmbuild --define '%_srcrpmdir .' -ts $(distdir).tar.xz
591600

592-
BUILT_SOURCES = .version sys_func.h sen.h
593-
594601
$(srcdir)/.version:
595602
$(AM_V_GEN)echo $(VERSION) > $@-t && mv $@-t $@
596603

@@ -635,8 +642,6 @@ ioctlsort_LDFLAGS = $(AM_LDFLAGS) $(LDFLAGS_FOR_BUILD)
635642

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

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

658+
BUILT_SOURCES = $(ioctlent_h) native_printer_decls.h native_printer_defs.h printers.h sen.h sys_func.h .version
659+
CLEANFILES = $(ioctlent_h) native_printer_decls.h native_printer_defs.h printers.h sen.h sys_func.h
660+
661+
# defines mpers_source_files
662+
include mpers.am
663+
srcdir_mpers_source_files = $(patsubst %,$(srcdir)/%,$(mpers_source_files))
664+
665+
mpers_NAME =
666+
mpers_PREFIX = $(mpers_NAME)_
667+
mpers_DEFS = $(DEFS)
668+
mpers_INCLUDES = $(DEFAULT_INCLUDES) $(INCLUDES)
669+
mpers_CPPFLAGS = $(strace_CPPFLAGS) $(CPPFLAGS)
670+
mpers_sh_opts = $(mpers_DEFS) $(mpers_INCLUDES) $(mpers_CPPFLAGS)
671+
libmpers_CPPFLAGS = $(AM_CPPFLAGS) -DIN_MPERS
672+
673+
# mpers targets
674+
675+
mpers-m%.stamp: $(srcdir_mpers_source_files)
676+
for f in $^; do \
677+
CC="$(CC)" CFLAGS="$(mpers_sh_opts)" \
678+
CPP="$(CPP)" CPPFLAGS="$(mpers_CPPFLAGS) $(mpers_INCLUDES) -DIN_MPERS -DMPERS_IS_$(mpers_NAME)" \
679+
$(srcdir)/mpers.sh -$(mpers_NAME) $$f || exit; \
680+
done
681+
> $@
682+
683+
m%_defs.h: $(srcdir_mpers_source_files)
684+
for f in $^; do \
685+
sed -n 's/^#include DEF_MPERS_TYPE(\([^)]\+\))/#ifdef MPERS_$(mpers_PREFIX)\1\n# define \1 MPERS_$(mpers_PREFIX)\1\n#endif/p' $$f || exit; \
686+
done > $@-t
687+
mv $@-t $@
688+
689+
m%_funcs.h: $(srcdir_mpers_source_files)
690+
for f in $^; do \
691+
sed -n 's/^SYS_FUNC(\([^)]\+\))/#undef sys_\1\n#define sys_\1 $(mpers_PREFIX)sys_\1/p' $$f || exit; \
692+
done > $@-t && \
693+
echo '#include "sys_func.h"' >> $@-t
694+
mv $@-t $@
695+
696+
# printers
697+
698+
printers.h: $(srcdir_mpers_source_files)
699+
echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
700+
echo 'typedef struct {' >> $@-t
701+
for f in $^; do \
702+
sed -n 's/^MPERS_PRINTER_DECL(\([^,]\+\),[[:space:]]*\([^)]\+\))\(.*\)/ \1 (*\2) \3;/p' $$f || exit; \
703+
done >> $@-t
704+
echo '} struct_printers;' >> $@-t
705+
mv $@-t $@
706+
707+
%_printer_decls.h: $(srcdir_mpers_source_files)
708+
echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
709+
for f in $^; do \
710+
sed -n 's/^MPERS_PRINTER_DECL(\([^,]\+\),[[:space:]]*\([^)]\+\))\(.*\)/extern \1 $(mpers_PREFIX)\2\3;/p' $$f || exit; \
711+
done >> $@-t
712+
mv $@-t $@
713+
714+
%_printer_defs.h: $(srcdir_mpers_source_files)
715+
echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
716+
for f in $^; do \
717+
sed -n 's/^MPERS_PRINTER_DECL(\([^,]\+\),[[:space:]]*\([^)]\+\))\(.*\)/\.\2 = $(mpers_PREFIX)\2,/p' $$f || exit; \
718+
done >> $@-t
719+
mv $@-t $@
720+
721+
native_printer_decls.h native_printer_defs.h: mpers_PREFIX =
722+
723+
if HAVE_M32_MPERS
724+
725+
strace_LDADD += libmpers-m32.a
726+
noinst_LIBRARIES += libmpers-m32.a
727+
libmpers_m32_a_SOURCES = $(mpers_source_files)
728+
libmpers_m32_a_CPPFLAGS = $(libmpers_CPPFLAGS) -DMPERS_IS_m32 -I$(builddir)/mpers-m32
729+
mpers_m32_targets = mpers-m32.stamp m32_defs.h m32_funcs.h m32_printer_decls.h m32_printer_defs.h
730+
731+
BUILT_SOURCES += $(mpers_m32_targets)
732+
CLEANFILES += $(mpers_m32_targets)
733+
734+
$(mpers_m32_targets): mpers_NAME = m32
735+
736+
endif # HAVE_M32_MPERS
737+
738+
if HAVE_MX32_MPERS
739+
740+
strace_LDADD += libmpers-mx32.a
741+
noinst_LIBRARIES += libmpers-mx32.a
742+
libmpers_mx32_a_SOURCES = $(mpers_source_files)
743+
libmpers_mx32_a_CPPFLAGS = $(libmpers_CPPFLAGS) -DMPERS_IS_mx32 -I$(builddir)/mpers-mx32
744+
mpers_mx32_targets = mpers-mx32.stamp mx32_defs.h mx32_funcs.h mx32_printer_decls.h mx32_printer_defs.h
745+
746+
BUILT_SOURCES += $(mpers_mx32_targets)
747+
CLEANFILES += $(mpers_mx32_targets)
748+
749+
$(mpers_mx32_targets): mpers_NAME = mx32
750+
751+
endif # HAVE_MX32_MPERS
752+
753+
clean-local:
754+
-rm -rf mpers-m32 mpers-mx32
755+
653756
if MAINTAINER_MODE
654757

655758
gen_changelog_start_date = 2009-07-08 20:00

README-mpers

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
To use mpers functionality, one should:
2+
* typedef all of the target types which are compound and not typedefed
3+
already;
4+
* for each target type, include DEF_MPERS_TYPE(target_type_t), these can
5+
be included conditionally;
6+
* include MPERS_DEFS once;
7+
* before inclusion of MPERS_DEFS include all important headers
8+
(containing definitions of these types or other behaviour-affecting
9+
defines);
10+
* printers should be defined
11+
as MPERS_PRINTER_DECL(return type, function name)(args) and called
12+
as MPERS_PRINTER_NAME(function name)(args).

bootstrap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ for m in -m32 -mx32; do
1515
done
1616

1717
./xlat/gen.sh
18+
./generate_mpers_am.sh
1819

1920
exec autoreconf -f -i "$@"

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ AC_CANONICAL_HOST
1313
AC_PROG_CC
1414
AX_PROG_CC_FOR_BUILD
1515
AC_USE_SYSTEM_EXTENSIONS
16+
AC_PROG_RANLIB
1617

1718
AC_MSG_CHECKING([for supported architecture])
1819
case "$host_cpu" in

defs.h

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#include <sys/time.h>
5555
#include <sys/syscall.h>
5656

57+
#include "mpers_type.h"
58+
5759
#ifndef HAVE_STRERROR
5860
const char *strerror(int);
5961
#endif
@@ -200,12 +202,27 @@ extern char *stpcpy(char *dst, const char *src);
200202
# define PERSONALITY0_WORDSIZE 8
201203
# define PERSONALITY1_WORDSIZE 4
202204
# define PERSONALITY2_WORDSIZE 4
205+
# ifdef HAVE_M32_MPERS
206+
# define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
207+
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
208+
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
209+
# endif
210+
# ifdef HAVE_MX32_MPERS
211+
# define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
212+
# define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
213+
# define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
214+
# endif
203215
#endif
204216

205217
#ifdef X32
206218
# define SUPPORTED_PERSONALITIES 2
207219
# define PERSONALITY0_WORDSIZE 4
208220
# define PERSONALITY1_WORDSIZE 4
221+
# ifdef HAVE_M32_MPERS
222+
# define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
223+
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
224+
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
225+
# endif
209226
#endif
210227

211228
#ifdef ARM
@@ -245,6 +262,34 @@ extern char *stpcpy(char *dst, const char *src);
245262
# define PERSONALITY0_WORDSIZE SIZEOF_LONG
246263
#endif
247264

265+
#ifndef PERSONALITY0_INCLUDE_PRINTERS_DECLS
266+
# define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
267+
#endif
268+
#ifndef PERSONALITY0_INCLUDE_PRINTERS_DEFS
269+
# define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
270+
#endif
271+
272+
#ifndef PERSONALITY1_INCLUDE_PRINTERS_DECLS
273+
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
274+
#endif
275+
#ifndef PERSONALITY1_INCLUDE_PRINTERS_DEFS
276+
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
277+
#endif
278+
279+
#ifndef PERSONALITY2_INCLUDE_PRINTERS_DECLS
280+
# define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
281+
#endif
282+
#ifndef PERSONALITY2_INCLUDE_PRINTERS_DEFS
283+
# define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
284+
#endif
285+
286+
#ifndef PERSONALITY1_INCLUDE_FUNCS
287+
# define PERSONALITY1_INCLUDE_FUNCS "empty.h"
288+
#endif
289+
#ifndef PERSONALITY2_INCLUDE_FUNCS
290+
# define PERSONALITY2_INCLUDE_FUNCS "empty.h"
291+
#endif
292+
248293
typedef struct sysent {
249294
unsigned nargs;
250295
int sys_flags;
@@ -694,6 +739,7 @@ extern const char *const signalent0[];
694739
extern const struct_ioctlent ioctlent0[];
695740
extern qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
696741
#define qual_flags (qual_vec[current_personality])
742+
697743
#if SUPPORTED_PERSONALITIES > 1
698744
extern const struct_sysent *sysent;
699745
extern const char *const *errnoent;
@@ -705,12 +751,22 @@ extern const struct_ioctlent *ioctlent;
705751
# define signalent signalent0
706752
# define ioctlent ioctlent0
707753
#endif
754+
708755
extern unsigned nsyscalls;
709756
extern unsigned nerrnos;
710757
extern unsigned nsignals;
711758
extern unsigned nioctlents;
712759
extern unsigned num_quals;
713760

761+
#if SUPPORTED_PERSONALITIES > 1
762+
# include "printers.h"
763+
extern const struct_printers *printers;
764+
# define MPERS_PRINTER_NAME(printer_name) printers->printer_name
765+
#else
766+
# include "native_printer_decls.h"
767+
# define MPERS_PRINTER_NAME(printer_name) printer_name
768+
#endif
769+
714770
/*
715771
* If you need non-NULL sysent[scno].sys_func and sysent[scno].sys_name
716772
*/
@@ -721,8 +777,12 @@ extern unsigned num_quals;
721777
#define SCNO_IN_RANGE(scno) \
722778
((unsigned long)(scno) < nsyscalls)
723779

724-
#ifndef SYS_FUNC_NAME
725-
# define SYS_FUNC_NAME(syscall_name) sys_ ## syscall_name
726-
#endif
780+
#define MPERS_FUNC_NAME__(prefix, name) prefix ## name
781+
#define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
782+
#define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
783+
784+
#define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(sys_ ## syscall_name)
727785

728786
#define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(syscall_name)(struct tcb *tcp)
787+
788+
#define MPERS_PRINTER_DECL(type, name) type MPERS_FUNC_NAME(name)

empty.h

Whitespace-only changes.

generate_mpers_am.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh -e
2+
3+
exec > mpers.am
4+
5+
echo "# Generated by $0; do not edit."
6+
echo -n 'mpers_source_files = '
7+
8+
sed -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*\([[:alnum:]][^.]*\.c\)[[:space:]]*\\$/\1/p' Makefile.am |
9+
xargs -r grep -lx '#[[:space:]]*include[[:space:]]\+MPERS_DEFS' |
10+
tr '\n' ' '
11+
echo

0 commit comments

Comments
 (0)