Replace readdir_r with readdir
[platform/upstream/ltrace.git] / debug.c
diff --git a/debug.c b/debug.c
index 1b03189..ce220fe 100644 (file)
--- a/debug.c
+++ b/debug.c
@@ -1,5 +1,6 @@
 /*
  * This file is part of ltrace.
+ * Copyright (C) 2012 Petr Machata, Red Hat Inc.
  * Copyright (C) 2003,2008,2009 Juan Cespedes
  * Copyright (C) 2006 Ian Wienand
  *
@@ -23,6 +24,7 @@
 #include <stdarg.h>
 
 #include "common.h"
+#include "backend.h"
 
 void
 debug_(int level, const char *file, int line, const char *fmt, ...) {
@@ -40,95 +42,26 @@ debug_(int level, const char *file, int line, const char *fmt, ...) {
        fflush(options.output);
 }
 
-/*
- * The following section provides a way to print things, like hex dumps,
- * with out using buffered output.  This was written by Steve Munroe of IBM.
- */
-
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <sys/ptrace.h>
-
-static int
-xwritehexl(long i) {
-       int rc = 0;
-       char text[17];
-       int j;
-       unsigned long temp = (unsigned long)i;
-
-       for (j = 15; j >= 0; j--) {
-               char c;
-               c = (char)((temp & 0x0f) + '0');
-               if (c > '9') {
-                       c = (char)(c + ('a' - '9' - 1));
-               }
-               text[j] = c;
-               temp = temp >> 4;
-       }
-
-       rc = write(1, text, 16);
-       return rc;
-}
-
-static int
-xwritec(char c) {
-       char temp = c;
-       char *text = &temp;
-       int rc = 0;
-       rc = write(1, text, 1);
-       return rc;
-}
-
-static int
-xwritecr(void) {
-       return xwritec('\n');
-}
-
 static int
-xwritedump(void *ptr, long addr, int len) {
-       int rc = 0;
-       long *tprt = (long *)ptr;
-       int i;
-
-       for (i = 0; i < len; i += 8) {
-               xwritehexl(addr);
-               xwritec('-');
-               xwritec('>');
-               xwritehexl(*tprt++);
-               xwritecr();
+xwritedump(long *ptr, arch_addr_t addr, size_t count)
+{
+       size_t i;
+       for (i = 0; i < count; ++i) {
+               if (fprintf(stderr, "%p->%0*lx\n",
+                           addr, 2 * (int)sizeof(long), ptr[i]) < 0)
+                       return -1;
                addr += sizeof(long);
        }
 
-       return rc;
+       return 0;
 }
 
 int
-xinfdump(long pid, void *ptr, int len) {
-       int rc;
-       int i;
-       long wrdcnt;
-       long *infwords;
-       long addr;
-
-       wrdcnt = len / sizeof(long) + 1;
-       infwords = malloc(wrdcnt * sizeof(long));
-       if (!infwords) {
-               perror("ltrace: malloc");
-               exit(1);
-       }
-       addr = (long)ptr;
-
-       addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
-
-       for (i = 0; i < wrdcnt; ++i) {
-               infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, (void *)addr, NULL);
-               addr += sizeof(long);
-       }
-
-       rc = xwritedump(infwords, (long)ptr, len);
-
-       free(infwords);
-       return rc;
+xinfdump(struct process *proc, arch_addr_t addr, size_t length)
+{
+       unsigned char buf[length];
+       size_t got = umovebytes(proc, addr, buf, length);
+       if (got == (size_t)-1)
+               return -1;
+       return xwritedump((long *)buf, addr, got / sizeof(long));
 }