Fix: src.ctf.fs: initialize the other_entry variable
[babeltrace.git] / tests / plugins / flt.lttng-utils.debug-info / test_dwarf.c
CommitLineData
33ba099b
AB
1/*
2 * test_dwarf.c
3 *
4 * Babeltrace bt_dwarf (DWARF utilities) tests
5 *
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Antoine Busque <abusque@efficios.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; under version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <fcntl.h>
91d81473 24#include <glib.h>
33ba099b
AB
25#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
28#include <unistd.h>
c283522b 29#include <lttng-utils/debug-info/dwarf.h>
33ba099b
AB
30#include "tap/tap.h"
31
8e65ce6d 32#define NR_TESTS 17
33ba099b 33
6a284733
MJ
34#define SO_NAME "libhello_so"
35#define DWARF_DIR_NAME "dwarf_full"
8e65ce6d 36#define ELF_DIR_NAME "elf_only"
6a284733 37
8e65ce6d
MJ
38/*
39 * Test that we fail on an ELF file without DWARF.
40 */
41static
42void test_bt_no_dwarf(const char *data_dir)
43{
44 int fd;
45 char *path;
46 Dwarf *dwarf_info = NULL;
47
48 path = g_build_filename(data_dir, ELF_DIR_NAME, SO_NAME, NULL);
5084732e 49 if (!path) {
8e65ce6d
MJ
50 diag("Failed to allocate memory for path");
51 exit(EXIT_FAILURE);
52 }
53
54 fd = open(path, O_RDONLY);
55 ok(fd >= 0, "Open ELF file %s", path);
56 if (fd < 0) {
57 skip(1, "dwarf_begin failed as expected");
58 } else {
59 dwarf_info = dwarf_begin(fd, DWARF_C_READ);
5084732e 60 ok(!dwarf_info, "dwarf_begin failed as expected");
8e65ce6d
MJ
61 }
62
5084732e 63 if (dwarf_info) {
8e65ce6d
MJ
64 dwarf_end(dwarf_info);
65 }
eda096d7
FD
66
67 if (fd >= 0) {
68 close(fd);
69 }
8e65ce6d
MJ
70 g_free(path);
71}
72
73/*
74 * Test with a proper ELF file with DWARF.
75 */
33ba099b
AB
76static
77void test_bt_dwarf(const char *data_dir)
78{
79 int fd, ret, tag;
6a284733 80 char *path;
33ba099b
AB
81 char *die_name = NULL;
82 struct bt_dwarf_cu *cu = NULL;
83 struct bt_dwarf_die *die = NULL;
84 Dwarf *dwarf_info = NULL;
85
6a284733 86 path = g_build_filename(data_dir, DWARF_DIR_NAME, SO_NAME, NULL);
5084732e 87 if (!path) {
8e65ce6d 88 diag("Failed to allocate memory for path");
6a284733
MJ
89 exit(EXIT_FAILURE);
90 }
33ba099b
AB
91
92 fd = open(path, O_RDONLY);
93 ok(fd >= 0, "Open DWARF file %s", path);
8c4bb40b
JG
94 if (fd < 0) {
95 exit(EXIT_FAILURE);
96 }
33ba099b 97 dwarf_info = dwarf_begin(fd, DWARF_C_READ);
5084732e 98 ok(dwarf_info, "dwarf_begin successful");
33ba099b 99 cu = bt_dwarf_cu_create(dwarf_info);
5084732e 100 ok(cu, "bt_dwarf_cu_create successful");
33ba099b
AB
101 ret = bt_dwarf_cu_next(cu);
102 ok(ret == 0, "bt_dwarf_cu_next successful");
103 die = bt_dwarf_die_create(cu);
5084732e 104 ok(die, "bt_dwarf_die_create successful");
b1d10d85
JG
105 if (!die) {
106 exit(EXIT_FAILURE);
107 }
33ba099b
AB
108 /*
109 * Test bt_dwarf_die_next twice, as the code path is different
110 * for DIEs at depth 0 (just created) and other depths.
111 */
112 ret = bt_dwarf_die_next(die);
113 ok(ret == 0, "bt_dwarf_die_next from root DIE successful");
114 ok(die->depth == 1,
115 "bt_dwarf_die_next from root DIE - correct depth value");
116 ret = bt_dwarf_die_next(die);
117 ok(ret == 0,
118 "bt_dwarf_die_next from non-root DIE successful");
119 ok(die->depth == 1,
120 "bt_dwarf_die_next from non-root DIE - correct depth value");
121
122 /* Reset DIE to test dwarf_child */
123 bt_dwarf_die_destroy(die);
124 die = bt_dwarf_die_create(cu);
b1d10d85
JG
125 if (!die) {
126 diag("Failed to create bt_dwarf_die");
127 exit(EXIT_FAILURE);
128 }
33ba099b
AB
129
130 ret = bt_dwarf_die_child(die);
131 ok(ret == 0, "bt_dwarf_die_child successful");
132 ok(die->depth == 1, "bt_dwarf_die_child - correct depth value");
133
134 ret = bt_dwarf_die_get_tag(die, &tag);
135 ok(ret == 0, "bt_dwarf_die_get_tag successful");
136 ok(tag == DW_TAG_typedef, "bt_dwarf_die_get_tag - correct tag value");
137 ret = bt_dwarf_die_get_name(die, &die_name);
138 ok(ret == 0, "bt_dwarf_die_get_name successful");
139 ok(strcmp(die_name, "size_t") == 0,
140 "bt_dwarf_die_get_name - correct name value");
141
142 bt_dwarf_die_destroy(die);
143 bt_dwarf_cu_destroy(cu);
144 dwarf_end(dwarf_info);
145 free(die_name);
146 close(fd);
6a284733 147 g_free(path);
33ba099b
AB
148}
149
150int main(int argc, char **argv)
151{
152 const char *data_dir;
153
154 plan_tests(NR_TESTS);
155
156 if (argc != 2) {
157 return EXIT_FAILURE;
158 } else {
159 data_dir = argv[1];
160 }
161
8e65ce6d 162 test_bt_no_dwarf(data_dir);
33ba099b
AB
163 test_bt_dwarf(data_dir);
164
165 return EXIT_SUCCESS;
166}
This page took 0.070365 seconds and 4 git commands to generate.