Home | History | Annotate | Line # | Download | only in libbacktrace
      1      1.1  christos /* testlib.c -- test functions for libbacktrace library
      2  1.1.1.2  christos    Copyright (C) 2012-2024 Free Software Foundation, Inc.
      3      1.1  christos    Written by Ian Lance Taylor, Google.
      4      1.1  christos 
      5      1.1  christos Redistribution and use in source and binary forms, with or without
      6      1.1  christos modification, are permitted provided that the following conditions are
      7      1.1  christos met:
      8      1.1  christos 
      9      1.1  christos     (1) Redistributions of source code must retain the above copyright
     10      1.1  christos     notice, this list of conditions and the following disclaimer.
     11      1.1  christos 
     12      1.1  christos     (2) Redistributions in binary form must reproduce the above copyright
     13      1.1  christos     notice, this list of conditions and the following disclaimer in
     14      1.1  christos     the documentation and/or other materials provided with the
     15      1.1  christos     distribution.
     16      1.1  christos 
     17      1.1  christos     (3) The name of the author may not be used to
     18      1.1  christos     endorse or promote products derived from this software without
     19      1.1  christos     specific prior written permission.
     20      1.1  christos 
     21      1.1  christos THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1  christos IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23      1.1  christos WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24      1.1  christos DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     25      1.1  christos INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26      1.1  christos (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     27      1.1  christos SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1  christos HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     29      1.1  christos STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     30      1.1  christos IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31      1.1  christos POSSIBILITY OF SUCH DAMAGE.  */
     32      1.1  christos 
     33      1.1  christos #include <assert.h>
     34      1.1  christos #include <stdio.h>
     35      1.1  christos #include <stdlib.h>
     36      1.1  christos #include <string.h>
     37      1.1  christos 
     38      1.1  christos #include "filenames.h"
     39      1.1  christos 
     40      1.1  christos #include "backtrace.h"
     41      1.1  christos 
     42      1.1  christos #include "testlib.h"
     43      1.1  christos 
     44      1.1  christos /* The backtrace state.  */
     45      1.1  christos 
     46      1.1  christos void *state;
     47      1.1  christos 
     48      1.1  christos /* The number of failures.  */
     49      1.1  christos 
     50      1.1  christos int failures;
     51      1.1  christos 
     52      1.1  christos /* Return the base name in a path.  */
     53      1.1  christos 
     54      1.1  christos const char *
     55      1.1  christos base (const char *p)
     56      1.1  christos {
     57      1.1  christos   const char *last;
     58      1.1  christos   const char *s;
     59      1.1  christos 
     60      1.1  christos   last = NULL;
     61      1.1  christos   for (s = p; *s != '\0'; ++s)
     62      1.1  christos     {
     63      1.1  christos       if (IS_DIR_SEPARATOR (*s))
     64      1.1  christos 	last = s + 1;
     65      1.1  christos     }
     66      1.1  christos   return last != NULL ? last : p;
     67      1.1  christos }
     68      1.1  christos 
     69      1.1  christos /* Check an entry in a struct info array.  */
     70      1.1  christos 
     71      1.1  christos void
     72      1.1  christos check (const char *name, int index, const struct info *all, int want_lineno,
     73      1.1  christos        const char *want_function, const char *want_file, int *failed)
     74      1.1  christos {
     75      1.1  christos   if (*failed)
     76      1.1  christos     return;
     77      1.1  christos   if (all[index].filename == NULL || all[index].function == NULL)
     78      1.1  christos     {
     79      1.1  christos       fprintf (stderr, "%s: [%d]: missing file name or function name\n",
     80      1.1  christos 	       name, index);
     81      1.1  christos       *failed = 1;
     82      1.1  christos       return;
     83      1.1  christos     }
     84      1.1  christos   if (strcmp (base (all[index].filename), want_file) != 0)
     85      1.1  christos     {
     86      1.1  christos       fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
     87      1.1  christos 	       all[index].filename, want_file);
     88      1.1  christos       *failed = 1;
     89      1.1  christos     }
     90      1.1  christos   if (all[index].lineno != want_lineno)
     91      1.1  christos     {
     92      1.1  christos       fprintf (stderr, "%s: [%d]: got %d expected %d\n", name, index,
     93      1.1  christos 	       all[index].lineno, want_lineno);
     94      1.1  christos       *failed = 1;
     95      1.1  christos     }
     96      1.1  christos   if (strcmp (all[index].function, want_function) != 0)
     97      1.1  christos     {
     98      1.1  christos       fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
     99      1.1  christos 	       all[index].function, want_function);
    100      1.1  christos       *failed = 1;
    101      1.1  christos     }
    102      1.1  christos }
    103      1.1  christos 
    104      1.1  christos /* The backtrace callback function.  */
    105      1.1  christos 
    106      1.1  christos int
    107      1.1  christos callback_one (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
    108      1.1  christos 	      const char *filename, int lineno, const char *function)
    109      1.1  christos {
    110      1.1  christos   struct bdata *data = (struct bdata *) vdata;
    111      1.1  christos   struct info *p;
    112      1.1  christos 
    113      1.1  christos   if (data->index >= data->max)
    114      1.1  christos     {
    115      1.1  christos       fprintf (stderr, "callback_one: callback called too many times\n");
    116      1.1  christos       data->failed = 1;
    117      1.1  christos       return 1;
    118      1.1  christos     }
    119      1.1  christos 
    120      1.1  christos   p = &data->all[data->index];
    121      1.1  christos   if (filename == NULL)
    122      1.1  christos     p->filename = NULL;
    123      1.1  christos   else
    124      1.1  christos     {
    125      1.1  christos       p->filename = strdup (filename);
    126      1.1  christos       assert (p->filename != NULL);
    127      1.1  christos     }
    128      1.1  christos   p->lineno = lineno;
    129      1.1  christos   if (function == NULL)
    130      1.1  christos     p->function = NULL;
    131      1.1  christos   else
    132      1.1  christos     {
    133      1.1  christos       p->function = strdup (function);
    134      1.1  christos       assert (p->function != NULL);
    135      1.1  christos     }
    136      1.1  christos   ++data->index;
    137      1.1  christos 
    138      1.1  christos   return 0;
    139      1.1  christos }
    140      1.1  christos 
    141      1.1  christos /* An error callback passed to backtrace.  */
    142      1.1  christos 
    143      1.1  christos void
    144      1.1  christos error_callback_one (void *vdata, const char *msg, int errnum)
    145      1.1  christos {
    146      1.1  christos   struct bdata *data = (struct bdata *) vdata;
    147      1.1  christos 
    148      1.1  christos   fprintf (stderr, "%s", msg);
    149      1.1  christos   if (errnum > 0)
    150      1.1  christos     fprintf (stderr, ": %s", strerror (errnum));
    151      1.1  christos   fprintf (stderr, "\n");
    152      1.1  christos   data->failed = 1;
    153      1.1  christos }
    154      1.1  christos 
    155      1.1  christos /* The backtrace_simple callback function.  */
    156      1.1  christos 
    157      1.1  christos int
    158      1.1  christos callback_two (void *vdata, uintptr_t pc)
    159      1.1  christos {
    160      1.1  christos   struct sdata *data = (struct sdata *) vdata;
    161      1.1  christos 
    162      1.1  christos   if (data->index >= data->max)
    163      1.1  christos     {
    164      1.1  christos       fprintf (stderr, "callback_two: callback called too many times\n");
    165      1.1  christos       data->failed = 1;
    166      1.1  christos       return 1;
    167      1.1  christos     }
    168      1.1  christos 
    169      1.1  christos   data->addrs[data->index] = pc;
    170      1.1  christos   ++data->index;
    171      1.1  christos 
    172      1.1  christos   return 0;
    173      1.1  christos }
    174      1.1  christos 
    175      1.1  christos /* An error callback passed to backtrace_simple.  */
    176      1.1  christos 
    177      1.1  christos void
    178      1.1  christos error_callback_two (void *vdata, const char *msg, int errnum)
    179      1.1  christos {
    180      1.1  christos   struct sdata *data = (struct sdata *) vdata;
    181      1.1  christos 
    182      1.1  christos   fprintf (stderr, "%s", msg);
    183      1.1  christos   if (errnum > 0)
    184      1.1  christos     fprintf (stderr, ": %s", strerror (errnum));
    185      1.1  christos   fprintf (stderr, "\n");
    186      1.1  christos   data->failed = 1;
    187      1.1  christos }
    188      1.1  christos 
    189      1.1  christos /* The backtrace_syminfo callback function.  */
    190      1.1  christos 
    191      1.1  christos void
    192      1.1  christos callback_three (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
    193      1.1  christos 		const char *symname, uintptr_t symval,
    194      1.1  christos 		uintptr_t symsize)
    195      1.1  christos {
    196      1.1  christos   struct symdata *data = (struct symdata *) vdata;
    197      1.1  christos 
    198      1.1  christos   if (symname == NULL)
    199      1.1  christos     data->name = NULL;
    200      1.1  christos   else
    201      1.1  christos     {
    202      1.1  christos       data->name = strdup (symname);
    203      1.1  christos       assert (data->name != NULL);
    204      1.1  christos     }
    205      1.1  christos   data->val = symval;
    206      1.1  christos   data->size = symsize;
    207      1.1  christos }
    208      1.1  christos 
    209      1.1  christos /* The backtrace_syminfo error callback function.  */
    210      1.1  christos 
    211      1.1  christos void
    212      1.1  christos error_callback_three (void *vdata, const char *msg, int errnum)
    213      1.1  christos {
    214      1.1  christos   struct symdata *data = (struct symdata *) vdata;
    215      1.1  christos 
    216      1.1  christos   fprintf (stderr, "%s", msg);
    217      1.1  christos   if (errnum > 0)
    218      1.1  christos     fprintf (stderr, ": %s", strerror (errnum));
    219      1.1  christos   fprintf (stderr, "\n");
    220      1.1  christos   data->failed = 1;
    221      1.1  christos }
    222      1.1  christos 
    223      1.1  christos /* The backtrace_create_state error callback function.  */
    224      1.1  christos 
    225      1.1  christos void
    226      1.1  christos error_callback_create (void *data ATTRIBUTE_UNUSED, const char *msg,
    227      1.1  christos                        int errnum)
    228      1.1  christos {
    229      1.1  christos   fprintf (stderr, "%s", msg);
    230      1.1  christos   if (errnum > 0)
    231      1.1  christos     fprintf (stderr, ": %s", strerror (errnum));
    232      1.1  christos   fprintf (stderr, "\n");
    233      1.1  christos   exit (EXIT_FAILURE);
    234      1.1  christos }
    235