blob: dde6e6d9800a0c3a14927afacb9ca0883f55e3bb [file] [log] [blame]
Juan Cespedes5e01f651998-03-08 22:31:44 +01001#include <stdio.h>
2#include <stdlib.h>
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +01003#include <limits.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01004
5#include "ltrace.h"
6#include "options.h"
7
8static int display_char(char what);
9static int display_string(enum tof type, struct process * proc, int arg_num);
10static int display_stringN(int arg2, enum tof type, struct process * proc, int arg_num);
11static int display_unknown(enum tof type, struct process * proc, int arg_num);
12
13int 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
57static 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");
64 case '\\': return fprintf(output, "\\");
65 default:
66 if ((what<32) || (what>126)) {
67 return fprintf(output, "\\%03o", what);
68 } else {
69 return fprintf(output, "%c", what);
70 }
71 }
72}
73
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +010074static int string_maxlength=INT_MAX;
75
76#define MIN(a,b) (((a)<(b)) ? (a) : (b))
77
Juan Cespedes5e01f651998-03-08 22:31:44 +010078static int display_string(enum tof type, struct process * proc, int arg_num)
79{
80 void * addr;
81 char * str1;
82 int i;
83 int len=0;
84
85 addr = (void *)gimme_arg(type, proc, arg_num);
86 if (!addr) {
87 return fprintf(output, "NULL");
88 }
89
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +010090 str1 = malloc(MIN(opt_s,string_maxlength)+3);
Juan Cespedes5e01f651998-03-08 22:31:44 +010091 if (!str1) {
92 return fprintf(output, "???");
93 }
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +010094 umovestr(proc, addr, MIN(opt_s,string_maxlength)+1, str1);
Juan Cespedes5e01f651998-03-08 22:31:44 +010095 len = fprintf(output, "\"");
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +010096 for(i=0; len<MIN(opt_s,string_maxlength)+1; i++) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010097 if (str1[i]) {
98 len += display_char(str1[i]);
99 } else {
100 break;
101 }
102 }
103 len += fprintf(output, "\"");
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +0100104 if (str1[i] && (opt_s <= string_maxlength)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100105 len += fprintf(output, "...");
106 }
107 free(str1);
108 return len;
109}
110
Juan Cespedes5e01f651998-03-08 22:31:44 +0100111static int display_stringN(int arg2, enum tof type, struct process * proc, int arg_num)
112{
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +0100113 int a;
114
115 string_maxlength=gimme_arg(type, proc, arg2-1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100116 a = display_string(type, proc, arg_num);
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +0100117 string_maxlength=INT_MAX;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100118 return a;
119}
120
121static int display_unknown(enum tof type, struct process * proc, int arg_num)
122{
123 long tmp;
124
125 tmp = gimme_arg(type, proc, arg_num);
126
127 if (tmp<1000000 && tmp>-1000000) {
128 return fprintf(output, "%ld", tmp);
129 } else {
130 return fprintf(output, "0x%08lx", tmp);
131 }
132}