Cleanup: remove private babeltrace.h
[babeltrace.git] / src / plugins / lttng-utils / debug-info / bin-info.h
1 #ifndef _BABELTRACE_BIN_INFO_H
2 #define _BABELTRACE_BIN_INFO_H
3
4 /*
5 * Babeltrace - Executable and Shared Object Debug Info Reader
6 *
7 * Copyright 2015 Antoine Busque <abusque@efficios.com>
8 *
9 * Author: Antoine Busque <abusque@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include <gelf.h>
33 #include <elfutils/libdw.h>
34 #include "common/macros.h"
35 #include "fd-cache/fd-cache.h"
36
37 #define DEFAULT_DEBUG_DIR "/usr/lib/debug"
38 #define DEBUG_SUBDIR ".debug"
39 #define BUILD_ID_SUBDIR ".build-id"
40 #define BUILD_ID_SUFFIX ".debug"
41 #define BUILD_ID_PREFIX_DIR_LEN 2
42
43 struct bin_info {
44 /* Base virtual memory address. */
45 uint64_t low_addr;
46 /* Upper bound of exec address space. */
47 uint64_t high_addr;
48 /* Size of exec address space. */
49 uint64_t memsz;
50 /* Paths to ELF and DWARF files. */
51 gchar *elf_path;
52 gchar *dwarf_path;
53 /* libelf and libdw objects representing the files. */
54 Elf *elf_file;
55 Dwarf *dwarf_info;
56 /* Optional build ID info. */
57 uint8_t *build_id;
58 size_t build_id_len;
59
60 /* Optional debug link info. */
61 gchar *dbg_link_filename;
62 uint32_t dbg_link_crc;
63 /* fd cache handles to ELF and DWARF files. */
64 struct bt_fd_cache_handle *elf_handle;
65 struct bt_fd_cache_handle *dwarf_handle;
66 /* Configuration. */
67 gchar *debug_info_dir;
68 /* Denotes whether the executable is position independent code. */
69 bool is_pic:1;
70 /* Denotes whether the build id in the trace matches to one on disk. */
71 bool file_build_id_matches:1;
72 /*
73 * Denotes whether the executable only has ELF symbols and no
74 * DWARF info.
75 */
76 bool is_elf_only:1;
77 /* Weak ref. Owned by the iterator. */
78 struct bt_fd_cache *fd_cache;
79 };
80
81 struct source_location {
82 uint64_t line_no;
83 gchar *filename;
84 };
85
86 /**
87 * Initializes the bin_info framework. Call this before calling
88 * anything else.
89 *
90 * @returns 0 on success, -1 on failure
91 */
92 BT_HIDDEN
93 int bin_info_init(void);
94
95 /**
96 * Instantiate a structure representing an ELF executable, possibly
97 * with DWARF info, located at the given path.
98 *
99 * @param path Path to the ELF file
100 * @param low_addr Base address of the executable
101 * @param memsz In-memory size of the executable
102 * @param is_pic Whether the executable is position independent
103 * code (PIC)
104 * @param debug_info_dir Directory containing debug info or NULL.
105 * @param target_prefix Path to the root file system of the target
106 * or NULL.
107 * @returns Pointer to the new bin_info on success,
108 * NULL on failure.
109 */
110 BT_HIDDEN
111 struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path,
112 uint64_t low_addr, uint64_t memsz, bool is_pic,
113 const char *debug_info_dir, const char *target_prefix);
114
115 /**
116 * Destroy the given bin_info instance
117 *
118 * @param bin bin_info instance to destroy
119 */
120 BT_HIDDEN
121 void bin_info_destroy(struct bin_info *bin);
122
123 /**
124 * Sets the build ID information for a given bin_info instance.
125 *
126 * @param bin The bin_info instance for which to set
127 * the build ID
128 * @param build_id Array of bytes containing the actual ID
129 * @param build_id_len Length in bytes of the build_id
130 * @returns 0 on success, -1 on failure
131 */
132 BT_HIDDEN
133 int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id,
134 size_t build_id_len);
135
136 /**
137 * Sets the debug link information for a given bin_info instance.
138 *
139 * @param bin The bin_info instance for which to set
140 * the debug link
141 * @param filename Name of the separate debug info file
142 * @param crc Checksum for the debug info file
143 * @returns 0 on success, -1 on failure
144 */
145 BT_HIDDEN
146 int bin_info_set_debug_link(struct bin_info *bin, const char *filename,
147 uint32_t crc);
148
149 /**
150 * Returns whether or not the given bin info \p bin contains the
151 * address \p addr.
152 *
153 * @param bin bin_info instance
154 * @param addr Address to lookup
155 * @returns 1 if \p bin contains \p addr, 0 if it does not,
156 * -1 on failure
157 */
158 static inline
159 int bin_info_has_address(struct bin_info *bin, uint64_t addr)
160 {
161 if (!bin) {
162 return -1;
163 }
164
165 return addr >= bin->low_addr && addr < bin->high_addr;
166 }
167
168 /**
169 * Get the name of the function containing a given address within an
170 * executable.
171 *
172 * If no DWARF info is available, the function falls back to ELF
173 * symbols and the "function name" is in fact the name of the closest
174 * symbol, followed by the offset between the symbol and the address.
175 *
176 * On success, if found, the out parameter `func_name` is set. The ownership
177 * of `func_name` is passed to the caller. On failure, `func_name` remains
178 * unchanged.
179 *
180 * @param bin bin_info instance for the executable containing
181 * the address
182 * @param addr Virtual memory address for which to find the
183 * function name
184 * @param func_name Out parameter, the function name.
185 * @returns 0 on success, -1 on failure
186 */
187 BT_HIDDEN
188 int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr,
189 char **func_name);
190
191 /**
192 * Get the source location (file name and line number) for a given
193 * address within an executable.
194 *
195 * If no DWARF info is available, the source location cannot be found
196 * and the function will return unsuccessfully.
197 *
198 * On success, if found, the out parameter `src_loc` is set. The ownership
199 * of `src_loc` is passed to the caller. On failure, `src_loc` remains
200 * unchanged.
201 *
202 * @param bin bin_info instance for the executable containing
203 * the address
204 * @param addr Virtual memory address for which to find the
205 * source location
206 * @param src_loc Out parameter, the source location
207 * @returns 0 on success, -1 on failure
208 */
209 BT_HIDDEN
210 int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr,
211 struct source_location **src_loc);
212 /**
213 * Get a string representing the location within the binary of a given
214 * address.
215 *
216 * In the case of a PIC binary, the location is relative (+0x1234).
217 * For a non-PIC binary, the location is absolute (@0x1234)
218 *
219 * On success, the out parameter `bin_loc` is set. The ownership is
220 * passed to the caller. On failure, `bin_loc` remains unchanged.
221 *
222 * @param bin bin_info instance for the executable containing
223 * the address
224 * @param addr Virtual memory address for which to find the
225 * binary location
226 * @param bin_loc Out parameter, the binary location
227 * @returns 0 on success, -1 on failure
228 */
229 BT_HIDDEN
230 int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc);
231
232 /**
233 * Destroy the given source_location instance
234 *
235 * @param src_loc source_location instance to destroy
236 */
237 BT_HIDDEN
238 void source_location_destroy(struct source_location *src_loc);
239
240 #endif /* _BABELTRACE_BIN_INFO_H */
This page took 0.033838 seconds and 4 git commands to generate.