aboutsummaryrefslogtreecommitdiff
path: root/debug.c
blob: 1b031892909a42cad05adbbbbfca464c8f9bbb16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * This file is part of ltrace.
 * Copyright (C) 2003,2008,2009 Juan Cespedes
 * Copyright (C) 2006 Ian Wienand
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <stdio.h>
#include <stdarg.h>

#include "common.h"

void
debug_(int level, const char *file, int line, const char *fmt, ...) {
	char buf[1024];
	va_list args;

	if (!(options.debug & level)) {
		return;
	}
	va_start(args, fmt);
	vsnprintf(buf, 1024, fmt, args);
	va_end(args);

	output_line(NULL, "DEBUG: %s:%d: %s", file, line, buf);
	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();
		addr += sizeof(long);
	}

	return rc;
}

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;
}