From patchwork Tue May 25 10:24:12 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 53531 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 05464B7D51 for ; Tue, 25 May 2010 20:38:17 +1000 (EST) Received: from localhost ([127.0.0.1]:44922 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OGrWV-0003vh-Al for incoming@patchwork.ozlabs.org; Tue, 25 May 2010 06:38:11 -0400 Received: from [140.186.70.92] (port=47926 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OGrJf-0006Rf-5s for qemu-devel@nongnu.org; Tue, 25 May 2010 06:25:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OGrJc-0001XM-Vq for qemu-devel@nongnu.org; Tue, 25 May 2010 06:24:55 -0400 Received: from mtagate6.de.ibm.com ([195.212.17.166]:60659) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OGrJc-0001Rh-L5 for qemu-devel@nongnu.org; Tue, 25 May 2010 06:24:52 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate6.de.ibm.com (8.13.1/8.13.1) with ESMTP id o4PAOZPK027533 for ; Tue, 25 May 2010 10:24:35 GMT Received: from d12av01.megacenter.de.ibm.com (d12av01.megacenter.de.ibm.com [9.149.165.212]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o4PAOZE8909324 for ; Tue, 25 May 2010 12:24:35 +0200 Received: from d12av01.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av01.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o4PAOYtG009027 for ; Tue, 25 May 2010 12:24:35 +0200 Received: from stefan-thinkpad.manchester-maybrook.uk.ibm.com (dyn-9-174-219-32.manchester-maybrook.uk.ibm.com [9.174.219.32]) by d12av01.megacenter.de.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id o4PAOX0e008947; Tue, 25 May 2010 12:24:34 +0200 From: Stefan Hajnoczi To: , Date: Tue, 25 May 2010 11:24:12 +0100 Message-Id: <1274783056-14759-4-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1274783056-14759-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1274783056-14759-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Jan Kiszka , Anthony Liguori , Avi Kivity , Stefan Hajnoczi , Prerna Saxena Subject: [Qemu-devel] [PATCH 3/7] trace: Add simple built-in tracing backend X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds a simple tracer which produces binary trace files and is built into QEMU. The main purpose of this patch is to show how new tracing backends can be added to tracetool. To try out the simple backend: ./configure --trace-backend=simple make After running QEMU you can pretty-print the trace: ./simpletrace.py trace-events /tmp/trace.log Signed-off-by: Stefan Hajnoczi --- I intend for this tracing backend to be replaced by something based on Prerna's work. For now it is useful for basic tracing. v2: * Make simpletrace.py parse trace-events instead of generating Python .gitignore | 1 + Makefile.objs | 3 ++ configure | 2 +- simpletrace.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ simpletrace.py | 53 ++++++++++++++++++++++++++++++++++++++ tracetool | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 simpletrace.c create mode 100755 simpletrace.py diff --git a/.gitignore b/.gitignore index 4644557..5128452 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ qemu-monitor.texi *.log *.pdf *.pg +*.pyc *.toc *.tp *.vr diff --git a/Makefile.objs b/Makefile.objs index 20e709e..7cb40ac 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -255,6 +255,9 @@ libdis-$(CONFIG_SPARC_DIS) += sparc-dis.o # trace trace-obj-y = trace.o +ifeq ($(TRACE_BACKEND),simple) +trace-obj-y += simpletrace.o +endif vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS) diff --git a/configure b/configure index e94e113..7d2c69b 100755 --- a/configure +++ b/configure @@ -829,7 +829,7 @@ echo " --enable-docs enable documentation build" echo " --disable-docs disable documentation build" echo " --disable-vhost-net disable vhost-net acceleration support" echo " --enable-vhost-net enable vhost-net acceleration support" -echo " --trace-backend=B Trace backend nop" +echo " --trace-backend=B Trace backend nop simple" echo "" echo "NOTE: The object files are built at the place where configure is launched" exit 1 diff --git a/simpletrace.c b/simpletrace.c new file mode 100644 index 0000000..2fec4d3 --- /dev/null +++ b/simpletrace.c @@ -0,0 +1,64 @@ +#include +#include +#include "trace.h" + +typedef struct { + unsigned long event; + unsigned long x1; + unsigned long x2; + unsigned long x3; + unsigned long x4; + unsigned long x5; +} TraceRecord; + +enum { + TRACE_BUF_LEN = 64 * 1024 / sizeof(TraceRecord), +}; + +static TraceRecord trace_buf[TRACE_BUF_LEN]; +static unsigned int trace_idx; +static FILE *trace_fp; + +static void trace(TraceEvent event, unsigned long x1, + unsigned long x2, unsigned long x3, + unsigned long x4, unsigned long x5) { + TraceRecord *rec = &trace_buf[trace_idx]; + rec->event = event; + rec->x1 = x1; + rec->x2 = x2; + rec->x3 = x3; + rec->x4 = x4; + rec->x5 = x5; + + if (++trace_idx == TRACE_BUF_LEN) { + trace_idx = 0; + + if (!trace_fp) { + trace_fp = fopen("/tmp/trace.log", "w"); + } + if (trace_fp) { + size_t result = fwrite(trace_buf, sizeof trace_buf, 1, trace_fp); + result = result; + } + } +} + +void trace1(TraceEvent event, unsigned long x1) { + trace(event, x1, 0, 0, 0, 0); +} + +void trace2(TraceEvent event, unsigned long x1, unsigned long x2) { + trace(event, x1, x2, 0, 0, 0); +} + +void trace3(TraceEvent event, unsigned long x1, unsigned long x2, unsigned long x3) { + trace(event, x1, x2, x3, 0, 0); +} + +void trace4(TraceEvent event, unsigned long x1, unsigned long x2, unsigned long x3, unsigned long x4) { + trace(event, x1, x2, x3, x4, 0); +} + +void trace5(TraceEvent event, unsigned long x1, unsigned long x2, unsigned long x3, unsigned long x4, unsigned long x5) { + trace(event, x1, x2, x3, x4, x5); +} diff --git a/simpletrace.py b/simpletrace.py new file mode 100755 index 0000000..d6631ba --- /dev/null +++ b/simpletrace.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +import sys +import struct +import re + +trace_fmt = 'LLLLLL' +trace_len = struct.calcsize(trace_fmt) +event_re = re.compile(r'(disable\s+)?([a-zA-Z0-9_]+)\(([^)]*)\)\s+"([^"]*)"') + +def parse_events(fobj): + def get_argnames(args): + return tuple(arg.split()[-1].lstrip('*') for arg in args.split(',')) + + events = {} + event_num = 0 + for line in fobj: + m = event_re.match(line.strip()) + if m is None: + continue + + disable, name, args, fmt = m.groups() + if disable: + continue + + events[event_num] = (name,) + get_argnames(args) + event_num += 1 + return events + +def read_record(fobj): + s = fobj.read(trace_len) + if len(s) != trace_len: + return None + return struct.unpack(trace_fmt, s) + +def format_record(events, rec): + event = events[rec[0]] + fields = [event[0]] + for i in xrange(1, len(event)): + fields.append('%s=0x%x' % (event[i], rec[i])) + return ' '.join(fields) + +if len(sys.argv) != 3: + sys.stderr.write('usage: %s \n' % sys.argv[0]) + sys.exit(1) + +events = parse_events(open(sys.argv[1], 'r')) +f = open(sys.argv[2], 'rb') +while True: + rec = read_record(f) + if rec is None: + break + + print format_record(events, rec) diff --git a/tracetool b/tracetool index 53d3612..f094ddc 100755 --- a/tracetool +++ b/tracetool @@ -3,11 +3,12 @@ usage() { cat >&2 <