Commit | Line | Data |
---|---|---|
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> | |
24 | #include <stdlib.h> | |
25 | #include <stdint.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
c283522b | 28 | #include <lttng-utils/debug-info/dwarf.h> |
33ba099b AB |
29 | #include "tap/tap.h" |
30 | ||
31 | #define NR_TESTS 15 | |
32 | ||
6a284733 MJ |
33 | #define SO_NAME "libhello_so" |
34 | #define DWARF_DIR_NAME "dwarf_full" | |
35 | ||
33ba099b AB |
36 | static |
37 | void test_bt_dwarf(const char *data_dir) | |
38 | { | |
39 | int fd, ret, tag; | |
6a284733 | 40 | char *path; |
33ba099b AB |
41 | char *die_name = NULL; |
42 | struct bt_dwarf_cu *cu = NULL; | |
43 | struct bt_dwarf_die *die = NULL; | |
44 | Dwarf *dwarf_info = NULL; | |
45 | ||
6a284733 MJ |
46 | path = g_build_filename(data_dir, DWARF_DIR_NAME, SO_NAME, NULL); |
47 | if (path == NULL) { | |
48 | exit(EXIT_FAILURE); | |
49 | } | |
33ba099b AB |
50 | |
51 | fd = open(path, O_RDONLY); | |
52 | ok(fd >= 0, "Open DWARF file %s", path); | |
8c4bb40b JG |
53 | if (fd < 0) { |
54 | exit(EXIT_FAILURE); | |
55 | } | |
33ba099b AB |
56 | dwarf_info = dwarf_begin(fd, DWARF_C_READ); |
57 | ok(dwarf_info != NULL, "dwarf_begin successful"); | |
58 | cu = bt_dwarf_cu_create(dwarf_info); | |
59 | ok(cu != NULL, "bt_dwarf_cu_create successful"); | |
60 | ret = bt_dwarf_cu_next(cu); | |
61 | ok(ret == 0, "bt_dwarf_cu_next successful"); | |
62 | die = bt_dwarf_die_create(cu); | |
63 | ok(die != NULL, "bt_dwarf_die_create successful"); | |
b1d10d85 JG |
64 | if (!die) { |
65 | exit(EXIT_FAILURE); | |
66 | } | |
33ba099b AB |
67 | /* |
68 | * Test bt_dwarf_die_next twice, as the code path is different | |
69 | * for DIEs at depth 0 (just created) and other depths. | |
70 | */ | |
71 | ret = bt_dwarf_die_next(die); | |
72 | ok(ret == 0, "bt_dwarf_die_next from root DIE successful"); | |
73 | ok(die->depth == 1, | |
74 | "bt_dwarf_die_next from root DIE - correct depth value"); | |
75 | ret = bt_dwarf_die_next(die); | |
76 | ok(ret == 0, | |
77 | "bt_dwarf_die_next from non-root DIE successful"); | |
78 | ok(die->depth == 1, | |
79 | "bt_dwarf_die_next from non-root DIE - correct depth value"); | |
80 | ||
81 | /* Reset DIE to test dwarf_child */ | |
82 | bt_dwarf_die_destroy(die); | |
83 | die = bt_dwarf_die_create(cu); | |
b1d10d85 JG |
84 | if (!die) { |
85 | diag("Failed to create bt_dwarf_die"); | |
86 | exit(EXIT_FAILURE); | |
87 | } | |
33ba099b AB |
88 | |
89 | ret = bt_dwarf_die_child(die); | |
90 | ok(ret == 0, "bt_dwarf_die_child successful"); | |
91 | ok(die->depth == 1, "bt_dwarf_die_child - correct depth value"); | |
92 | ||
93 | ret = bt_dwarf_die_get_tag(die, &tag); | |
94 | ok(ret == 0, "bt_dwarf_die_get_tag successful"); | |
95 | ok(tag == DW_TAG_typedef, "bt_dwarf_die_get_tag - correct tag value"); | |
96 | ret = bt_dwarf_die_get_name(die, &die_name); | |
97 | ok(ret == 0, "bt_dwarf_die_get_name successful"); | |
98 | ok(strcmp(die_name, "size_t") == 0, | |
99 | "bt_dwarf_die_get_name - correct name value"); | |
100 | ||
101 | bt_dwarf_die_destroy(die); | |
102 | bt_dwarf_cu_destroy(cu); | |
103 | dwarf_end(dwarf_info); | |
104 | free(die_name); | |
105 | close(fd); | |
6a284733 | 106 | g_free(path); |
33ba099b AB |
107 | } |
108 | ||
109 | int main(int argc, char **argv) | |
110 | { | |
111 | const char *data_dir; | |
112 | ||
113 | plan_tests(NR_TESTS); | |
114 | ||
115 | if (argc != 2) { | |
116 | return EXIT_FAILURE; | |
117 | } else { | |
118 | data_dir = argv[1]; | |
119 | } | |
120 | ||
121 | test_bt_dwarf(data_dir); | |
122 | ||
123 | return EXIT_SUCCESS; | |
124 | } |