blob: 32fb40f7d07e81242ab342ec00eac04e40459836 [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,
10 const char *fmt, ...)
Ian Wienand2d45b1a2006-02-20 22:48:07 +010011{
Juan Cespedescac15c32003-01-31 18:58:58 +010012 char buf[1024];
13 va_list args;
14
15 if (opt_d < level) {
16 return;
17 }
18 va_start(args, fmt);
19 vsnprintf(buf, 1024, fmt, args);
20 va_end(args);
21
22 output_line(NULL, "DEBUG: %s:%d: %s(): %s", file, line, func, buf);
23}
Ian Wienand9a2ad352006-02-20 22:44:45 +010024
25// The following section provides a way to print things, like hex dumps,
26// with out using buffered output. This was written by Steve Munroe of IBM.
27
28#include <stdio.h>
29#include <errno.h>
30#include <unistd.h>
31#include <stdlib.h>
32#include <sys/ptrace.h>
33
Juan Cespedesaee09312007-08-31 18:49:48 +020034static int xwritehexl(long i)
Ian Wienand9a2ad352006-02-20 22:44:45 +010035{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010036 int rc = 0;
37 char text[17];
38 int j;
39 unsigned long temp = (unsigned long)i;
Ian Wienand9a2ad352006-02-20 22:44:45 +010040
Ian Wienand2d45b1a2006-02-20 22:48:07 +010041 for (j = 15; j >= 0; j--) {
42 char c;
43 c = (char)((temp & 0x0f) + '0');
44 if (c > '9') {
45 c = (char)(c + ('a' - '9' - 1));
46 }
47 text[j] = c;
48 temp = temp >> 4;
49 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010050
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051 rc = write(1, text, 16);
52 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010053}
54
Juan Cespedesaee09312007-08-31 18:49:48 +020055static int xwritec(char c)
Ian Wienand9a2ad352006-02-20 22:44:45 +010056{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010057 char temp = c;
58 char *text = &temp;
59 int rc = 0;
60 rc = write(1, text, 1);
61 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010062}
63
Juan Cespedesaee09312007-08-31 18:49:48 +020064static int xwritecr(void)
Ian Wienand9a2ad352006-02-20 22:44:45 +010065{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 return xwritec('\n');
Ian Wienand9a2ad352006-02-20 22:44:45 +010067}
68
Juan Cespedesaee09312007-08-31 18:49:48 +020069static int xwritedump(void *ptr, long addr, int len)
Ian Wienand9a2ad352006-02-20 22:44:45 +010070{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010071 int rc = 0;
72 long *tprt = (long *)ptr;
73 int i;
Ian Wienand9a2ad352006-02-20 22:44:45 +010074
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 for (i = 0; i < len; i += 8) {
76 xwritehexl(addr);
77 xwritec('-');
78 xwritec('>');
79 xwritehexl(*tprt++);
80 xwritecr();
81 addr += sizeof(long);
82 }
Ian Wienand9a2ad352006-02-20 22:44:45 +010083
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +010085}
86
87int xinfdump(long pid, void *ptr, int len)
88{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 int rc;
90 int i;
91 long wrdcnt = len / sizeof(long) + 1;
92 long *infwords = malloc(wrdcnt * sizeof(long));
93 long addr = (long)ptr;
Ian Wienand9a2ad352006-02-20 22:44:45 +010094
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
Ian Wienand9a2ad352006-02-20 22:44:45 +010096
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 for (i = 0; i < wrdcnt; ++i) {
98 infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, addr);
99 addr += sizeof(long);
100 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100101
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 rc = xwritedump(infwords, (long)ptr, len);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100103
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 free(infwords);
105 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100106}