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