Home | History | Annotate | Line # | Download | only in libexecinfo
t_backtrace.c revision 1.1
      1 /*	$NetBSD: t_backtrace.c,v 1.1 2012/05/27 18:47:18 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_backtrace.c,v 1.1 2012/05/27 18:47:18 christos Exp $");
     33 
     34 #include <atf-c.h>
     35 #include <string.h>
     36 #include <stdlib.h>
     37 #include <execinfo.h>
     38 
     39 #ifndef __arraycount
     40 #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
     41 #endif
     42 
     43 static void __attribute__((__noinline__))
     44 myfunc3(size_t ncalls)
     45 {
     46 	static const char *top[] = { "myfunc", "atfu_backtrace_fmt_basic_body",
     47 	    "atf_tc_run", "atf_tp_main", "main", "___start" };
     48 	size_t j, nptrs;
     49 	void *buffer[ncalls + 10];
     50 	char **strings;
     51 
     52 	nptrs = backtrace(buffer, __arraycount(buffer));
     53 	ATF_CHECK_EQ(nptrs, ncalls + 8);
     54 
     55 	strings = backtrace_symbols_fmt(buffer, nptrs, "%n");
     56 
     57 	ATF_CHECK(strings != NULL);
     58 	ATF_CHECK_STREQ(strings[0], "myfunc3");
     59 	ATF_CHECK_STREQ(strings[1], "myfunc2");
     60 
     61 	for (j = 2; j < ncalls + 2; j++)
     62 		ATF_CHECK_STREQ(strings[j], "myfunc1");
     63 
     64 	for (size_t i = 0; j < nptrs; i++, j++)
     65 		ATF_CHECK_STREQ(strings[j], top[i]);
     66 
     67 	free(strings);
     68 }
     69 
     70 static void __attribute__((__noinline__))
     71 myfunc2(size_t ncalls)
     72 {
     73 	myfunc3(ncalls);
     74 }
     75 
     76 static void __attribute__((__noinline__))
     77 myfunc1(size_t origcalls, size_t ncalls)
     78 {
     79 	if (ncalls > 1)
     80 		myfunc1(origcalls, ncalls - 1);
     81 	else
     82 		myfunc2(origcalls);
     83 }
     84 
     85 static void __attribute__((__noinline__))
     86 myfunc(size_t ncalls)
     87 {
     88 	myfunc1(ncalls, ncalls);
     89 }
     90 
     91 ATF_TC(backtrace_fmt_basic);
     92 ATF_TC_HEAD(backtrace_fmt_basic, tc)
     93 {
     94         atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)");
     95 }
     96 
     97 ATF_TC_BODY(backtrace_fmt_basic, tc)
     98 {
     99 	myfunc(12);
    100 }
    101 
    102 ATF_TP_ADD_TCS(tp)
    103 {
    104 
    105 	ATF_TP_ADD_TC(tp, backtrace_fmt_basic);
    106 
    107 	return atf_no_error();
    108 }
    109