Skip to content

Commit

Permalink
fix issues with BFD arches (#7)
Browse files Browse the repository at this point in the history
It came out that BFD architectures in `qemu/dis/bfd.h` folder is 9 years older,
than architectures in the `bap-frames/libtrace`, and the latter is 14
years older than the latest BFD release. In fact, the BFD architectures
are not stable, and numbers for them are allocated at each configuration
and depends on the configuration options. The choice to rely on them was
a bad decision. The better approach would be to use our own wired format
for arches, specified in protobuf or something like this. But so far,
we're relying on piqi, that doesn't have (a) normal enums with fixed
values, (b) normal extension mechanism. So we will still rely on the old
way of specifying architectures.

Now we will use architectures from `frames/libtrace/` library, to
ensure, that everyone has the same constants. To reduce confusion with
BFD we've moved all definitions to the `frames` namespace (i.e.,
`s/bfd_/frames_`).

This PR also fixes numerous issues with the build system:

1. protobuf now is built as a part of `all` target, no need to
   do `make -C protobuf`
2. protobuf will generate `librotoframes` library, instead of libtrace
   that nameclashed with at least two libraries: trace library from the
   qemu itself and libtrace from the bap-frames
3. configure script will properly expand paths
4. protobuf target will now depend on the `piqi` file in the frames
   library and if the latter is changed, the library will be regenerated
   and recompiled, as well as all dependencies.
  • Loading branch information
ivg committed Apr 22, 2016
1 parent 465ce68 commit c5ae159
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,6 @@ cscope.*
tags
TAGS
*~
/protobuf/frame.piqi.pb-c.c
/protobuf/frame.piqi.pb-c.h
/protobuf/frame.piqi.proto
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ ifneq ($(wildcard config-host.mak),)
all:
include config-host.mak

ifeq ($(HAS_TRACEWRAP),y)
all: protoframes

protoframes:
make -C protobuf
endif


# Check that we're not trying to do an out-of-tree build from
# a tree that's been used for an in-tree build.
ifneq ($(realpath $(SRC_PATH)),$(realpath .))
Expand Down Expand Up @@ -146,8 +154,13 @@ ifeq ($(CONFIG_SMARTCARD_NSS),y)
include $(SRC_PATH)/libcacard/Makefile
endif




all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules



vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)

vl.o: QEMU_CFLAGS+=$(SDL_CFLAGS)
Expand Down
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ project.

# Installing released binaries

If you don't want to mess with the source and building, then you can just
dowload a tarball with prebuilt binaries. Look at the latest release and
If you don't want to mess with the source and building, then you can just
dowload a tarball with prebuilt binaries. Look at the latest release and
it might happen, that we have built binaries for your linux distribution,
if it is not the case, then create an issue, and we will build it for you.

Let's pretend, that you're using Ubuntu Trusty, and install it. First
Let's pretend, that you're using Ubuntu Trusty, and install it. First
download it with your favorite downloader:

```
wget https://github.com/BinaryAnalysisPlatform/qemu/releases/download/v2.0.0-tracewrap-alpha/qemu-tracewrap-ubuntu-14.04.4-LTS.tgz
```

Install it in the specified prefix with a command like `tar -C <prefix> -xf qemu-tracewrap-ubuntu-14.04.4-LTS.tgz`, e.g.,
Install it in the specified prefix with a command like `tar -C <prefix> -xf qemu-tracewrap-ubuntu-14.04.4-LTS.tgz`, e.g.,
to install in your home directory:
```
tar -C $HOME -xf qemu-tracewrap-ubuntu-14.04.4-LTS.tgz
Expand Down Expand Up @@ -62,42 +62,36 @@ $ opam install piqi

## Building

Download [bap-traces](https://github.com/BinaryAnalysisPlatform/bap-traces) with
Download [bap-frames](https://github.com/BinaryAnalysisPlatform/bap-frames) with
following command

```bash
$ git clone https://github.com/BinaryAnalysisPlatform/bap-traces.git
$ git clone https://github.com/BinaryAnalysisPlatform/bap-frames.git
```

Download qemu tracer with following command

```bash
$ git clone [email protected]:BinaryAnalysisPlatform/qemu.git -b tracewrap
$ git clone [email protected]:BinaryAnalysisPlatform/qemu.git
```

Change folder to qemu and build tracer with command
Change folder to qemu and build tracer:
```bash
$ ./configure --prefix=$HOME --with-tracewrap=`realpath ../bap-frames` \
--extra-ldflags=-Lprotobuf --target-list="arm-linux-user i386-linux-user \
x86_64-linux-user mips-linux-user"
$ make -C protobuf
$ cd qemu
$ ./configure --prefix=$HOME --with-tracewrap=../bap-frames --target-list="`echo {arm,i386,x86_64,mips}-linux-user`"
$ make
$ make install
```

# Usage

To run executable `exec` and to save the trace data to `exec.trace`, use
To run executable `exec` compiled for `arch`, use `qemu-arch exec` command, e.g.,
`qemu-x86_64 /bin/ls`. It will dump the trace into `ls.frames` file. You can configure
the filename with `-tracefile` option, e.g., `qemu-arm -tracefile arm.ls.frames ls`

```bash
$ qemu-arm -tracefile exec.trace exec # trace ARM target executable
$ qemu-i386 -tracefile exec.trace exec # trace X86 target executable
$ qemu-x86_64 -tracefile exec.trace exec # trace X86-64 target executable
$ qemu-mips -tracefile exec.trace exec # trace MIPS target executable
```

Hints: use option -L to set the elf interpreter prefix to 'path'. Use
[fetchlibs.sh](https://raw.githubusercontent.com/BinaryAnalysisPlatform/bap-traces/master/test/fetchlibs.sh)
[fetchlibs.sh](https://raw.githubusercontent.com/BinaryAnalysisPlatform/bap-frames/master/test/fetchlibs.sh)
to download arm and x86 libraries.

# Notes
Expand Down
11 changes: 5 additions & 6 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -749,13 +749,13 @@ for opt do
;;
--with-trace-file=*) trace_file="$optarg"
;;
--with-tracewrap=*)
--with-tracewrap=*)
tracewrap="yes"
TRACEWRAP_DIR="$optarg"
QEMU_INCLUDES="$QEMU_INCLUDES -I\$(SRC_PATH)/protobuf/ "
TRACEWRAP_DIR="`realpath $optarg`"
QEMU_INCLUDES="$QEMU_INCLUDES -I\$(SRC_PATH)/protobuf/ -I$TRACEWRAP_DIR/libtrace/src/"
QEMU_CFLAGS="$QEMU_CFLAGS -L\$(SRC_PATH)/protobuf -DHAS_TRACEWRAP "
LIBS="$LIBS -ltrace -lprotobuf-c "
;;
LIBS="$LIBS -lprotoframes -lprotobuf-c "
;;
--enable-gprof) gprof="yes"
;;
--enable-gcov) gcov="yes"
Expand Down Expand Up @@ -5227,4 +5227,3 @@ printf "exec" >>config.status
printf " '%s'" "$0" "$@" >>config.status
echo >>config.status
chmod +x config.status

6 changes: 6 additions & 0 deletions linux-user/arm/trace_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "frame_arch.h"

const uint64_t frame_arch = frame_arch_arm;
const uint64_t frame_mach = frame_mach_arm_unknown;
6 changes: 3 additions & 3 deletions linux-user/i386/trace_info.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "disas/bfd.h"
#include "frame_arch.h"

const uint64_t bfd_arch = bfd_arch_i386;
const uint64_t bfd_machine = bfd_mach_i386_i386;
const uint64_t frame_arch = frame_arch_i386;
const uint64_t frame_mach = frame_mach_i386_i386;
7 changes: 3 additions & 4 deletions linux-user/mips/trace_info.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "disas/bfd.h"
#include "frame_arch.h"

const uint64_t bfd_arch = bfd_arch_mips;
const uint64_t bfd_machine = 32 ; /* bfd_mach_mipsisa32 */
/* our bfd.h is so outdated, that it doesn't include it.*/
const uint64_t frame_arch = frame_arch_mips;
const uint64_t frame_mach = frame_mach_mipsisa32 ;
6 changes: 3 additions & 3 deletions linux-user/x86_64/trace_info.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "disas/bfd.h"
#include "frame_arch.h"

const uint64_t bfd_arch = bfd_arch_i386;
const uint64_t bfd_machine = bfd_mach_x86_64;
const uint64_t frame_arch = frame_arch_i386;
const uint64_t frame_mach = frame_mach_x86_64;
13 changes: 7 additions & 6 deletions protobuf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ CC=gcc

FILENAME=frame.piqi
EXT=pb-c
PROTOFILE=$(TRACEWRAP_DIR)/piqi/$(FILENAME)

all: libtrace.a
all: libprotoframes.a

$(FILENAME).proto:
piqi to-proto $(TRACEWRAP_DIR)/$(FILENAME) -o $(FILENAME).proto
$(FILENAME).proto: $(PROTOFILE)
piqi to-proto $(PROTOFILE) -o $(FILENAME).proto

$(FILENAME).$(EXT).c: $(FILENAME).proto
protoc-c --c_out=. $(FILENAME).proto
Expand All @@ -18,8 +19,8 @@ $(FILENAME).$(EXT).c: $(FILENAME).proto
$(FILENAME).$(EXT).o: $(FILENAME).$(EXT).c $(FILENAME).$(EXT).h
$(CC) -fPIC -c -o $(FILENAME).$(EXT).o $(FILENAME).$(EXT).c

libtrace.a: $(FILENAME).$(EXT).o
ar rcs libtrace.a $(FILENAME).$(EXT).o
libprotoframes.a: $(FILENAME).$(EXT).o
ar rcs libprotoframes.a $(FILENAME).$(EXT).o

clean:
rm libtrace.a $(FILENAME).$(EXT).[hco]
rm $(FILENAME).$(EXT).[hco] $(FILENAME).proto
6 changes: 0 additions & 6 deletions target-arm/trace_info.h

This file was deleted.

4 changes: 2 additions & 2 deletions tracewrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ static void write_header(void) {
uint64_t toc_off = 0L;
WRITE(magic_number);
WRITE(out_trace_version);
WRITE(bfd_arch);
WRITE(bfd_machine);
WRITE(frame_arch);
WRITE(frame_mach);
WRITE(toc_num_frames);
WRITE(toc_off);
}
Expand Down

0 comments on commit c5ae159

Please sign in to comment.