parameters.c revision b77f778eda200e0dc1c481e2f7dc5ff322147bd7
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, 3, 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  return 0;
201}
202