blob: c31a9a9c6049f360c79ca8b45779e409a7d700ee [file] [log] [blame]
Juan Cespedescac15c32003-01-31 18:58:58 +01001#include <stdio.h>
2#include <stdarg.h>
3
4#include "debug.h"
5#include "options.h"
6#include "output.h"
7
Ian Wienand9a2ad352006-02-20 22:44:45 +01008void
Juan Cespedesa413e5b2007-09-04 17:34:53 +02009debug_(int level, const char *file, int line, const char *func,
Juan Cespedesf1350522008-12-16 18:19:58 +010010 const char *fmt, ...) {
Juan Cespedescac15c32003-01-31 18:58:58 +010011 char buf[1024];
12 va_list args;
13
Juan Cespedesda9b9532009-04-07 15:33:50 +020014 if (options.debug < level) {
Juan Cespedescac15c32003-01-31 18:58:58 +010015 return;
16 }
17 va_start(args, fmt);
18 vsnprintf(buf, 1024, fmt, args);
19 va_end(args);
20
21 output_line(NULL, "DEBUG: %s:%d: %s(): %s", file, line, func, buf);
22}
Ian Wienand9a2ad352006-02-20 22:44:45 +010023
24// The following section provides a way to print things, like hex dumps,
25// with out using buffered output. This was written by Steve Munroe of IBM.
26
27#include <stdio.h>
28#include <errno.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <sys/ptrace.h>
32
Juan Cespedesf1350522008-12-16 18:19:58 +010033static int
34xwritehexl(long i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010035 int rc = 0;
36 char text[17];
37 int j;
38 unsigned long temp = (unsigned long)i;
Ian Wienand9a2ad352006-02-20 22:44:45 +010039
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 for (j = 15; j >= 0; j--) {
41 char c;
42 c = (char)((temp & 0x0f) + '0');
43 if (c > '9') {
44 c = (char)(c + ('a' - '9' - 1));
45 }
46 text[j] = c;
47 temp = temp >> 4;
48 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010049
Ian Wienand2d45b1a2006-02-20 22:48:07 +010050 rc = write(1, text, 16);
51 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010052}
53
Juan Cespedesf1350522008-12-16 18:19:58 +010054static int
55xwritec(char c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056 char temp = c;
57 char *text = &temp;
58 int rc = 0;
59 rc = write(1, text, 1);
60 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010061}
62
Juan Cespedesf1350522008-12-16 18:19:58 +010063static int
64xwritecr(void) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010065 return xwritec('\n');
Ian Wienand9a2ad352006-02-20 22:44:45 +010066}
67
Juan Cespedesf1350522008-12-16 18:19:58 +010068static int
69xwritedump(void *ptr, long addr, int len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070 int rc = 0;
71 long *tprt = (long *)ptr;
72 int i;
Ian Wienand9a2ad352006-02-20 22:44:45 +010073
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 for (i = 0; i < len; i += 8) {
75 xwritehexl(addr);
76 xwritec('-');
77 xwritec('>');
78 xwritehexl(*tprt++);
79 xwritecr();
80 addr += sizeof(long);
81 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010082
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010084}
85
Juan Cespedesf1350522008-12-16 18:19:58 +010086int
87xinfdump(long pid, void *ptr, int len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 int rc;
89 int i;
Juan Cespedese672ad12009-02-11 18:49:18 +010090 long wrdcnt;
91 long *infwords;
92 long addr;
93
94 wrdcnt = len / sizeof(long) + 1;
95 infwords = malloc(wrdcnt * sizeof(long));
96 if (!infwords) {
97 perror("ltrace: malloc");
98 exit(1);
99 }
100 addr = (long)ptr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100101
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100103
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 for (i = 0; i < wrdcnt; ++i) {
105 infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, addr);
106 addr += sizeof(long);
107 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100108
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 rc = xwritedump(infwords, (long)ptr, len);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100110
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 free(infwords);
112 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100113}