flt.lttng-utils.debug-info: Implement file descriptor cache
[babeltrace.git] / plugins / lttng-utils / debug-info / bin-info.h
CommitLineData
d5ddf820
AB
1#ifndef _BABELTRACE_BIN_INFO_H
2#define _BABELTRACE_BIN_INFO_H
c40a57e5
AB
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 <babeltrace/babeltrace-internal.h>
1e638f98 35#include <babeltrace/fd-cache-internal.h>
c40a57e5
AB
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
d5ddf820 42struct bin_info {
c40a57e5
AB
43 /* Base virtual memory address. */
44 uint64_t low_addr;
45 /* Upper bound of exec address space. */
46 uint64_t high_addr;
47 /* Size of exec address space. */
48 uint64_t memsz;
49 /* Paths to ELF and DWARF files. */
06d1cf5d
FD
50 gchar *elf_path;
51 gchar *dwarf_path;
c40a57e5
AB
52 /* libelf and libdw objects representing the files. */
53 Elf *elf_file;
54 Dwarf *dwarf_info;
55 /* Optional build ID info. */
56 uint8_t *build_id;
57 size_t build_id_len;
ca9f27f3 58
c40a57e5 59 /* Optional debug link info. */
06d1cf5d 60 gchar *dbg_link_filename;
c40a57e5 61 uint32_t dbg_link_crc;
1e638f98
FD
62 /* fd cache handles to ELF and DWARF files. */
63 struct bt_fd_cache_handle *elf_handle;
64 struct bt_fd_cache_handle *dwarf_handle;
9d325e17 65 /* Configuration. */
06d1cf5d 66 gchar *debug_info_dir;
c40a57e5
AB
67 /* Denotes whether the executable is position independent code. */
68 bool is_pic:1;
ca9f27f3
FD
69 /* denotes whether the build id in the trace matches to one on disk. */
70 bool file_build_id_matches:1;
d5ddf820
AB
71 /*
72 * Denotes whether the executable only has ELF symbols and no
73 * DWARF info.
74 */
c40a57e5 75 bool is_elf_only:1;
1e638f98
FD
76 /* Weak ref. Owned by the iterator. */
77 struct bt_fd_cache *fd_cache;
c40a57e5
AB
78};
79
80struct source_location {
81 uint64_t line_no;
06d1cf5d 82 gchar *filename;
c40a57e5
AB
83};
84
85/**
d5ddf820 86 * Initializes the bin_info framework. Call this before calling
c40a57e5
AB
87 * anything else.
88 *
89 * @returns 0 on success, -1 on failure
90 */
91BT_HIDDEN
d5ddf820 92int bin_info_init(void);
c40a57e5
AB
93
94/**
95 * Instantiate a structure representing an ELF executable, possibly
96 * with DWARF info, located at the given path.
97 *
98 * @param path Path to the ELF file
99 * @param low_addr Base address of the executable
100 * @param memsz In-memory size of the executable
9f2b13ca
AB
101 * @param is_pic Whether the executable is position independent
102 * code (PIC)
9d325e17
PP
103 * @param debug_info_dir Directory containing debug info or NULL.
104 * @param target_prefix Path to the root file system of the target
105 * or NULL.
d5ddf820 106 * @returns Pointer to the new bin_info on success,
c40a57e5
AB
107 * NULL on failure.
108 */
109BT_HIDDEN
1e638f98
FD
110struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path,
111 uint64_t low_addr, uint64_t memsz, bool is_pic,
112 const char *debug_info_dir, const char *target_prefix);
c40a57e5
AB
113
114/**
d5ddf820 115 * Destroy the given bin_info instance
c40a57e5 116 *
d5ddf820 117 * @param bin bin_info instance to destroy
c40a57e5
AB
118 */
119BT_HIDDEN
d5ddf820 120void bin_info_destroy(struct bin_info *bin);
c40a57e5
AB
121
122/**
d5ddf820 123 * Sets the build ID information for a given bin_info instance.
c40a57e5 124 *
d5ddf820 125 * @param bin The bin_info instance for which to set
c40a57e5
AB
126 * the build ID
127 * @param build_id Array of bytes containing the actual ID
128 * @param build_id_len Length in bytes of the build_id
129 * @returns 0 on success, -1 on failure
130 */
131BT_HIDDEN
d5ddf820 132int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id,
c40a57e5
AB
133 size_t build_id_len);
134
135/**
d5ddf820 136 * Sets the debug link information for a given bin_info instance.
c40a57e5 137 *
d5ddf820 138 * @param bin The bin_info instance for which to set
c40a57e5
AB
139 * the debug link
140 * @param filename Name of the separate debug info file
141 * @param crc Checksum for the debug info file
142 * @returns 0 on success, -1 on failure
143 */
144BT_HIDDEN
4f45f9bb
JD
145int bin_info_set_debug_link(struct bin_info *bin, const char *filename,
146 uint32_t crc);
c40a57e5
AB
147
148/**
d5ddf820
AB
149 * Returns whether or not the given bin info \p bin contains the
150 * address \p addr.
c40a57e5 151 *
d5ddf820 152 * @param bin bin_info instance
c40a57e5 153 * @param addr Address to lookup
d5ddf820 154 * @returns 1 if \p bin contains \p addr, 0 if it does not,
c40a57e5
AB
155 * -1 on failure
156 */
157static inline
d5ddf820 158int bin_info_has_address(struct bin_info *bin, uint64_t addr)
c40a57e5 159{
d5ddf820 160 if (!bin) {
c40a57e5
AB
161 return -1;
162 }
163
d5ddf820 164 return addr >= bin->low_addr && addr < bin->high_addr;
c40a57e5
AB
165}
166
167/**
168 * Get the name of the function containing a given address within an
169 * executable.
170 *
171 * If no DWARF info is available, the function falls back to ELF
172 * symbols and the "function name" is in fact the name of the closest
173 * symbol, followed by the offset between the symbol and the address.
174 *
175 * On success, if found, the out parameter `func_name` is set. The ownership
176 * of `func_name` is passed to the caller. On failure, `func_name` remains
177 * unchanged.
178 *
d5ddf820 179 * @param bin bin_info instance for the executable containing
c40a57e5
AB
180 * the address
181 * @param addr Virtual memory address for which to find the
182 * function name
183 * @param func_name Out parameter, the function name.
184 * @returns 0 on success, -1 on failure
185 */
186BT_HIDDEN
d5ddf820 187int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr,
c40a57e5
AB
188 char **func_name);
189
190/**
191 * Get the source location (file name and line number) for a given
192 * address within an executable.
193 *
194 * If no DWARF info is available, the source location cannot be found
4caab45b 195 * and the function will return unsuccessfully.
c40a57e5
AB
196 *
197 * On success, if found, the out parameter `src_loc` is set. The ownership
198 * of `src_loc` is passed to the caller. On failure, `src_loc` remains
199 * unchanged.
200 *
d5ddf820 201 * @param bin bin_info instance for the executable containing
c40a57e5
AB
202 * the address
203 * @param addr Virtual memory address for which to find the
204 * source location
205 * @param src_loc Out parameter, the source location
206 * @returns 0 on success, -1 on failure
207 */
208BT_HIDDEN
d5ddf820 209int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr,
c40a57e5 210 struct source_location **src_loc);
36ae9941
AB
211/**
212 * Get a string representing the location within the binary of a given
213 * address.
214 *
215 * In the case of a PIC binary, the location is relative (+0x1234).
216 * For a non-PIC binary, the location is absolute (@0x1234)
217 *
218 * On success, the out parameter `bin_loc` is set. The ownership is
219 * passed to the caller. On failure, `bin_loc` remains unchanged.
220 *
d5ddf820 221 * @param bin bin_info instance for the executable containing
36ae9941
AB
222 * the address
223 * @param addr Virtual memory address for which to find the
224 * binary location
225 * @param bin_loc Out parameter, the binary location
226 * @returns 0 on success, -1 on failure
227 */
228BT_HIDDEN
d5ddf820 229int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc);
c40a57e5
AB
230
231/**
232 * Destroy the given source_location instance
233 *
234 * @param src_loc source_location instance to destroy
235 */
236BT_HIDDEN
237void source_location_destroy(struct source_location *src_loc);
238
d5ddf820 239#endif /* _BABELTRACE_BIN_INFO_H */
This page took 0.049686 seconds and 4 git commands to generate.