blob: e6b97487fcc859a78bf8478e0152647af5c46bbf [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 Cespedescd8976d2009-05-14 13:47:58 +02009debug_(int level, const char *file, int line, const char *fmt, ...) {
Juan Cespedescac15c32003-01-31 18:58:58 +010010 char buf[1024];
11 va_list args;
12
Juan Cespedescd8976d2009-05-14 13:47:58 +020013 if (!(options.debug & level)) {
Juan Cespedescac15c32003-01-31 18:58:58 +010014 return;
15 }
16 va_start(args, fmt);
17 vsnprintf(buf, 1024, fmt, args);
18 va_end(args);
19
Juan Cespedescd8976d2009-05-14 13:47:58 +020020 output_line(NULL, "DEBUG: %s:%d: %s", file, line, buf);
Juan Cespedescac15c32003-01-31 18:58:58 +010021}
Ian Wienand9a2ad352006-02-20 22:44:45 +010022
23// The following section provides a way to print things, like hex dumps,
24// with out using buffered output. This was written by Steve Munroe of IBM.
25
26#include <stdio.h>
27#include <errno.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <sys/ptrace.h>
31
Juan Cespedesf1350522008-12-16 18:19:58 +010032static int
33xwritehexl(long i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010034 int rc = 0;
35 char text[17];
36 int j;
37 unsigned long temp = (unsigned long)i;
Ian Wienand9a2ad352006-02-20 22:44:45 +010038
Ian Wienand2d45b1a2006-02-20 22:48:07 +010039 for (j = 15; j >= 0; j--) {
40 char c;
41 c = (char)((temp & 0x0f) + '0');
42 if (c > '9') {
43 c = (char)(c + ('a' - '9' - 1));
44 }
45 text[j] = c;
46 temp = temp >> 4;
47 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010048
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049 rc = write(1, text, 16);
50 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010051}
52
Juan Cespedesf1350522008-12-16 18:19:58 +010053static int
54xwritec(char c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010055 char temp = c;
56 char *text = &temp;
57 int rc = 0;
58 rc = write(1, text, 1);
59 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010060}
61
Juan Cespedesf1350522008-12-16 18:19:58 +010062static int
63xwritecr(void) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010064 return xwritec('\n');
Ian Wienand9a2ad352006-02-20 22:44:45 +010065}
66
Juan Cespedesf1350522008-12-16 18:19:58 +010067static int
68xwritedump(void *ptr, long addr, int len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 int rc = 0;
70 long *tprt = (long *)ptr;
71 int i;
Ian Wienand9a2ad352006-02-20 22:44:45 +010072
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 for (i = 0; i < len; i += 8) {
74 xwritehexl(addr);
75 xwritec('-');
76 xwritec('>');
77 xwritehexl(*tprt++);
78 xwritecr();
79 addr += sizeof(long);
80 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010081
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010083}
84
Juan Cespedesf1350522008-12-16 18:19:58 +010085int
86xinfdump(long pid, void *ptr, int len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 int rc;
88 int i;
Juan Cespedese672ad12009-02-11 18:49:18 +010089 long wrdcnt;
90 long *infwords;
91 long addr;
92
93 wrdcnt = len / sizeof(long) + 1;
94 infwords = malloc(wrdcnt * sizeof(long));
95 if (!infwords) {
96 perror("ltrace: malloc");
97 exit(1);
98 }
99 addr = (long)ptr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100100
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100102
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 for (i = 0; i < wrdcnt; ++i) {
104 infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, addr);
105 addr += sizeof(long);
106 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100107
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 rc = xwritedump(infwords, (long)ptr, len);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100109
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 free(infwords);
111 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100112}