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_attr.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 /*
40  * Test case for dwarf_attr, dwarf_hasattr and dwarf_whatattr etc.
41  */
42 static void tp_dwarf_attr(void);
43 static void tp_dwarf_attr_sanity(void);
44 static struct dwarf_tp dwarf_tp_array[] = {
45 	{"tp_dwarf_attr", tp_dwarf_attr},
46 	{"tp_dwarf_attr_sanity", tp_dwarf_attr_sanity},
47 	{NULL, NULL},
48 };
49 static int result = TET_UNRESOLVED;
50 #include "driver.c"
51 #include "die_traverse.c"
52 
53 static Dwarf_Half attr_array[] = { DW_AT_ordering,
54 				   DW_AT_bit_offset,
55 				   DW_AT_bit_size,
56 				   DW_AT_byte_size,
57 				   DW_AT_high_pc,
58 				   DW_AT_low_pc,
59 				   DW_AT_language,
60 				   DW_AT_name,
61 				   DW_AT_data_member_location,
62 				   DW_AT_producer,
63 				   DW_AT_comp_dir,
64 				   DW_AT_location,
65 				   DW_AT_decl_file,
66 				   DW_AT_decl_line };
67 static int attr_array_size = sizeof(attr_array) / sizeof(Dwarf_Half);
68 
69 static void
_dwarf_attr(Dwarf_Die die)70 _dwarf_attr(Dwarf_Die die)
71 {
72 	Dwarf_Attribute at;
73 	Dwarf_Bool has_attr;
74 	Dwarf_Half attr;
75 	Dwarf_Error de;
76 	const char *attr_name;
77 	int i, r;
78 
79 	for (i = 0; i < attr_array_size; i++) {
80 		if (dwarf_hasattr(die, attr_array[i], &has_attr, &de) !=
81 		    DW_DLV_OK) {
82 			tet_printf("dwarf_hasattr failed: %s\n",
83 			    dwarf_errmsg(de));
84 			result = TET_FAIL;
85 		}
86 		TS_CHECK_INT(has_attr);
87 
88 		if (has_attr) {
89 			if (dwarf_get_AT_name(attr_array[i], &attr_name) !=
90 			    DW_DLV_OK) {
91 				tet_printf("dwarf_get_AT_name failed: %s\n",
92 				    dwarf_errmsg(de));
93 				result = TET_FAIL;
94 				continue;
95 			}
96 			if (attr_name == NULL) {
97 				tet_infoline("dwarf_get_AT_name returned "
98 				    "DW_DLV_OK but didn't return string");
99 				result = TET_FAIL;
100 				continue;
101 			}
102 			TS_CHECK_STRING(attr_name);
103 
104 			tet_printf("DIE #%d has attribute '%s'\n", die_cnt,
105 			    attr_name);
106 
107 			r = dwarf_attr(die, attr_array[i], &at, &de);
108 			if (r == DW_DLV_ERROR) {
109 				tet_printf("dwarf_attr failed: %s",
110 				    dwarf_errmsg(de));
111 				result = TET_FAIL;
112 				continue;
113 			} else if (r == DW_DLV_NO_ENTRY) {
114 				tet_infoline("dwarf_hasattr returned true for "
115 				    "attribute '%s', while dwarf_attr returned"
116 				    " DW_DLV_NO_ENTRY for the same attr");
117 				result = TET_FAIL;
118 				continue;
119 			}
120 			if (dwarf_whatattr(at, &attr, &de) != DW_DLV_OK) {
121 				tet_printf("dwarf_whatattr failed: %s",
122 				    dwarf_errmsg(de));
123 				result = TET_FAIL;
124 				continue;
125 			}
126 			if (attr != attr_array[i]) {
127 				tet_infoline("attr returned by dwarf_whatattr"
128 				    " != attr_array[i]");
129 				result = TET_FAIL;
130 				continue;
131 			}
132 		}
133 	}
134 }
135 
136 static void
tp_dwarf_attr(void)137 tp_dwarf_attr(void)
138 {
139 	Dwarf_Debug dbg;
140 	Dwarf_Error de;
141 	int fd;
142 
143 	result = TET_UNRESOLVED;
144 
145 	TS_DWARF_INIT(dbg, fd, de);
146 
147 	TS_DWARF_DIE_TRAVERSE(dbg, _dwarf_attr);
148 
149 	if (result == TET_UNRESOLVED)
150 		result = TET_PASS;
151 
152 done:
153 	TS_DWARF_FINISH(dbg, de);
154 	TS_RESULT(result);
155 }
156 
157 static void
tp_dwarf_attr_sanity(void)158 tp_dwarf_attr_sanity(void)
159 {
160 	Dwarf_Debug dbg;
161 	Dwarf_Error de;
162 	Dwarf_Bool has_attr;
163 	Dwarf_Half attr;
164 	Dwarf_Attribute at;
165 	int fd;
166 
167 	result = TET_UNRESOLVED;
168 
169 	TS_DWARF_INIT(dbg, fd, de);
170 
171 	if (dwarf_hasattr(NULL, DW_AT_name, &has_attr, &de) != DW_DLV_ERROR) {
172 		tet_infoline("dwarf_hasattr didn't return DW_DLV_ERROR"
173 		    " when called with NULL arguments");
174 		result = TET_FAIL;
175 		goto done;
176 	}
177 
178 	if (dwarf_attr(NULL, DW_AT_name, &at, &de) != DW_DLV_ERROR) {
179 		tet_infoline("dwarf_attr didn't return DW_DLV_ERROR"
180 		    " when called with NULL arguments");
181 		result = TET_FAIL;
182 		goto done;
183 	}
184 
185 	if (dwarf_whatattr(NULL, &attr, &de) != DW_DLV_ERROR) {
186 		tet_infoline("dwarf_whatattr didn't return DW_DLV_ERROR"
187 		    " when called with NULL arguments");
188 		result = TET_FAIL;
189 		goto done;
190 	}
191 
192 	if (result == TET_UNRESOLVED)
193 		result = TET_PASS;
194 
195 done:
196 	TS_DWARF_FINISH(dbg, de);
197 	TS_RESULT(result);
198 }
199