* gcc-interface/lang.opt (funsigned-char): New option.
[official-gcc.git] / libbacktrace / testlib.c
blob6dbef7c38f35cdbe7799aa1c21188295fdb16fbe
1 /* testlib.c -- test functions for libbacktrace library
2 Copyright (C) 2012-2018 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
7 met:
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
15 distribution.
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. */
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "filenames.h"
40 #include "backtrace.h"
42 #include "testlib.h"
44 /* The backtrace state. */
46 void *state;
48 /* The number of failures. */
50 int failures;
52 /* Return the base name in a path. */
54 const char *
55 base (const char *p)
57 const char *last;
58 const char *s;
60 last = NULL;
61 for (s = p; *s != '\0'; ++s)
63 if (IS_DIR_SEPARATOR (*s))
64 last = s + 1;
66 return last != NULL ? last : p;
69 /* Check an entry in a struct info array. */
71 void
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)
75 if (*failed)
76 return;
77 if (all[index].filename == NULL || all[index].function == NULL)
79 fprintf (stderr, "%s: [%d]: missing file name or function name\n",
80 name, index);
81 *failed = 1;
82 return;
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);
88 *failed = 1;
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);
94 *failed = 1;
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);
100 *failed = 1;
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;
111 struct info *p;
113 if (data->index >= data->max)
115 fprintf (stderr, "callback_one: callback called too many times\n");
116 data->failed = 1;
117 return 1;
120 p = &data->all[data->index];
121 if (filename == NULL)
122 p->filename = NULL;
123 else
125 p->filename = strdup (filename);
126 assert (p->filename != NULL);
128 p->lineno = lineno;
129 if (function == NULL)
130 p->function = NULL;
131 else
133 p->function = strdup (function);
134 assert (p->function != NULL);
136 ++data->index;
138 return 0;
141 /* An error callback passed to backtrace. */
143 void
144 error_callback_one (void *vdata, const char *msg, int errnum)
146 struct bdata *data = (struct bdata *) vdata;
148 fprintf (stderr, "%s", msg);
149 if (errnum > 0)
150 fprintf (stderr, ": %s", strerror (errnum));
151 fprintf (stderr, "\n");
152 data->failed = 1;
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");
165 data->failed = 1;
166 return 1;
169 data->addrs[data->index] = pc;
170 ++data->index;
172 return 0;
175 /* An error callback passed to backtrace_simple. */
177 void
178 error_callback_two (void *vdata, const char *msg, int errnum)
180 struct sdata *data = (struct sdata *) vdata;
182 fprintf (stderr, "%s", msg);
183 if (errnum > 0)
184 fprintf (stderr, ": %s", strerror (errnum));
185 fprintf (stderr, "\n");
186 data->failed = 1;
189 /* The backtrace_syminfo callback function. */
191 void
192 callback_three (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
193 const char *symname, uintptr_t symval,
194 uintptr_t symsize)
196 struct symdata *data = (struct symdata *) vdata;
198 if (symname == NULL)
199 data->name = NULL;
200 else
202 data->name = strdup (symname);
203 assert (data->name != NULL);
205 data->val = symval;
206 data->size = symsize;
209 /* The backtrace_syminfo error callback function. */
211 void
212 error_callback_three (void *vdata, const char *msg, int errnum)
214 struct symdata *data = (struct symdata *) vdata;
216 fprintf (stderr, "%s", msg);
217 if (errnum > 0)
218 fprintf (stderr, ": %s", strerror (errnum));
219 fprintf (stderr, "\n");
220 data->failed = 1;
223 /* The backtrace_create_state error callback function. */
225 void
226 error_callback_create (void *data ATTRIBUTE_UNUSED, const char *msg,
227 int errnum)
229 fprintf (stderr, "%s", msg);
230 if (errnum > 0)
231 fprintf (stderr, ": %s", strerror (errnum));
232 fprintf (stderr, "\n");
233 exit (EXIT_FAILURE);