| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 3 | #include <limits.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 4 | |
| 5 | #include "ltrace.h" |
| 6 | #include "options.h" |
| 7 | |
| 8 | static int display_char(char what); |
| 9 | static int display_string(enum tof type, struct process * proc, int arg_num); |
| 10 | static int display_stringN(int arg2, enum tof type, struct process * proc, int arg_num); |
| 11 | static int display_unknown(enum tof type, struct process * proc, int arg_num); |
| 12 | |
| 13 | int display_arg(enum tof type, struct process * proc, int arg_num, enum param_type rt) |
| 14 | { |
| 15 | int tmp; |
| 16 | long arg; |
| 17 | |
| 18 | switch(rt) { |
| 19 | case LT_PT_VOID: |
| 20 | return 0; |
| 21 | case LT_PT_INT: |
| 22 | return fprintf(output, "%d", (int)gimme_arg(type, proc, arg_num)); |
| 23 | case LT_PT_UINT: |
| 24 | return fprintf(output, "%u", (unsigned)gimme_arg(type, proc, arg_num)); |
| 25 | case LT_PT_OCTAL: |
| 26 | return fprintf(output, "0%o", (unsigned)gimme_arg(type, proc, arg_num)); |
| 27 | case LT_PT_CHAR: |
| 28 | tmp = fprintf(output, "'"); |
| 29 | tmp += display_char((int)gimme_arg(type, proc, arg_num)); |
| 30 | tmp += fprintf(output, "'"); |
| 31 | return tmp; |
| 32 | case LT_PT_ADDR: |
| 33 | arg = gimme_arg(type, proc, arg_num); |
| 34 | if (!arg) { |
| 35 | return fprintf(output, "NULL"); |
| 36 | } else { |
| 37 | return fprintf(output, "0x%08x", (unsigned)arg); |
| 38 | } |
| 39 | case LT_PT_FORMAT: |
| 40 | case LT_PT_STRING: |
| 41 | return display_string(type, proc, arg_num); |
| 42 | case LT_PT_STRING0: |
| 43 | return display_stringN(0, type, proc, arg_num); |
| 44 | case LT_PT_STRING1: |
| 45 | return display_stringN(1, type, proc, arg_num); |
| 46 | case LT_PT_STRING2: |
| 47 | return display_stringN(2, type, proc, arg_num); |
| 48 | case LT_PT_STRING3: |
| 49 | return display_stringN(3, type, proc, arg_num); |
| 50 | case LT_PT_UNKNOWN: |
| 51 | default: |
| 52 | return display_unknown(type, proc, arg_num); |
| 53 | } |
| 54 | return fprintf(output, "?"); |
| 55 | } |
| 56 | |
| 57 | static int display_char(char what) |
| 58 | { |
| 59 | switch(what) { |
| 60 | case -1: return fprintf(output, "EOF"); |
| 61 | case '\r': return fprintf(output, "\\r"); |
| 62 | case '\n': return fprintf(output, "\\n"); |
| 63 | case '\t': return fprintf(output, "\\t"); |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame^] | 64 | case '\b': return fprintf(output, "\\b"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 65 | case '\\': return fprintf(output, "\\"); |
| 66 | default: |
| 67 | if ((what<32) || (what>126)) { |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame^] | 68 | return fprintf(output, "\\%03o", (unsigned char)what); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 69 | } else { |
| 70 | return fprintf(output, "%c", what); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 75 | static int string_maxlength=INT_MAX; |
| 76 | |
| 77 | #define MIN(a,b) (((a)<(b)) ? (a) : (b)) |
| 78 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 79 | static int display_string(enum tof type, struct process * proc, int arg_num) |
| 80 | { |
| 81 | void * addr; |
| 82 | char * str1; |
| 83 | int i; |
| 84 | int len=0; |
| 85 | |
| 86 | addr = (void *)gimme_arg(type, proc, arg_num); |
| 87 | if (!addr) { |
| 88 | return fprintf(output, "NULL"); |
| 89 | } |
| 90 | |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 91 | str1 = malloc(MIN(opt_s,string_maxlength)+3); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 92 | if (!str1) { |
| 93 | return fprintf(output, "???"); |
| 94 | } |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 95 | umovestr(proc, addr, MIN(opt_s,string_maxlength)+1, str1); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 96 | len = fprintf(output, "\""); |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 97 | for(i=0; len<MIN(opt_s,string_maxlength)+1; i++) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 98 | if (str1[i]) { |
| 99 | len += display_char(str1[i]); |
| 100 | } else { |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | len += fprintf(output, "\""); |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 105 | if (str1[i] && (opt_s <= string_maxlength)) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 106 | len += fprintf(output, "..."); |
| 107 | } |
| 108 | free(str1); |
| 109 | return len; |
| 110 | } |
| 111 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 112 | static int display_stringN(int arg2, enum tof type, struct process * proc, int arg_num) |
| 113 | { |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 114 | int a; |
| 115 | |
| 116 | string_maxlength=gimme_arg(type, proc, arg2-1); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 117 | a = display_string(type, proc, arg_num); |
| Juan Cespedes | 2c4a8cb | 1998-03-11 23:33:18 +0100 | [diff] [blame] | 118 | string_maxlength=INT_MAX; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 119 | return a; |
| 120 | } |
| 121 | |
| 122 | static int display_unknown(enum tof type, struct process * proc, int arg_num) |
| 123 | { |
| 124 | long tmp; |
| 125 | |
| 126 | tmp = gimme_arg(type, proc, arg_num); |
| 127 | |
| 128 | if (tmp<1000000 && tmp>-1000000) { |
| 129 | return fprintf(output, "%ld", tmp); |
| 130 | } else { |
| 131 | return fprintf(output, "0x%08lx", tmp); |
| 132 | } |
| 133 | } |