debug.c revision 91be87822a57bf4fb606333d5a7903dec9f56420
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2003,2008,2009 Juan Cespedes
4 * Copyright (C) 2006 Ian Wienand
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdarg.h>
24
25#include "common.h"
26#include "backend.h"
27
28void
29debug_(int level, const char *file, int line, const char *fmt, ...) {
30	char buf[1024];
31	va_list args;
32
33	if (!(options.debug & level)) {
34		return;
35	}
36	va_start(args, fmt);
37	vsnprintf(buf, 1024, fmt, args);
38	va_end(args);
39
40	output_line(NULL, "DEBUG: %s:%d: %s", file, line, buf);
41	fflush(options.output);
42}
43
44/*
45 * The following section provides a way to print things, like hex dumps,
46 * with out using buffered output.  This was written by Steve Munroe of IBM.
47 */
48
49#include <stdio.h>
50#include <errno.h>
51#include <unistd.h>
52#include <stdlib.h>
53#include <sys/ptrace.h>
54
55static int
56xwritehexl(long i) {
57	int rc = 0;
58	char text[17];
59	int j;
60	unsigned long temp = (unsigned long)i;
61
62	for (j = 15; j >= 0; j--) {
63		char c;
64		c = (char)((temp & 0x0f) + '0');
65		if (c > '9') {
66			c = (char)(c + ('a' - '9' - 1));
67		}
68		text[j] = c;
69		temp = temp >> 4;
70	}
71
72	rc = write(1, text, 16);
73	return rc;
74}
75
76static int
77xwritec(char c) {
78	char temp = c;
79	char *text = &temp;
80	int rc = 0;
81	rc = write(1, text, 1);
82	return rc;
83}
84
85static int
86xwritecr(void) {
87	return xwritec('\n');
88}
89
90static int
91xwritedump(void *ptr, long addr, int len) {
92	int rc = 0;
93	long *tprt = (long *)ptr;
94	int i;
95
96	for (i = 0; i < len; i += 8) {
97		xwritehexl(addr);
98		xwritec('-');
99		xwritec('>');
100		xwritehexl(*tprt++);
101		xwritecr();
102		addr += sizeof(long);
103	}
104
105	return rc;
106}
107
108int
109xinfdump(long pid, void *ptr, int len)
110{
111	unsigned char buf[len];
112	size_t got = umovebytes(pid2proc(pid), ptr, buf, len);
113	if (got == (size_t)-1)
114		return -1;
115	return xwritedump(buf, (long)ptr, got);
116}
117