1 /*-
2  * Copyright (c) 2010 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $Id: dwarf_child.c 2084 2011-10-27 04:48:12Z jkoshy $
27  */
28 
29 #include <assert.h>
30 #include <dwarf.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <libdwarf.h>
34 #include <string.h>
35 
36 #include "driver.h"
37 #include "tet_api.h"
38 
39 static void tp_dwarf_child_first(void);
40 static void tp_dwarf_child_sanity(void);
41 static struct dwarf_tp dwarf_tp_array[] = {
42 	{"tp_dwarf_child_first", tp_dwarf_child_first},
43 	{"tp_dwarf_child_sanity", tp_dwarf_child_sanity},
44 	{NULL, NULL},
45 };
46 #include "driver.c"
47 
48 static void
tp_dwarf_child_first(void)49 tp_dwarf_child_first(void)
50 {
51 	Dwarf_Debug dbg;
52 	Dwarf_Error de;
53 	Dwarf_Die die, die0;
54 	Dwarf_Unsigned cu_next_offset;
55 	int r, fd, result, die_cnt;
56 
57 	result = TET_UNRESOLVED;
58 
59 	TS_DWARF_INIT(dbg, fd, de);
60 
61 	tet_infoline("count the number of children of compilation unit DIE");
62 
63 	die_cnt = 0;
64 	TS_DWARF_CU_FOREACH(dbg, cu_next_offset, de) {
65 		r = dwarf_siblingof(dbg, NULL, &die, &de);
66 		if (r == DW_DLV_OK) {
67 			r = dwarf_child(die, &die0, &de);
68 			while (r == DW_DLV_OK) {
69 				if (die0 == NULL) {
70 					tet_infoline("dwarf_child or "
71 					    "dwarf_siblingof return "
72 					    "DW_DLV_OK while argument die0 "
73 					    "is not filled in");
74 					result = TET_FAIL;
75 					goto done;
76 				}
77 				die_cnt++;
78 				die = die0;
79 				r = dwarf_siblingof(dbg, die, &die0, &de);
80 			}
81 		}
82 		if (r == DW_DLV_ERROR) {
83 			tet_printf("dwarf_siblingof or dwarf_child failed:"
84 			    " %s\n", dwarf_errmsg(de));
85 			result = TET_FAIL;
86 			goto done;
87 		}
88 	}
89 
90 	TS_CHECK_INT(die_cnt);
91 
92 	if (result == TET_UNRESOLVED)
93 		result = TET_PASS;
94 
95 done:
96 	TS_DWARF_FINISH(dbg, de);
97 	TS_RESULT(result);
98 }
99 
100 static void
tp_dwarf_child_sanity(void)101 tp_dwarf_child_sanity(void)
102 {
103 	Dwarf_Debug dbg;
104 	Dwarf_Error de;
105 	Dwarf_Die die;
106 	Dwarf_Unsigned cu_next_offset;
107 	int fd, result;
108 
109 	result = TET_UNRESOLVED;
110 
111 	TS_DWARF_INIT(dbg, fd, de);
112 
113 	if (dwarf_child(NULL, &die, &de) != DW_DLV_ERROR ||
114 	    dwarf_child(NULL, NULL, &de) != DW_DLV_ERROR) {
115 		tet_infoline("dwarf_child didn't return DW_DLV_ERROR when"
116 		    " called with NULL arguments");
117 		result = TET_FAIL;
118 		goto done;
119 
120 	}
121 
122 	TS_DWARF_CU_FOREACH(dbg, cu_next_offset, de) {
123 		if (dwarf_child(NULL, &die, &de) != DW_DLV_ERROR ||
124 		    dwarf_child(NULL, NULL, &de) != DW_DLV_ERROR) {
125 			tet_infoline("dwarf_child didn't return DW_DLV_ERROR"
126 			    " when called with NULL arguments");
127 			result = TET_FAIL;
128 			goto done;
129 		}
130 	}
131 
132 	if (result == TET_UNRESOLVED)
133 		result = TET_PASS;
134 
135 done:
136 	TS_DWARF_FINISH(dbg, de);
137 	TS_RESULT(result);
138 }
139