| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 7 | #include <string.h> |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 8 | #include <limits.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 9 | |
| 10 | #include "ltrace.h" |
| 11 | #include "options.h" |
| 12 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 13 | static int display_char(int what); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 14 | static int display_string(enum tof type, struct process *proc, int arg_num); |
| 15 | static int display_stringN(int arg2, enum tof type, struct process *proc, |
| 16 | int arg_num); |
| 17 | static int display_unknown(enum tof type, struct process *proc, int arg_num); |
| 18 | static int display_format(enum tof type, struct process *proc, int arg_num); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 19 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 20 | int |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 21 | display_arg(enum tof type, struct process *proc, int arg_num, |
| 22 | const struct complete_arg_type *at) |
| 23 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 24 | int tmp; |
| 25 | long arg; |
| 26 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 27 | if (!at) |
| Richard Kettlewell | 5df6269 | 2006-02-16 01:10:00 +0100 | [diff] [blame] | 28 | return display_unknown(type, proc, arg_num); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 29 | switch (at->at) { |
| 30 | case ARGTYPE_VOID: |
| 31 | return 0; |
| 32 | case ARGTYPE_INT: |
| 33 | return fprintf(output, "%d", |
| 34 | (int)gimme_arg(type, proc, arg_num)); |
| 35 | case ARGTYPE_UINT: |
| 36 | return fprintf(output, "%u", |
| 37 | (unsigned)gimme_arg(type, proc, arg_num)); |
| 38 | case ARGTYPE_LONG: |
| 39 | return fprintf(output, "%ld", gimme_arg(type, proc, arg_num)); |
| 40 | case ARGTYPE_ULONG: |
| 41 | return fprintf(output, "%lu", |
| 42 | (unsigned long)gimme_arg(type, proc, arg_num)); |
| 43 | case ARGTYPE_OCTAL: |
| 44 | return fprintf(output, "0%o", |
| 45 | (unsigned)gimme_arg(type, proc, arg_num)); |
| 46 | case ARGTYPE_CHAR: |
| 47 | tmp = fprintf(output, "'"); |
| 48 | tmp += display_char((int)gimme_arg(type, proc, arg_num)); |
| 49 | tmp += fprintf(output, "'"); |
| 50 | return tmp; |
| 51 | case ARGTYPE_ADDR: |
| 52 | arg = gimme_arg(type, proc, arg_num); |
| 53 | if (!arg) { |
| 54 | return fprintf(output, "NULL"); |
| 55 | } else { |
| 56 | return fprintf(output, "%p", (void *)arg); |
| 57 | } |
| 58 | case ARGTYPE_FORMAT: |
| 59 | return display_format(type, proc, arg_num); |
| 60 | case ARGTYPE_STRING: |
| 61 | return display_string(type, proc, arg_num); |
| 62 | case ARGTYPE_STRINGN: |
| 63 | return display_stringN(at->argno, type, proc, arg_num); |
| 64 | case ARGTYPE_UNKNOWN: |
| 65 | default: |
| 66 | return display_unknown(type, proc, arg_num); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 67 | } |
| 68 | return fprintf(output, "?"); |
| 69 | } |
| 70 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 71 | static int display_char(int what) |
| 72 | { |
| 73 | switch (what) { |
| 74 | case -1: |
| 75 | return fprintf(output, "EOF"); |
| 76 | case '\r': |
| 77 | return fprintf(output, "\\r"); |
| 78 | case '\n': |
| 79 | return fprintf(output, "\\n"); |
| 80 | case '\t': |
| 81 | return fprintf(output, "\\t"); |
| 82 | case '\b': |
| 83 | return fprintf(output, "\\b"); |
| 84 | case '\\': |
| 85 | return fprintf(output, "\\\\"); |
| 86 | default: |
| 87 | if ((what < 32) || (what > 126)) { |
| 88 | return fprintf(output, "\\%03o", (unsigned char)what); |
| 89 | } else { |
| 90 | return fprintf(output, "%c", what); |
| 91 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 95 | static int string_maxlength = INT_MAX; |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 96 | |
| 97 | #define MIN(a,b) (((a)<(b)) ? (a) : (b)) |
| 98 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 99 | static int display_string(enum tof type, struct process *proc, int arg_num) |
| 100 | { |
| 101 | void *addr; |
| 102 | unsigned char *str1; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 103 | int i; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 104 | int len = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 105 | |
| 106 | addr = (void *)gimme_arg(type, proc, arg_num); |
| 107 | if (!addr) { |
| 108 | return fprintf(output, "NULL"); |
| 109 | } |
| 110 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 111 | str1 = malloc(MIN(opt_s, string_maxlength) + 3); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 112 | if (!str1) { |
| 113 | return fprintf(output, "???"); |
| 114 | } |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 115 | umovestr(proc, addr, MIN(opt_s, string_maxlength) + 1, str1); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 116 | len = fprintf(output, "\""); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 117 | for (i = 0; i < MIN(opt_s, string_maxlength); i++) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 118 | if (str1[i]) { |
| 119 | len += display_char(str1[i]); |
| 120 | } else { |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | len += fprintf(output, "\""); |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 125 | if (str1[i] && (opt_s <= string_maxlength)) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 126 | len += fprintf(output, "..."); |
| 127 | } |
| 128 | free(str1); |
| 129 | return len; |
| 130 | } |
| 131 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 132 | static int |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 133 | display_stringN(int arg2, enum tof type, struct process *proc, int arg_num) |
| 134 | { |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 135 | int a; |
| 136 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 137 | string_maxlength = gimme_arg(type, proc, arg2 - 1); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 138 | a = display_string(type, proc, arg_num); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 139 | string_maxlength = INT_MAX; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 140 | return a; |
| 141 | } |
| 142 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 143 | static int display_unknown(enum tof type, struct process *proc, int arg_num) |
| 144 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 145 | long tmp; |
| 146 | |
| 147 | tmp = gimme_arg(type, proc, arg_num); |
| 148 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 149 | if (tmp < 1000000 && tmp > -1000000) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 150 | return fprintf(output, "%ld", tmp); |
| 151 | } else { |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 152 | return fprintf(output, "%p", (void *)tmp); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 153 | } |
| 154 | } |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 155 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 156 | static int display_format(enum tof type, struct process *proc, int arg_num) |
| 157 | { |
| 158 | void *addr; |
| 159 | unsigned char *str1; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 160 | int i; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 161 | int len = 0; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 162 | |
| 163 | addr = (void *)gimme_arg(type, proc, arg_num); |
| 164 | if (!addr) { |
| 165 | return fprintf(output, "NULL"); |
| 166 | } |
| 167 | |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 168 | str1 = malloc(MIN(opt_s, string_maxlength) + 3); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 169 | if (!str1) { |
| 170 | return fprintf(output, "???"); |
| 171 | } |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 172 | umovestr(proc, addr, MIN(opt_s, string_maxlength) + 1, str1); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 173 | len = fprintf(output, "\""); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 174 | for (i = 0; len < MIN(opt_s, string_maxlength) + 1; i++) { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 175 | if (str1[i]) { |
| 176 | len += display_char(str1[i]); |
| 177 | } else { |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | len += fprintf(output, "\""); |
| 182 | if (str1[i] && (opt_s <= string_maxlength)) { |
| 183 | len += fprintf(output, "..."); |
| 184 | } |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 185 | for (i = 0; str1[i]; i++) { |
| 186 | if (str1[i] == '%') { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 187 | int is_long = 0; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 188 | while (1) { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 189 | unsigned char c = str1[++i]; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 190 | if (c == '%') { |
| 191 | break; |
| 192 | } else if (!c) { |
| 193 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 194 | } else if (strchr("lzZtj", c)) { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 195 | is_long++; |
| 196 | if (c == 'j') |
| 197 | is_long++; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 198 | if (is_long > 1 |
| 199 | && sizeof(long) < |
| 200 | sizeof(long long)) { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 201 | len += fprintf(output, ", ..."); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 202 | str1[i + 1] = '\0'; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 203 | break; |
| 204 | } |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 205 | } else if (c == 'd' || c == 'i') { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 206 | if (is_long) |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 207 | len += |
| 208 | fprintf(output, ", %d", |
| 209 | (int)gimme_arg(type, |
| 210 | proc, |
| 211 | ++arg_num)); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 212 | else |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 213 | len += |
| 214 | fprintf(output, ", %ld", |
| 215 | gimme_arg(type, |
| 216 | proc, |
| 217 | ++arg_num)); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 218 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 219 | } else if (c == 'u') { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 220 | if (is_long) |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 221 | len += |
| 222 | fprintf(output, ", %u", |
| 223 | (int)gimme_arg(type, |
| 224 | proc, |
| 225 | ++arg_num)); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 226 | else |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 227 | len += |
| 228 | fprintf(output, ", %lu", |
| 229 | gimme_arg(type, |
| 230 | proc, |
| 231 | ++arg_num)); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 232 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 233 | } else if (c == 'o') { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 234 | if (is_long) |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 235 | len += |
| 236 | fprintf(output, ", 0%o", |
| 237 | (int)gimme_arg(type, |
| 238 | proc, |
| 239 | ++arg_num)); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 240 | else |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 241 | len += |
| 242 | fprintf(output, ", 0%lo", |
| 243 | gimme_arg(type, |
| 244 | proc, |
| 245 | ++arg_num)); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 246 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 247 | } else if (c == 'x' || c == 'X') { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 248 | if (is_long) |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 249 | len += |
| 250 | fprintf(output, ", %#x", |
| 251 | (int)gimme_arg(type, |
| 252 | proc, |
| 253 | ++arg_num)); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 254 | else |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 255 | len += |
| 256 | fprintf(output, ", %#lx", |
| 257 | gimme_arg(type, |
| 258 | proc, |
| 259 | ++arg_num)); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 260 | break; |
| 261 | } else if (strchr("eEfFgGaACS", c) |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 262 | || (is_long |
| 263 | && (c == 'c' || c == 's'))) { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 264 | len += fprintf(output, ", ..."); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 265 | str1[i + 1] = '\0'; |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 266 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 267 | } else if (c == 'c') { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 268 | len += fprintf(output, ", '"); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 269 | len += display_char((int) |
| 270 | gimme_arg(type, |
| 271 | proc, |
| 272 | ++arg_num)); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 273 | len += fprintf(output, "'"); |
| 274 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 275 | } else if (c == 's') { |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 276 | len += fprintf(output, ", "); |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 277 | len += |
| 278 | display_string(type, proc, |
| 279 | ++arg_num); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 280 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 281 | } else if (c == 'p' || c == 'n') { |
| 282 | len += |
| 283 | fprintf(output, ", %p", |
| 284 | (void *)gimme_arg(type, |
| 285 | proc, |
| 286 | ++arg_num)); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 287 | break; |
| Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame^] | 288 | } else if (c == '*') { |
| 289 | len += |
| 290 | fprintf(output, ", %d", |
| 291 | (int)gimme_arg(type, proc, |
| 292 | ++arg_num)); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | free(str1); |
| 298 | return len; |
| 299 | } |