blob: 7ae2144ef52131515e50e9cb1fd27e0d00db344e [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
Juan Cespedes5c682042009-05-21 15:59:56 +020023/*
24 * The following section provides a way to print things, like hex dumps,
25 * with out using buffered output. This was written by Steve Munroe of IBM.
26 */
Ian Wienand9a2ad352006-02-20 22:44:45 +010027
28#include <stdio.h>
29#include <errno.h>
30#include <unistd.h>
31#include <stdlib.h>
32#include <sys/ptrace.h>
33
Juan Cespedesf1350522008-12-16 18:19:58 +010034static int
35xwritehexl(long i) {
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 Cespedesf1350522008-12-16 18:19:58 +010055static int
56xwritec(char c) {
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 Cespedesf1350522008-12-16 18:19:58 +010064static int
65xwritecr(void) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 return xwritec('\n');
Ian Wienand9a2ad352006-02-20 22:44:45 +010067}
68
Juan Cespedesf1350522008-12-16 18:19:58 +010069static int
70xwritedump(void *ptr, long addr, int len) {
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
Juan Cespedesf1350522008-12-16 18:19:58 +010087int
88xinfdump(long pid, void *ptr, int len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 int rc;
90 int i;
Juan Cespedese672ad12009-02-11 18:49:18 +010091 long wrdcnt;
92 long *infwords;
93 long addr;
94
95 wrdcnt = len / sizeof(long) + 1;
96 infwords = malloc(wrdcnt * sizeof(long));
97 if (!infwords) {
98 perror("ltrace: malloc");
99 exit(1);
100 }
101 addr = (long)ptr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100102
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100104
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 for (i = 0; i < wrdcnt; ++i) {
106 infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, addr);
107 addr += sizeof(long);
108 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100109
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 rc = xwritedump(infwords, (long)ptr, len);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100111
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 free(infwords);
113 return rc;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100114}