debug.c revision a413e5b8880de643a83ad124d078091c0956fe1d
1#include <stdio.h>
2#include <stdarg.h>
3
4#include "debug.h"
5#include "options.h"
6#include "output.h"
7
8void
9debug_(int level, const char *file, int line, const char *func,
10		const char *fmt, ...)
11{
12	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}
24
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
34static int xwritehexl(long i)
35{
36	int rc = 0;
37	char text[17];
38	int j;
39	unsigned long temp = (unsigned long)i;
40
41	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	}
50
51	rc = write(1, text, 16);
52	return rc;
53}
54
55static int xwritec(char c)
56{
57	char temp = c;
58	char *text = &temp;
59	int rc = 0;
60	rc = write(1, text, 1);
61	return rc;
62}
63
64static int xwritecr(void)
65{
66	return xwritec('\n');
67}
68
69static int xwritedump(void *ptr, long addr, int len)
70{
71	int rc = 0;
72	long *tprt = (long *)ptr;
73	int i;
74
75	for (i = 0; i < len; i += 8) {
76		xwritehexl(addr);
77		xwritec('-');
78		xwritec('>');
79		xwritehexl(*tprt++);
80		xwritecr();
81		addr += sizeof(long);
82	}
83
84	return rc;
85}
86
87int xinfdump(long pid, void *ptr, int len)
88{
89	int rc;
90	int i;
91	long wrdcnt = len / sizeof(long) + 1;
92	long *infwords = malloc(wrdcnt * sizeof(long));
93	long addr = (long)ptr;
94
95	addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
96
97	for (i = 0; i < wrdcnt; ++i) {
98		infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, addr);
99		addr += sizeof(long);
100	}
101
102	rc = xwritedump(infwords, (long)ptr, len);
103
104	free(infwords);
105	return rc;
106}
107