]>
gcc.gnu.org Git - gcc.git/blob - libbacktrace/testlib.c
1 /* testlib.c -- test functions for libbacktrace library
2 Copyright (C) 2012-2022 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
38 #include "filenames.h"
40 #include "backtrace.h"
44 /* The backtrace state. */
48 /* The number of failures. */
52 /* Return the base name in a path. */
61 for (s
= p
; *s
!= '\0'; ++s
)
63 if (IS_DIR_SEPARATOR (*s
))
66 return last
!= NULL
? last
: p
;
69 /* Check an entry in a struct info array. */
72 check (const char *name
, int index
, const struct info
*all
, int want_lineno
,
73 const char *want_function
, const char *want_file
, int *failed
)
77 if (all
[index
].filename
== NULL
|| all
[index
].function
== NULL
)
79 fprintf (stderr
, "%s: [%d]: missing file name or function name\n",
84 if (strcmp (base (all
[index
].filename
), want_file
) != 0)
86 fprintf (stderr
, "%s: [%d]: got %s expected %s\n", name
, index
,
87 all
[index
].filename
, want_file
);
90 if (all
[index
].lineno
!= want_lineno
)
92 fprintf (stderr
, "%s: [%d]: got %d expected %d\n", name
, index
,
93 all
[index
].lineno
, want_lineno
);
96 if (strcmp (all
[index
].function
, want_function
) != 0)
98 fprintf (stderr
, "%s: [%d]: got %s expected %s\n", name
, index
,
99 all
[index
].function
, want_function
);
104 /* The backtrace callback function. */
107 callback_one (void *vdata
, uintptr_t pc ATTRIBUTE_UNUSED
,
108 const char *filename
, int lineno
, const char *function
)
110 struct bdata
*data
= (struct bdata
*) vdata
;
113 if (data
->index
>= data
->max
)
115 fprintf (stderr
, "callback_one: callback called too many times\n");
120 p
= &data
->all
[data
->index
];
121 if (filename
== NULL
)
125 p
->filename
= strdup (filename
);
126 assert (p
->filename
!= NULL
);
129 if (function
== NULL
)
133 p
->function
= strdup (function
);
134 assert (p
->function
!= NULL
);
141 /* An error callback passed to backtrace. */
144 error_callback_one (void *vdata
, const char *msg
, int errnum
)
146 struct bdata
*data
= (struct bdata
*) vdata
;
148 fprintf (stderr
, "%s", msg
);
150 fprintf (stderr
, ": %s", strerror (errnum
));
151 fprintf (stderr
, "\n");
155 /* The backtrace_simple callback function. */
158 callback_two (void *vdata
, uintptr_t pc
)
160 struct sdata
*data
= (struct sdata
*) vdata
;
162 if (data
->index
>= data
->max
)
164 fprintf (stderr
, "callback_two: callback called too many times\n");
169 data
->addrs
[data
->index
] = pc
;
175 /* An error callback passed to backtrace_simple. */
178 error_callback_two (void *vdata
, const char *msg
, int errnum
)
180 struct sdata
*data
= (struct sdata
*) vdata
;
182 fprintf (stderr
, "%s", msg
);
184 fprintf (stderr
, ": %s", strerror (errnum
));
185 fprintf (stderr
, "\n");
189 /* The backtrace_syminfo callback function. */
192 callback_three (void *vdata
, uintptr_t pc ATTRIBUTE_UNUSED
,
193 const char *symname
, uintptr_t symval
,
196 struct symdata
*data
= (struct symdata
*) vdata
;
202 data
->name
= strdup (symname
);
203 assert (data
->name
!= NULL
);
206 data
->size
= symsize
;
209 /* The backtrace_syminfo error callback function. */
212 error_callback_three (void *vdata
, const char *msg
, int errnum
)
214 struct symdata
*data
= (struct symdata
*) vdata
;
216 fprintf (stderr
, "%s", msg
);
218 fprintf (stderr
, ": %s", strerror (errnum
));
219 fprintf (stderr
, "\n");
223 /* The backtrace_create_state error callback function. */
226 error_callback_create (void *data ATTRIBUTE_UNUSED
, const char *msg
,
229 fprintf (stderr
, "%s", msg
);
231 fprintf (stderr
, ": %s", strerror (errnum
));
232 fprintf (stderr
, "\n");
This page took 0.083645 seconds and 5 git commands to generate.