| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdarg.h> |
| 3 | |
| 4 | #include "debug.h" |
| 5 | #include "options.h" |
| 6 | #include "output.h" |
| 7 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 8 | void |
| 9 | debug_(int level, const char *file, int line, const char *func, const char *fmt, ...) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 10 | char buf[1024]; |
| 11 | va_list args; |
| 12 | |
| 13 | if (opt_d < level) { |
| 14 | return; |
| 15 | } |
| 16 | va_start(args, fmt); |
| 17 | vsnprintf(buf, 1024, fmt, args); |
| 18 | va_end(args); |
| 19 | |
| 20 | output_line(NULL, "DEBUG: %s:%d: %s(): %s", file, line, func, buf); |
| 21 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame^] | 22 | |
| 23 | // The following section provides a way to print things, like hex dumps, |
| 24 | // with out using buffered output. This was written by Steve Munroe of IBM. |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <errno.h> |
| 28 | #include <unistd.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/ptrace.h> |
| 31 | |
| 32 | int xwrite(const char *string, size_t len) |
| 33 | { |
| 34 | int rc = 0; |
| 35 | rc = write (1, string, len); |
| 36 | return rc; |
| 37 | } |
| 38 | |
| 39 | int xwrites(const char *string) |
| 40 | { |
| 41 | size_t len = 0; |
| 42 | int rc = 0; |
| 43 | const char *tstring = string; |
| 44 | |
| 45 | while (*tstring++ != '\0') |
| 46 | { |
| 47 | len++; |
| 48 | } |
| 49 | |
| 50 | if (len >0) |
| 51 | { |
| 52 | rc = xwrite( string, len ); |
| 53 | } |
| 54 | |
| 55 | return rc; |
| 56 | } |
| 57 | |
| 58 | int xwritehexi(int i) |
| 59 | { |
| 60 | int rc = 0; |
| 61 | char text[9]; |
| 62 | int j; |
| 63 | unsigned int temp = (unsigned int)i; |
| 64 | |
| 65 | for ( j = 7; j >= 0; j--) |
| 66 | { |
| 67 | char c; |
| 68 | c = (char)((temp & 0x0f ) + '0'); |
| 69 | if ( c > '9' ) |
| 70 | { |
| 71 | c = (char)(c + ('a' - '9' - 1)); |
| 72 | } |
| 73 | text[j] = c; |
| 74 | temp = temp>>4; |
| 75 | } |
| 76 | |
| 77 | rc = write (1, text, 8); |
| 78 | return rc; |
| 79 | } |
| 80 | |
| 81 | int xwritehexl(long i) |
| 82 | { |
| 83 | int rc = 0; |
| 84 | char text[17]; |
| 85 | int j; |
| 86 | unsigned long temp = (unsigned long)i; |
| 87 | |
| 88 | for ( j = 15; j >= 0; j--) |
| 89 | { |
| 90 | char c; |
| 91 | c = (char)((temp & 0x0f ) + '0'); |
| 92 | if ( c > '9' ) |
| 93 | { |
| 94 | c = (char)(c + ('a' - '9' - 1)); |
| 95 | } |
| 96 | text[j] = c; |
| 97 | temp = temp>>4; |
| 98 | } |
| 99 | |
| 100 | rc = write (1, text, 16); |
| 101 | return rc; |
| 102 | } |
| 103 | |
| 104 | int xwritec(char c) |
| 105 | { |
| 106 | char temp = c; |
| 107 | char *text = &temp; |
| 108 | int rc = 0; |
| 109 | rc = write (1, text, 1); |
| 110 | return rc; |
| 111 | } |
| 112 | |
| 113 | int xwritecr(void) |
| 114 | { |
| 115 | return xwritec('\n'); |
| 116 | } |
| 117 | |
| 118 | int xwritedump(void *ptr, long addr, int len) |
| 119 | { |
| 120 | int rc = 0; |
| 121 | long *tprt = (long*)ptr; |
| 122 | int i; |
| 123 | |
| 124 | for ( i = 0; i < len ; i+=8 ) |
| 125 | { |
| 126 | xwritehexl( addr); |
| 127 | xwritec('-'); |
| 128 | xwritec('>'); |
| 129 | xwritehexl( *tprt++); |
| 130 | xwritecr(); |
| 131 | addr += sizeof(long); |
| 132 | } |
| 133 | |
| 134 | return rc; |
| 135 | } |
| 136 | |
| 137 | int xinfdump(long pid, void *ptr, int len) |
| 138 | { |
| 139 | int rc; |
| 140 | int i; |
| 141 | long wrdcnt = len / sizeof(long) + 1; |
| 142 | long *infwords = malloc(wrdcnt*sizeof(long)); |
| 143 | long addr = (long)ptr; |
| 144 | |
| 145 | addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long); |
| 146 | |
| 147 | for (i=0; i<wrdcnt; ++i) { |
| 148 | infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, addr); |
| 149 | addr += sizeof(long); |
| 150 | } |
| 151 | |
| 152 | rc = xwritedump(infwords, (long)ptr, len); |
| 153 | |
| 154 | free(infwords); |
| 155 | return rc; |
| 156 | } |