1db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai/* Ltrace Test : parameters.c.
2db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai   Objectives  : Verify that Ltrace can handle all the different
3db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai   parameter types
4db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
5db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai   This file was written by Steve Fink <sphink@gmail.com>. */
6db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
7db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include <stdio.h>
8db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include <unistd.h>
9db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include <sys/syscall.h>
10db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include <sys/stat.h>
11db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include <errno.h>
12db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include <string.h>
13db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include <stdlib.h>
14db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
15db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_intptr(int *i);
16db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_intptr_ret(int *i);
17db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaiint func_strlen(char*);
18db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_strfixed(char*);
19db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_ppp(int***);
20db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_stringp(char**);
21db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_short(short, short);
22db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_ushort(unsigned short, unsigned short);
23db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaifloat func_float(float, float);
24db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaidouble func_double(double, double);
25db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_arrayi(int*, int);
26db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_arrayf(float*, int);
27db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_struct(void*);
28db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
29db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaitypedef enum {
30db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  RED,
31db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  GREEN,
32db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  BLUE,
33db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  CHARTREUSE,
34db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  PETUNIA
35e5dc60822e5938fea2ae892ccddb906641ba174emmentovai} color_t;
36e5dc60822e5938fea2ae892ccddb906641ba174emmentovaivoid func_enum(color_t);
37db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid func_typedef(color_t);
38db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
394e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.comvoid func_work(char *x);
404e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.comvoid func_call(char *x, char *y, void (*cb)(char *));
410e6f5c95d7b791c2a7d2c4056d9746f3fa1ff166mmentovai
42db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaivoid
43e5dc60822e5938fea2ae892ccddb906641ba174emmentovaicall_func_work (char *x)
44db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai{
45db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai	func_work(x);
46db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai}
47db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
48db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaiint
49db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovaimain ()
506162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.com{
516162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.com  int x = 17;
52db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  int *xP, **xPP;
53db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  char buf[200];
546162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.com  char *s;
55db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  int *ai;
56db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  float *af;
57db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
58db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_intptr(&x);
59db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
60db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_intptr_ret(&x);
61db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
62db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_string("zero\0xxxxxxxxxxxxxx");
63db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_strlen(buf);
64db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
65db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  extern int func_arg0(char *);
66db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_arg0(buf);
67db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
68db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  printf("%s\n", buf);
69db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
70db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_strfixed(buf);
71db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  printf("%s\n", buf);
72db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
73db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  x = 80;
74db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  xP = &x;
75db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  xPP = &xP;
76db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_ppp(&xPP);
77db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
78db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  s = (char*) malloc(100);
79db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  strcpy(s, "Dude");
80db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_stringp(&s);
81db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
82db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_enum(BLUE);
83db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
84db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_short(-8, -9);
85db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_ushort(33, 34);
86db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  float f = func_float(3.4, -3.4);
87db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  double d = func_double(3.4, -3.4);
88db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
89db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  func_typedef(BLUE);
90db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai
91db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  ai = (int*) calloc(sizeof(int), 8);
92e5dc60822e5938fea2ae892ccddb906641ba174emmentovai  for (x = 0; x < 8; x++)
93db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai    ai[x] = 10 + x;
94e5dc60822e5938fea2ae892ccddb906641ba174emmentovai  func_arrayi(ai, 8);
95  func_arrayi(ai, 2);
96
97  af = (float*) calloc(sizeof(float), 8);
98  for (x = 0; x < 8; x++)
99    af[x] = 10.1 + x;
100  func_arrayf(af, 8);
101  func_arrayf(af, 2);
102
103  {
104    struct {
105      int simple;
106      int alen;
107      int slen;
108      struct { int a; int b; }* array;
109      struct { int a; int b; } seq[3];
110      char* str;
111    } x;
112
113    x.simple = 89;
114
115    x.alen = 2;
116    x.array = malloc(800);
117    x.array[0].a = 1;
118    x.array[0].b = 10;
119    x.array[1].a = 3;
120    x.array[1].b = 30;
121
122    x.seq[0].a = 4;
123    x.seq[0].b = 40;
124    x.seq[1].a = 5;
125    x.seq[1].b = 50;
126    x.seq[2].a = 6;
127    x.seq[2].b = 60;
128
129    x.slen = 3;
130    x.str = "123junk";
131
132    func_struct(&x);
133  }
134
135  {
136    char x[10] = {};
137    char y[10] = {};
138    func_call(x, y, call_func_work);
139  }
140
141  struct S2 {
142    float f;
143    char a;
144    char b;
145  };
146  struct S3 {
147    char a[6];
148    float f;
149  };
150  struct S2 func_struct_2(int, struct S3 s3, double d);
151  func_struct_2(17, (struct S3){ "ABCDE", 0.25 }, 0.5);
152
153  struct S4 {
154    long a;
155    long b;
156    long c;
157    long d;
158  };
159  struct S4 func_struct_large(struct S4 a, struct S4 b);
160  func_struct_large((struct S4){ 1, 2, 3, 4 }, (struct S4){ 5, 6, 7, 8 });
161
162  struct S5 {
163    char a;
164    char b;
165    long c;
166    long d;
167  };
168  struct S5 func_struct_large2(struct S5 a, struct S5 b);
169  func_struct_large2((struct S5){ '0', '1', 3, 4 }, (struct S5){ '2', '3', 7, 8 });
170
171  struct S6 {
172    long a;
173    long b;
174    char c;
175    char d;
176  };
177  struct S6 func_struct_large3(struct S6 a, struct S6 b);
178  func_struct_large3((struct S6){ 3, 4, '0', '1' }, (struct S6){ 7, 8 ,'2', '3' });
179
180  void func_many_args(int a, int b, long c, double d, char e, int f, float g,
181		      char h, int i, double j, int k, double l, char m, int n,
182		      short o, int p, char q, float r, float s, double t,
183		      long u, float v, float w, float x, float y);
184  func_many_args(1, 2, 3, 4.0, '5', 6, 7.0,
185		 '8', 9, 10.0, 11, 12.0, 'A', 14,
186		 15, 16, 'B', 18.0, 19.0, 20.0,
187		 21, 22.0, 23.0, 24.0, 25.0);
188
189  void func_printf(char *format, ...);
190  func_printf("sotnuh %d %ld %g %c\n", 5, 6L, 1.5, 'X');
191  func_printf("sotnuh1 %d %ld %hd\n", 5, 6L, (short)7);
192  func_printf("sotnuh2 %s %10s %10s\n", "a string", "a trimmed string", "short");
193  func_printf("many_args"
194	 "%d %d %ld %g %c %d %g "
195	 "%c %d %g %d %g %c %d "
196	 "%hd %d %c %g %g %g "
197	 "%ld %g %g %g %g",
198	 1, 2, 3L, 4.0, '5', 6, 7.0,
199	 '8', 9, 10.0, 11, 12.0, 'A', 14,
200	 (short)15, 16, 'B', 18.0, 19.0, 20.0,
201	 21L, 22.0, 23.0, 24.0, 25.0);
202
203  func_printf("sotnuh3 %*s\n", 4, "a trimmed string");
204
205  void func_sprintf(char *str, char *format, ...);
206  func_sprintf(NULL, "test %d %d %d %d\n", 1, 2, 3, 4);
207
208  void func_lens(int, long, short, long);
209  func_lens(22, 23, 24, 25);
210
211  int func_bool(int a, int b);
212  func_bool(1, 10);
213  func_bool(2, 0);
214
215  void func_hide(int a, int b, int c, int d, int e, int f, int g, int h);
216  func_hide(1, 2, 3, 4, 5, 6, 7, 8);
217
218  struct func_hide_struct {
219	  int a; int b; int c; int d; int e; int f; int g; int h;
220  };
221  void func_hide_struct(struct func_hide_struct hs);
222  func_hide_struct((struct func_hide_struct){1, 2, 3, 4, 5, 6, 7, 8});
223
224  enum ab { A, B };
225  long *func_short_enums(short abs[]);
226  func_short_enums((short[]){ A, B, A, A });
227
228  long func_negative_enum(short a, unsigned short b, int c, unsigned d,
229                         long e, unsigned long f);
230  func_negative_enum(-1, -1, -1, -1, -1, -1);
231
232  void func_charp_string(char *p);
233  func_charp_string("null-terminated string");
234
235  struct struct_empty {};
236  struct struct_empty func_struct_empty(struct struct_empty e);
237  func_struct_empty((struct struct_empty) {});
238
239  struct struct_size1 { char a; };
240  struct struct_size1 func_struct_size1(struct struct_size1 e);
241  func_struct_size1((struct struct_size1){ '5' });
242
243  struct struct_size2 { short a; };
244  struct struct_size2 func_struct_size2(struct struct_size2 e);
245  func_struct_size2((struct struct_size2){ 5 });
246
247  struct struct_size4 { int a; };
248  struct struct_size4 func_struct_size4(struct struct_size4 e);
249  func_struct_size4((struct struct_size4){ 5 });
250
251  struct struct_size8 { int a; int b; };
252  struct struct_size8 func_struct_size8(struct struct_size8 e);
253  func_struct_size8((struct struct_size8){ 5, 6 });
254
255  return 0;
256}
257