parameters.c revision 51d85d99f3ed74c01109b1040499c3a01f68361c
1/* Ltrace Test : parameters.c.
2   Objectives  : Verify that Ltrace can handle all the different
3   parameter types
4
5   This file was written by Steve Fink <sphink@gmail.com>. */
6
7#include <stdio.h>
8#include <unistd.h>
9#include <sys/syscall.h>
10#include <sys/stat.h>
11#include <errno.h>
12#include <string.h>
13#include <stdlib.h>
14
15void func_intptr(int *i);
16void func_intptr_ret(int *i);
17int func_strlen(char*);
18void func_strfixed(char*);
19void func_ppp(int***);
20void func_stringp(char**);
21void func_short(short, short);
22void func_ushort(unsigned short, unsigned short);
23float func_float(float, float);
24double func_double(double, double);
25void func_arrayi(int*, int);
26void func_arrayf(float*, int);
27void func_struct(void*);
28
29typedef enum {
30  RED,
31  GREEN,
32  BLUE,
33  CHARTREUSE,
34  PETUNIA
35} color_t;
36void func_enum(color_t);
37void func_typedef(color_t);
38
39void func_work(char *x);
40void func_call(char *x, char *y, void (*cb)(char *));
41
42void
43call_func_work (char *x)
44{
45	func_work(x);
46}
47
48int
49main ()
50{
51  int x = 17;
52  int *xP, **xPP;
53  char buf[200];
54  char *s;
55  int *ai;
56  float *af;
57
58  func_intptr(&x);
59
60  func_intptr_ret(&x);
61
62  func_string("zero\0xxxxxxxxxxxxxx");
63  func_strlen(buf);
64  printf("%s\n", buf);
65
66  func_strfixed(buf);
67  printf("%s\n", buf);
68
69  x = 80;
70  xP = &x;
71  xPP = &xP;
72  func_ppp(&xPP);
73
74  s = (char*) malloc(100);
75  strcpy(s, "Dude");
76  func_stringp(&s);
77
78  func_enum(BLUE);
79
80  func_short(-8, -9);
81  func_ushort(33, 34);
82  float f = func_float(3.4, -3.4);
83  double d = func_double(3.4, -3.4);
84
85  func_typedef(BLUE);
86
87  ai = (int*) calloc(sizeof(int), 8);
88  for (x = 0; x < 8; x++)
89    ai[x] = 10 + x;
90  func_arrayi(ai, 8);
91  func_arrayi(ai, 2);
92
93  af = (float*) calloc(sizeof(float), 8);
94  for (x = 0; x < 8; x++)
95    af[x] = 10.1 + x;
96  func_arrayf(af, 8);
97  func_arrayf(af, 2);
98
99  {
100    struct {
101      int simple;
102      int alen;
103      int slen;
104      struct { int a; int b; }* array;
105      struct { int a; int b; } seq[3];
106      char* str;
107    } x;
108
109    x.simple = 89;
110
111    x.alen = 2;
112    x.array = malloc(800);
113    x.array[0].a = 1;
114    x.array[0].b = 10;
115    x.array[1].a = 3;
116    x.array[1].b = 30;
117
118    x.seq[0].a = 4;
119    x.seq[0].b = 40;
120    x.seq[1].a = 5;
121    x.seq[1].b = 50;
122    x.seq[2].a = 6;
123    x.seq[2].b = 60;
124
125    x.slen = 3;
126    x.str = "123junk";
127
128    func_struct(&x);
129  }
130
131  {
132    char x[10] = {};
133    char y[10] = {};
134    func_call(x, y, call_func_work);
135  }
136
137  struct S2 {
138    float f;
139    char a;
140    char b;
141  };
142  struct S3 {
143    char a[6];
144    float f;
145  };
146  struct S2 func_struct_2(int, struct S3 s3, double d);
147  func_struct_2(17, (struct S3){ "ABCDE", 0.25 }, 0.5);
148
149  struct S4 {
150    long a;
151    long b;
152    long c;
153    long d;
154  };
155  struct S4 func_struct_large(struct S4 a, struct S4 b);
156  func_struct_large((struct S4){ 1, 2, 3, 4 }, (struct S4){ 5, 6, 7, 8 });
157
158  struct S5 {
159    char a;
160    char b;
161    long c;
162    long d;
163  };
164  struct S5 func_struct_large2(struct S5 a, struct S5 b);
165  func_struct_large2((struct S5){ '0', '1', 3, 4 }, (struct S5){ '2', '3', 7, 8 });
166
167  struct S6 {
168    long a;
169    long b;
170    char c;
171    char d;
172  };
173  struct S6 func_struct_large3(struct S6 a, struct S6 b);
174  func_struct_large3((struct S6){ 3, 4, '0', '1' }, (struct S6){ 7, 8 ,'2', '3' });
175
176  void func_many_args(int a, int b, long c, double d, char e, int f, float g,
177		      char h, int i, double j, int k, double l, char m, int n,
178		      short o, int p, char q, float r, float s, double t,
179		      long u, float v, float w, float x, float y);
180  func_many_args(1, 2, 3, 4.0, '5', 6, 7.0,
181		 '8', 9, 10.0, 11, 12.0, 'A', 14,
182		 15, 16, 'B', 18.0, 19.0, 20.0,
183		 21, 22.0, 23.0, 24.0, 25.0);
184
185  printf("sotnuh %d %ld %g %c\n", 5, 6L, 1.5, 'X');
186  printf("sotnuh1 %d %ld %hd\n", 5, 6L, (short)7);
187  printf("sotnuh2 %s %10s %10s\n", "a string", "a trimmed string", "short");
188  printf("many_args"
189	 "%d %d %ld %g %c %d %g "
190	 "%c %d %g %d %g %c %d "
191	 "%hd %d %c %g %g %g "
192	 "%ld %g %g %g %g",
193	 1, 2, 3L, 4.0, '5', 6, 7.0,
194	 '8', 9, 10.0, 11, 12.0, 'A', 14,
195	 (short)15, 16, 'B', 18.0, 19.0, 20.0,
196	 21L, 22.0, 23.0, 24.0, 25.0);
197
198  printf("sotnuh3 %*s\n", 4, "a trimmed string");
199
200  void func_lens(int, long, short, long);
201  func_lens(22, 23, 24, 25);
202
203  int func_bool(int a, int b);
204  func_bool(1, 10);
205  func_bool(2, 0);
206
207  void func_hide(int a, int b, int c, int d, int e, int f);
208  func_hide(1, 2, 3, 4, 5, 6);
209
210  enum ab { A, B };
211  long *func_short_enums(short abs[]);
212  func_short_enums((short[]){ A, B, A, A });
213
214  long func_negative_enum(short a, unsigned short b, int c, unsigned d,
215                         long e, unsigned long f);
216  func_negative_enum(-1, -1, -1, -1, -1, -1);
217
218  void func_charp_string(char *p);
219  func_charp_string("null-terminated string");
220
221  struct dbl_eqv1 { double d; };
222  struct dbl_eqv2 { struct dbl_eqv1 d; };
223  struct dbl_eqv3 { struct dbl_eqv2 d; };
224  struct dbl_eqv4 { struct dbl_eqv3 d; };
225
226  struct flt_eqv1 { float d; };
227  struct flt_eqv2 { struct flt_eqv1 d; };
228  struct flt_eqv3 { struct flt_eqv2 d; };
229  struct flt_eqv4 { struct flt_eqv3 d; };
230
231  struct dbl_eqv1 func_dbl_eqv(struct dbl_eqv1 a, struct dbl_eqv2 b,
232			       struct dbl_eqv3 c, struct dbl_eqv4 d);
233  func_dbl_eqv((struct dbl_eqv1){ 2.5 },
234	       (struct dbl_eqv2){ { 1.5 } },
235	       (struct dbl_eqv3){ { { 0.5 } } },
236	       (struct dbl_eqv4){ { { { -0.5 } } } });
237
238  struct flt_eqv1 func_flt_eqv(struct flt_eqv1 a, struct flt_eqv2 b,
239			       struct flt_eqv3 c, struct flt_eqv4 d);
240  func_flt_eqv((struct flt_eqv1){ 2.5 },
241	       (struct flt_eqv2){ { 1.5 } },
242	       (struct flt_eqv3){ { { 0.5 } } },
243	       (struct flt_eqv4){ { { { -0.5 } } } });
244
245  struct struct_empty {};
246  struct struct_empty func_struct_empty(struct struct_empty e);
247  func_struct_empty((struct struct_empty) {});
248
249  struct struct_size1 { char a; };
250  struct struct_size1 func_struct_size1(struct struct_size1 e);
251  func_struct_size1((struct struct_size1){ '5' });
252
253  struct struct_size2 { short a; };
254  struct struct_size2 func_struct_size2(struct struct_size2 e);
255  func_struct_size2((struct struct_size2){ 5 });
256
257  struct struct_size4 { int a; };
258  struct struct_size4 func_struct_size4(struct struct_size4 e);
259  func_struct_size4((struct struct_size4){ 5 });
260
261  struct struct_size8 { int a; int b; };
262  struct struct_size8 func_struct_size8(struct struct_size8 e);
263  func_struct_size8((struct struct_size8){ 5, 6 });
264
265  /* Test Itanium Homogeneous Floating-point Aggregates.   */
266
267  struct struct_hfa_f2 { float a; struct flt_eqv1 b; };
268  struct struct_hfa_f2 func_hfa_f2(struct struct_hfa_f2 e);
269  func_hfa_f2((struct struct_hfa_f2){ 1, { 2 } });
270
271  struct struct_hfa_f3 { float a; struct struct_hfa_f2 b; };
272  struct struct_hfa_f3 func_hfa_f3(struct struct_hfa_f3 e);
273  func_hfa_f3((struct struct_hfa_f3){ 3, { 1, { 2 } } });
274
275  struct struct_hfa_f4 { float a; struct struct_hfa_f3 b; };
276  struct struct_hfa_f4 func_hfa_f4(struct struct_hfa_f4 e);
277  func_hfa_f4((struct struct_hfa_f4){ 4, { 3, { 1, { 2 } } } });
278
279  struct struct_hfa_f5 { float a; struct struct_hfa_f4 b; };
280  struct struct_hfa_f5 func_hfa_f5(struct struct_hfa_f5 e);
281  func_hfa_f5((struct struct_hfa_f5){ 5, { 4, { 3, { 1, { 2 } } } } });
282
283  struct struct_hfa_f6 { float a; struct struct_hfa_f5 b; };
284  struct struct_hfa_f6 func_hfa_f6(struct struct_hfa_f6 e);
285  func_hfa_f6((struct struct_hfa_f6){ 6, { 5, { 4, { 3, { 1, { 2 } } } } } });
286
287  struct struct_hfa_f7 { float a; struct struct_hfa_f6 b; };
288  struct struct_hfa_f7 func_hfa_f7(struct struct_hfa_f7 e);
289  func_hfa_f7((struct struct_hfa_f7){ 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } });
290
291  struct struct_hfa_f8 { float a; struct struct_hfa_f7 b; };
292  struct struct_hfa_f8 func_hfa_f8(struct struct_hfa_f8 e);
293  func_hfa_f8((struct struct_hfa_f8){ 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } });
294
295  struct struct_hfa_f9 { float a; struct struct_hfa_f8 b; };
296  struct struct_hfa_f9 func_hfa_f9(struct struct_hfa_f9 e);
297  func_hfa_f9((struct struct_hfa_f9){ 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } });
298
299  struct struct_hfa_f10 { float a; struct struct_hfa_f9 b; };
300  struct struct_hfa_f10 func_hfa_f10(struct struct_hfa_f10 e);
301  func_hfa_f10((struct struct_hfa_f10){ 10, { 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } } });
302
303  struct struct_hfa_f11 { float a; struct struct_hfa_f10 b; };
304  struct struct_hfa_f11 func_hfa_f11(struct struct_hfa_f11 e);
305  func_hfa_f11((struct struct_hfa_f11){ 11, { 10, { 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } } } });
306
307  struct struct_hfa_f12 { float a; struct struct_hfa_f11 b; };
308  struct struct_hfa_f12 func_hfa_f12(struct struct_hfa_f12 e);
309  func_hfa_f12((struct struct_hfa_f12){ 12, { 11, { 10, { 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } } } } });
310
311
312  struct struct_hfa_d2 { double a; struct dbl_eqv1 b; };
313  struct struct_hfa_d2 func_hfa_d2(struct struct_hfa_d2 e);
314  func_hfa_d2((struct struct_hfa_d2){ 1, { 2 } });
315
316  struct struct_hfa_d3 { double a; struct struct_hfa_d2 b; };
317  struct struct_hfa_d3 func_hfa_d3(struct struct_hfa_d3 e);
318  func_hfa_d3((struct struct_hfa_d3){ 3, { 1, { 2 } } });
319
320  struct struct_hfa_d4 { double a; struct struct_hfa_d3 b; };
321  struct struct_hfa_d4 func_hfa_d4(struct struct_hfa_d4 e);
322  func_hfa_d4((struct struct_hfa_d4){ 4, { 3, { 1, { 2 } } } });
323
324  struct struct_hfa_d5 { double a; struct struct_hfa_d4 b; };
325  struct struct_hfa_d5 func_hfa_d5(struct struct_hfa_d5 e);
326  func_hfa_d5((struct struct_hfa_d5){ 5, { 4, { 3, { 1, { 2 } } } } });
327
328  struct struct_hfa_d6 { double a; struct struct_hfa_d5 b; };
329  struct struct_hfa_d6 func_hfa_d6(struct struct_hfa_d6 e);
330  func_hfa_d6((struct struct_hfa_d6){ 6, { 5, { 4, { 3, { 1, { 2 } } } } } });
331
332  struct struct_hfa_d7 { double a; struct struct_hfa_d6 b; };
333  struct struct_hfa_d7 func_hfa_d7(struct struct_hfa_d7 e);
334  func_hfa_d7((struct struct_hfa_d7){ 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } });
335
336  struct struct_hfa_d8 { double a; struct struct_hfa_d7 b; };
337  struct struct_hfa_d8 func_hfa_d8(struct struct_hfa_d8 e);
338  func_hfa_d8((struct struct_hfa_d8){ 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } });
339
340  struct struct_hfa_d9 { double a; struct struct_hfa_d8 b; };
341  struct struct_hfa_d9 func_hfa_d9(struct struct_hfa_d9 e);
342  func_hfa_d9((struct struct_hfa_d9){ 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } });
343
344  struct struct_hfa_d10 { double a; struct struct_hfa_d9 b; };
345  struct struct_hfa_d10 func_hfa_d10(struct struct_hfa_d10 e);
346  func_hfa_d10((struct struct_hfa_d10){ 10, { 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } } });
347
348  struct struct_hfa_d11 { double a; struct struct_hfa_d10 b; };
349  struct struct_hfa_d11 func_hfa_d11(struct struct_hfa_d11 e);
350  func_hfa_d11((struct struct_hfa_d11){ 11, { 10, { 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } } } });
351
352  struct struct_hfa_d12 { double a; struct struct_hfa_d11 b; };
353  struct struct_hfa_d12 func_hfa_d12(struct struct_hfa_d12 e);
354  func_hfa_d12((struct struct_hfa_d12){ 12, { 11, { 10, { 9, { 8, { 7, { 6, { 5, { 4, { 3, { 1, { 2 } } } } } } } } } } } });
355
356  return 0;
357}
358