blob: b3ce31e2df62f89ee76708aeded9cb14f1127a19 [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");
Juan Cespedes81690ef1998-03-13 19:31:29 +010064 case '\b': return fprintf(output, "\\b");
Juan Cespedes5e01f651998-03-08 22:31:44 +010065 case '\\': return fprintf(output, "\\");
66 default:
67 if ((what<32) || (what>126)) {
Juan Cespedes81690ef1998-03-13 19:31:29 +010068 return fprintf(output, "\\%03o", (unsigned char)what);
Juan Cespedes5e01f651998-03-08 22:31:44 +010069 } else {
70 return fprintf(output, "%c", what);
71 }
72 }
73}
74
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +010075static int string_maxlength=INT_MAX;
76
77#define MIN(a,b) (((a)<(b)) ? (a) : (b))
78
Juan Cespedes5e01f651998-03-08 22:31:44 +010079static 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 Cespedes2c4a8cb1998-03-11 23:33:18 +010091 str1 = malloc(MIN(opt_s,string_maxlength)+3);
Juan Cespedes5e01f651998-03-08 22:31:44 +010092 if (!str1) {
93 return fprintf(output, "???");
94 }
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +010095 umovestr(proc, addr, MIN(opt_s,string_maxlength)+1, str1);
Juan Cespedes5e01f651998-03-08 22:31:44 +010096 len = fprintf(output, "\"");
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +010097 for(i=0; len<MIN(opt_s,string_maxlength)+1; i++) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010098 if (str1[i]) {
99 len += display_char(str1[i]);
100 } else {
101 break;
102 }
103 }
104 len += fprintf(output, "\"");
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +0100105 if (str1[i] && (opt_s <= string_maxlength)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100106 len += fprintf(output, "...");
107 }
108 free(str1);
109 return len;
110}
111
Juan Cespedes5e01f651998-03-08 22:31:44 +0100112static int display_stringN(int arg2, enum tof type, struct process * proc, int arg_num)
113{
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +0100114 int a;
115
116 string_maxlength=gimme_arg(type, proc, arg2-1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100117 a = display_string(type, proc, arg_num);
Juan Cespedes2c4a8cb1998-03-11 23:33:18 +0100118 string_maxlength=INT_MAX;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100119 return a;
120}
121
122static 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}