Back to index

glibc  2.9
tst-backtrace1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <execinfo.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 
00024 #define BT_SIZE 64
00025 void *bt_array[BT_SIZE];
00026 int bt_cnt;
00027 
00028 int
00029 do_bt (void)
00030 {
00031   bt_cnt = backtrace (bt_array, BT_SIZE);
00032   return 56;
00033 }
00034 
00035 int
00036 call_do_bt (void)
00037 {
00038   return do_bt () + 1;
00039 }
00040 
00041 void *
00042 tf (void *arg)
00043 {
00044   if (call_do_bt () != 57)
00045     return (void *) 1L;
00046   return NULL;
00047 }
00048 
00049 int
00050 do_test (void)
00051 {
00052   pthread_t th;
00053   if (pthread_create (&th, NULL, tf, NULL))
00054     {
00055       puts ("create failed");
00056       return 1;
00057     }
00058 
00059   void *res;
00060   if (pthread_join (th, &res))
00061     {
00062       puts ("join failed");
00063       return 1;
00064     }
00065 
00066   if (res != NULL)
00067     {
00068       puts ("thread failed");
00069       return 1;
00070     }
00071 
00072   char **text = backtrace_symbols (bt_array, bt_cnt);
00073   if (text == NULL)
00074     {
00075       puts ("backtrace_symbols failed");
00076       return 1;
00077     }
00078 
00079   for (int i = 0; i < bt_cnt; ++i)
00080     puts (text[i]);
00081 
00082   return 0;
00083 }
00084 
00085 #define TEST_FUNCTION do_test ()
00086 #include "../test-skeleton.c"