qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "J. Mayer" <l_indien@magic.fr>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [ADD] tests for PPC target.
Date: 18 Nov 2003 09:08:13 +0100	[thread overview]
Message-ID: <1069142893.14646.2271.camel@rapid> (raw)
In-Reply-To: <1069142555.13659.2265.camel@rapid>

target-ppc__tests__ctrace.c.diff

This is a program which follows the execution of it's child and
print the state of the CPU for each executed instruction.
The output can be compared with qemu.log to find qemu bugs...

diff -urNbB -x CVS qemu-current/target-ppc/tests/ctrace.c qemu/target-ppc/tests/ctrace.c
--- qemu-current/target-ppc/tests/ctrace.c	Thu Jan  1 01:00:00 1970
+++ qemu/target-ppc/tests/ctrace.c	Wed Nov 12 10:37:09 2003
@@ -0,0 +1,177 @@
+/*
+ *  CPU trace debugger.
+ * 
+ *  Copyright (c) 2003 Jocelyn Mayer
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ * This utility traces the CPU state for each execution step of a program.
+ * Of course, this is awfully slow.
+ * This can be used to compare the result of the execution of a native program
+ * vs the same one emulated by qemu.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+
+#include <linux/ptrace.h>
+#include <linux/user.h>
+
+#if defined (__powerpc__)
+static void dump_cpu_state (int child)
+{
+    unsigned long GPR[32];
+    unsigned long nip, LR, CTR, CR;
+    unsigned long opcode = 0, tmp;
+    int i;
+    
+    /* Retrieve GPR's */
+    for (i = 0; i < 32; i++)
+        GPR[i] = ptrace(PTRACE_PEEKUSR, child, (void *)(i << 2), &GPR[i]);
+
+    /* Retrieve nip */
+    nip = ptrace(PTRACE_PEEKUSR, child, (void *)(32 << 2), &nip);
+    /* Retrieve LR  */
+    LR = ptrace(PTRACE_PEEKUSR, child, (void *)(36 << 2), &LR);
+    /* Retrieve CTR  */
+    CTR = ptrace(PTRACE_PEEKUSR, child, (void *)(35 << 2), &CTR);
+    /* Retrieve CR  */
+    CR = ptrace(PTRACE_PEEKUSR, child, (void *)(38 << 2), &CR);
+    /* Retrieve current opcode */
+    if (nip == -1)
+        return;
+
+    opcode = ptrace(PTRACE_PEEKTEXT, child, nip - 4, NULL);
+    /* Dump all */
+    printf("nip=0x%08lx LR=0x%08lx CTR=0x%08lx\n",
+           nip, LR, CTR);
+    for (i = 0; i < 32; i++) {
+        if ((i & 7) == 0)
+            printf("GPR%02d: ", i);
+        printf("%08lx ", GPR[i]);
+        if ((i & 7) == 7)
+            printf("\n");
+    }
+    printf("0x%08lx: translate opcode %08lx\n", nip, opcode);
+}
+#elif defined (__i386__)
+static void dump_cpu_state (int child)
+{
+    struct user_regs_struct regs;
+
+    memset(&regs, 0, sizeof(regs));
+    if (ptrace(PTRACE_GETREGS, child, NULL, &regs) < 0) {
+        printf("PTRACE_GETREGS: %m\n");
+        return;
+    }
+    printf("nip=0x%08lx\n", regs.eip);
+    printf("eax=0x%08lx ebx=0x%08lx ecx=0x%08lx edx=0x%08lx\n",
+           regs.eax, regs.ebx, regs.ecx, regs.edx);
+    printf("esi=0x%08lx edi=0x%08lx esp=0x%08lx ebp=0x%08lx\n",
+           regs.esi, regs.edi, regs.esp, regs.ebp);
+    fflush(stdout);
+}
+#else
+#error "Unsupported target CPU"
+#endif
+
+/* Main loop */
+__attribute__((noreturn))
+int main (int argc, char **argv)
+{
+    pid_t child, me;
+    int status;
+
+    me = getpid();
+    if (argc < 2) {
+        fprintf(stderr, "Usage : ctrace prog args...\n");
+        fprintf(stderr, "Need a program to be inspected !\n");
+        fflush(stderr);
+        exit(1);
+    }
+    if (access(argv[1], R_OK | X_OK) < 0) {
+        fprintf(stderr, "Can't execute %s\n", argv[1]);
+        fflush(stderr);
+        exit(1);
+    }
+    fflush(stdout);
+
+    child = fork();
+    switch (child) {
+    case 0:
+        /* Child */
+        /* Initiate trace */
+        if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0) {
+            fprintf(stderr, "Ptrace failed : %m\n");
+            fflush(stderr);
+            exit(1);
+        }
+        /* Launch the program to be traced */
+        execv(argv[1], &argv[1]);
+        /* Can't come here ! */
+        fprintf(stderr, "Exec failed: %m\n");
+        fflush(stderr);
+        exit(1);
+    case -1:
+        /* Error case... */
+        fprintf(stderr, "Can't fork to execute %s\n", argv[1]);
+        fflush(stderr);
+        exit(1);
+    default:
+        break;
+    }
+    /* Wait for the child to be launched */
+    while (0) {
+        while (waitpid(child, &status, WUNTRACED) != child)
+            continue;
+        if (WIFSTOPPED(status))
+            break;
+    }
+    /* Now, trace it ! */
+    while (1) {
+        while (waitpid(child, &status, WUNTRACED) != child)
+            continue;
+        if (!WIFSTOPPED(status) && WSTOPSIG(status) != SIGTRAP)
+            break;
+        if (WSTOPSIG(status) != SIGTRAP) {
+            printf("%d: seen signal %d\n", me, WSTOPSIG(status));
+            fflush(stdout);
+        }
+        dump_cpu_state(child);
+        while (ptrace(PTRACE_SINGLESTEP, child, 1, SIGCONT) < 0)
+            continue;
+    }
+    printf("process %s ", argv[1]);
+    fflush(stdout);
+    if (WIFEXITED(status)) {
+        printf("ended with code: %d\n", WEXITSTATUS(status));
+        fflush(stdout);
+    } else if (WIFSIGNALED(status)) {
+        printf("killed by signal: %d\n", WTERMSIG(status));
+        fflush(stdout);
+    } else if (WIFSTOPPED(status)) {
+        /* Should never happen */
+        printf("stopped\n");
+        fflush(stdout);
+    }
+
+    exit(0);
+}

  parent reply	other threads:[~2003-11-18  9:07 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-17  9:51 [Qemu-devel] new knoppix SegFault Jens Arm
2003-11-18  7:15 ` [Qemu-devel] [PATCH] Fixes for qemu J. Mayer
2003-11-18  7:30   ` J. Mayer
2003-11-18  7:31   ` Chad Page
2003-11-18  7:32   ` J. Mayer
2003-11-18  7:33   ` J. Mayer
2003-11-18  7:34   ` J. Mayer
2003-11-18  8:24   ` J. Mayer
2003-11-18  7:22 ` [Qemu-devel] [ADD] floppy disk emulation J. Mayer
2003-11-18  7:37   ` J. Mayer
2003-11-18  7:38   ` J. Mayer
2003-11-18  7:39   ` J. Mayer
2003-11-18  7:39   ` J. Mayer
2003-11-18  8:24   ` J. Mayer
2003-11-18  7:28 ` [Qemu-devel] [ADD] PPC processor emulation J. Mayer
2003-11-18  7:43   ` J. Mayer
2003-11-18  7:43   ` J. Mayer
2003-11-18  7:44   ` J. Mayer
2003-11-18  7:45   ` J. Mayer
2003-11-18  7:45   ` J. Mayer
2003-11-18  7:46   ` J. Mayer
2003-11-18  7:46   ` J. Mayer
2003-11-18  7:48   ` J. Mayer
2003-11-18  7:48   ` J. Mayer
2003-11-18  7:49   ` J. Mayer
2003-11-18  7:50   ` J. Mayer
2003-11-18  7:50   ` J. Mayer
2003-11-18  7:51   ` J. Mayer
2003-11-18  7:53   ` J. Mayer
2003-11-18  7:54   ` J. Mayer
2003-11-18  7:55   ` J. Mayer
2003-11-18  7:56   ` J. Mayer
2003-11-18  7:56   ` J. Mayer
2003-11-18  7:57   ` J. Mayer
2003-11-18  7:58   ` J. Mayer
2003-11-18  7:59   ` J. Mayer
2003-11-18  7:59   ` J. Mayer
2003-11-18  8:00   ` J. Mayer
2003-11-18  8:02   ` [Qemu-devel] [ADD] tests for PPC target J. Mayer
2003-11-18  8:06     ` J. Mayer
2003-11-18  8:08     ` J. Mayer [this message]
2003-11-18  8:08     ` J. Mayer
2003-11-18  8:09     ` J. Mayer
2003-11-18  8:10     ` J. Mayer
2003-11-18  8:25     ` J. Mayer
2003-11-18  8:24   ` [Qemu-devel] [ADD] PPC processor emulation J. Mayer
2003-11-18  9:37   ` Gwenole Beauchesne
2003-11-18 10:37     ` J. Mayer
2003-11-18 11:39       ` Raymond W. Lucke IV
2003-11-18 12:13         ` J. Mayer
2003-11-18 20:24           ` Raymond W. Lucke IV
2003-11-18 20:44             ` Jocelyn Mayer
2003-11-18 21:48               ` Chad Page
2003-11-18 22:50                 ` J. Mayer
2003-11-19  1:11                   ` Benjamin Herrenschmidt
2003-11-19 15:35                     ` Jocelyn Mayer
2003-11-18 12:24       ` Gwenole Beauchesne
2003-11-18 12:57         ` Johan Rydberg
2003-11-18 14:52           ` Gwenole Beauchesne
2003-11-18 14:59         ` Jocelyn Mayer
2003-11-18  7:29 ` [Qemu-devel] [PATCH] Term prompt for qemu J. Mayer
2003-11-18  8:11   ` J. Mayer
2003-11-18  8:11   ` J. Mayer
2003-11-18  8:13   ` J. Mayer
2003-11-18  8:25   ` J. Mayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1069142893.14646.2271.camel@rapid \
    --to=l_indien@magic.fr \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).