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_attrlist.c 3083 2014-09-02 22:08:01Z kaiwang27 $
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_attrlist and dwarf_whatattr etc.
41  */
42 static void tp_dwarf_attrlist(void);
43 static void tp_dwarf_attrlist_sanity(void);
44 static struct dwarf_tp dwarf_tp_array[] = {
45 	{"tp_dwarf_attrlist", tp_dwarf_attrlist},
46 	{"tp_dwarf_attrlist_sanity", tp_dwarf_attrlist_sanity},
47 	{NULL, NULL},
48 };
49 static int result = TET_UNRESOLVED;
50 #include "driver.c"
51 #include "die_traverse2.c"
52 
53 static void
_dwarf_attrlist(Dwarf_Die die)54 _dwarf_attrlist(Dwarf_Die die)
55 {
56 	Dwarf_Attribute *attrlist;
57 	Dwarf_Signed attrcount;
58 	Dwarf_Half attr;
59 	Dwarf_Error de;
60 	int i, r;
61 
62 	r = dwarf_attrlist(die, &attrlist, &attrcount, &de);
63 	TS_CHECK_INT(r);
64 	if (r == DW_DLV_ERROR) {
65 		tet_printf("dwarf_attrlist failed: %s\n", dwarf_errmsg(de));
66 		result = TET_FAIL;
67 		return;
68 	} else if (r == DW_DLV_NO_ENTRY)
69 		return;
70 
71 	TS_CHECK_INT(attrcount);
72 	for (i = 0; i < attrcount; i++) {
73 		if (dwarf_whatattr(attrlist[i], &attr, &de) != DW_DLV_OK) {
74 			tet_printf("dwarf_whatattr failed: %s\n",
75 			    dwarf_errmsg(de));
76 			result = TET_FAIL;
77 		}
78 		TS_CHECK_UINT(attr);
79 	}
80 }
81 
82 static void
tp_dwarf_attrlist(void)83 tp_dwarf_attrlist(void)
84 {
85 	Dwarf_Debug dbg;
86 	Dwarf_Error de;
87 	int fd;
88 
89 	result = TET_UNRESOLVED;
90 
91 	TS_DWARF_INIT(dbg, fd, de);
92 
93 	TS_DWARF_DIE_TRAVERSE2(dbg, 1, _dwarf_attrlist);
94 	TS_DWARF_DIE_TRAVERSE2(dbg, 0, _dwarf_attrlist);
95 
96 	if (result == TET_UNRESOLVED)
97 		result = TET_PASS;
98 
99 done:
100 	TS_DWARF_FINISH(dbg, de);
101 	TS_RESULT(result);
102 }
103 
104 static void
tp_dwarf_attrlist_sanity(void)105 tp_dwarf_attrlist_sanity(void)
106 {
107 	Dwarf_Debug dbg;
108 	Dwarf_Attribute *attrlist;
109 	Dwarf_Signed attrcount;
110 	Dwarf_Error de;
111 	int fd;
112 
113 	result = TET_UNRESOLVED;
114 
115 	TS_DWARF_INIT(dbg, fd, de);
116 
117 	if (dwarf_attrlist(NULL, &attrlist, &attrcount, &de) != DW_DLV_ERROR) {
118 		tet_infoline("dwarf_attrlist didn't return DW_DLV_ERROR"
119 		    " when called with NULL arguments");
120 		result = TET_FAIL;
121 		goto done;
122 	}
123 
124 	if (result == TET_UNRESOLVED)
125 		result = TET_PASS;
126 
127 done:
128 	TS_DWARF_FINISH(dbg, de);
129 	TS_RESULT(result);
130 }
131