Home | History | Annotate | Download | only in ltrace.main
      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 (at) 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 
     15 void func_intptr(int *i);
     16 void func_intptr_ret(int *i);
     17 int func_strlen(char*);
     18 void func_strfixed(char*);
     19 void func_ppp(int***);
     20 void func_stringp(char**);
     21 void func_short(short, short);
     22 void func_ushort(unsigned short, unsigned short);
     23 float func_float(float, float);
     24 double func_double(double, double);
     25 void func_arrayi(int*, int);
     26 void func_arrayf(float*, int);
     27 void func_struct(void*);
     28 
     29 typedef enum {
     30   RED,
     31   GREEN,
     32   BLUE,
     33   CHARTREUSE,
     34   PETUNIA
     35 } color_t;
     36 void func_enum(color_t);
     37 void func_typedef(color_t);
     38 
     39 void func_work(char *x);
     40 void func_call(char *x, char *y, void (*cb)(char *));
     41 
     42 void
     43 call_func_work (char *x)
     44 {
     45 	func_work(x);
     46 }
     47 
     48 int
     49 main ()
     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 
     65   extern int func_arg0(char *);
     66   func_arg0(buf);
     67 
     68   printf("%s\n", buf);
     69 
     70   func_strfixed(buf);
     71   printf("%s\n", buf);
     72 
     73   x = 80;
     74   xP = &x;
     75   xPP = &xP;
     76   func_ppp(&xPP);
     77 
     78   s = (char*) malloc(100);
     79   strcpy(s, "Dude");
     80   func_stringp(&s);
     81 
     82   func_enum(BLUE);
     83 
     84   func_short(-8, -9);
     85   func_ushort(33, 34);
     86   float f = func_float(3.4, -3.4);
     87   double d = func_double(3.4, -3.4);
     88 
     89   func_typedef(BLUE);
     90 
     91   ai = (int*) calloc(sizeof(int), 8);
     92   for (x = 0; x < 8; x++)
     93     ai[x] = 10 + x;
     94   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