parameters2.exp revision 0965420dfd1f4167609c4b3bbee5dc0277423897
1# This file is part of ltrace.
2# Copyright (C) 2012 Petr Machata, Red Hat Inc.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License as
6# published by the Free Software Foundation; either version 2 of the
7# License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17# 02110-1301 USA
18
19set trivial [ltraceCompile {} [ltraceSource c {
20    int main(void) {}
21}]]
22
23ltraceMatch1 [ltraceRun -L -F [ltraceSource conf {
24    typedef aa = int;
25    typedef aaa = int;
26    typedef bbb = struct(aa);
27}] -- $trivial] "error" == 0
28
29ltraceMatch1 [ltraceRun -L -F [ltraceSource conf {
30    typedef aa = int;
31    typedef aa = int;
32}] -- $trivial] "error" != 0
33
34ltraceMatch1 [ltraceRun -L -F [ltraceSource conf {
35    typedef aa = struct;
36    typedef aa = int;
37}] -- $trivial] "error" != 0
38
39ltraceMatch1 [ltraceRun -L -F [ltraceSource conf {
40    typedef aa = struct;
41    typedef aa = struct(int);
42    typedef aa = struct(int);
43}] -- $trivial] "error" != 0
44
45ltraceMatch1 [ltraceRun -L -F [ltraceSource conf {
46    typedef aa = struct;
47    typedef aa = struct();
48    typedef aa = struct();
49}] -- $trivial] "error" != 0
50
51ltraceMatch1 [ltraceRun -L -F [ltraceSource conf {
52    typedef aa = struct(int, struct;);
53}] -- $trivial] "error" != 0
54
55set libll [ltraceCompile libll.so [ltraceSource c {
56    struct xxx;
57    void ll(struct xxx *xxx) {}
58}]]
59
60set conf [ltraceSource conf {
61    typedef xxx = struct;
62    typedef xxx = struct(int, xxx*);
63    void ll(xxx*);
64}]
65
66ltraceMatch [ltraceRun -F $conf -e ll [ltraceCompile {} $libll [ltraceSource c {
67    struct xxx {
68	int i;
69	struct xxx *next;
70    };
71
72    void ll (struct xxx *xxx);
73    int main (int argc, char *argv[])
74    {
75	struct xxx a = { 1, 0 };
76	struct xxx b = { 2, &a };
77	struct xxx c = { 3, &b };
78	struct xxx d = { 4, &c };
79	ll (&d);
80
81	struct xxx e = { 1, 0 };
82	struct xxx f = { 2, &e };
83	e.next = &f;
84	ll (&f);
85
86	struct xxx g = { 1, &g };
87	ll (&g);
88
89	return 0;
90    }
91}]]] {
92    {{->ll\({ 4, { 3, { 2, { 1, nil } } } }\) *= <void>} == 1}
93    {{->ll\({ 2, { 1, recurse\^ } }\) *= <void>} == 1}
94    {{->ll\({ 1, recurse }\) *= <void>} == 1}
95}
96
97ltraceMatch1 [ltraceRun -F $conf -e ll -A 5 \
98-- [ltraceCompile ll $libll [ltraceSource c {
99    #include <stdlib.h>
100    struct ble {
101	int i;
102	struct ble *next;
103    };
104
105    void ll (struct ble *ble);
106    int main (int argc, char *argv[])
107    {
108	struct ble *b = NULL;
109	int i;
110	for (i = 0; i < 10; ++i) {
111	    struct ble *n = malloc(sizeof(*n));
112	    n->i = i;
113	    n->next = b;
114	    b = n;
115	}
116	ll (b);
117
118	return 0;
119    }
120}]]] {->ll\({ 9, { 8, { 7, { 6, { 5, \.\.\. } } } } }\) *= <void>} == 1
121
122proc ltraceParamTest {conf cdecl libcode maincode match {params ""}} {
123    set conffile [ltraceSource conf $conf]
124    set lib [ltraceCompile liblib.so [ltraceSource c [concat $cdecl $libcode]]]
125    set bin [ltraceCompile {} $lib \
126		 [ltraceSource c \
127		      [concat $cdecl "int main(void) {" $maincode "}"]]]
128
129    set command [concat "ltraceRun -F $conffile " $params "-- $bin"]
130    return [ltraceMatch [eval $command] $match]
131}
132
133# Test using lens in typedef.
134
135ltraceParamTest {
136    typedef hexptr = hex(uint*);
137    void fun(hexptr);
138} {
139    void fun(unsigned *arg);
140} {
141    void fun(unsigned *arg) {}
142} {
143    unsigned u = 0x123;
144    fun(&u);
145} {
146    {{fun\(0x123\) *= <void>} == 1}
147}
148
149# Test support for bitvec lens.
150
151ltraceParamTest {
152    void fun(bitvec(uint));
153    void fun2(bitvec(array(char, 32)*));
154} {
155    void fun(unsigned i);
156    void fun2(unsigned char *arr);
157} {
158    void fun(unsigned i) {}
159    void fun2(unsigned char *arr) {}
160} {
161    fun(0);
162    fun(0x123);
163    fun(0xfffffffe);
164    fun(0xffffffff);
165
166    unsigned char bytes[32] = {0x00};
167    bytes[1] = 0xff;
168    bytes[31] = 0x80;
169    fun2(bytes);
170} {
171    {{fun\(<>\) *= <void>} == 1}
172    {{fun\(<0-1,5,8>\) *= <void>} == 1}
173    {{fun\(~<0>\) *= <void>} == 1}
174    {{fun\(~<>\) *= <void>} == 1}
175    {{fun2\(<8-15,255>\) *= <void>} == 1}
176}
177
178# Test support for hex(float), hex(double).
179
180ltraceParamTest {
181    hex(float) hex_float(hex(float));
182    hex(double) hex_double(hex(double));
183} {
184    float hex_float(float f);
185    double hex_double(double d);
186} {
187    float hex_float(float f) { return f + 1; }
188    double hex_double(double d) { return d + 1; }
189} {
190    hex_float(1.5);
191    hex_double(1.5);
192} {
193    {{hex_float\(0x1.8p\+0\) *= 0x1.4p\+1} == 1}
194    {{hex_double\(0x1.8p\+0\) *= 0x1.4p\+1} == 1}
195}
196
197# Test that -x fun can find "fun" prototype even if "fun" is in a
198# library.
199
200ltraceParamTest {
201    void fun();
202} {
203    void libfun(void);
204} {
205    void fun(void) {}
206    void libfun(void) { fun(); }
207} {
208    libfun();
209} {
210    {{fun@.*\(\)} == 1}
211} {
212    -L -x fun
213}
214
215ltraceDone
216