Visibility hidden by default
[babeltrace.git] / src / plugins / lttng-utils / debug-info / bin-info.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2015 Antoine Busque <abusque@efficios.com>
5 *
6 * Babeltrace - Executable and Shared Object Debug Info Reader
7 */
8
9 #ifndef _BABELTRACE_BIN_INFO_H
10 #define _BABELTRACE_BIN_INFO_H
11
12 #include <babeltrace2/babeltrace.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <gelf.h>
16 #include <elfutils/libdw.h>
17 #include "common/macros.h"
18 #include "fd-cache/fd-cache.h"
19
20 #define DEFAULT_DEBUG_DIR "/usr/lib/debug"
21 #define DEBUG_SUBDIR ".debug"
22 #define BUILD_ID_SUBDIR ".build-id"
23 #define BUILD_ID_SUFFIX ".debug"
24 #define BUILD_ID_PREFIX_DIR_LEN 2
25
26 struct bin_info {
27 bt_logging_level log_level;
28
29 /* Used for logging; can be `NULL` */
30 bt_self_component *self_comp;
31
32 /* Base virtual memory address. */
33 uint64_t low_addr;
34 /* Upper bound of exec address space. */
35 uint64_t high_addr;
36 /* Size of exec address space. */
37 uint64_t memsz;
38 /* Paths to ELF and DWARF files. */
39 gchar *elf_path;
40 gchar *dwarf_path;
41 /* libelf and libdw objects representing the files. */
42 Elf *elf_file;
43 Dwarf *dwarf_info;
44 /* Optional build ID info. */
45 uint8_t *build_id;
46 size_t build_id_len;
47
48 /* Optional debug link info. */
49 gchar *dbg_link_filename;
50 uint32_t dbg_link_crc;
51 /* fd cache handles to ELF and DWARF files. */
52 struct bt_fd_cache_handle *elf_handle;
53 struct bt_fd_cache_handle *dwarf_handle;
54 /* Configuration. */
55 gchar *debug_info_dir;
56 /* Denotes whether the executable is position independent code. */
57 bool is_pic:1;
58 /* Denotes whether the build id in the trace matches to one on disk. */
59 bool file_build_id_matches:1;
60 /*
61 * Denotes whether the executable only has ELF symbols and no
62 * DWARF info.
63 */
64 bool is_elf_only:1;
65 /* Weak ref. Owned by the iterator. */
66 struct bt_fd_cache *fd_cache;
67 };
68
69 struct source_location {
70 uint64_t line_no;
71 gchar *filename;
72 };
73
74 /**
75 * Initializes the bin_info framework. Call this before calling
76 * anything else.
77 *
78 * @returns 0 on success, -1 on failure
79 */
80 int bin_info_init(bt_logging_level log_level,
81 bt_self_component *self_comp);
82
83 /**
84 * Instantiate a structure representing an ELF executable, possibly
85 * with DWARF info, located at the given path.
86 *
87 * @param path Path to the ELF file
88 * @param low_addr Base address of the executable
89 * @param memsz In-memory size of the executable
90 * @param is_pic Whether the executable is position independent
91 * code (PIC)
92 * @param debug_info_dir Directory containing debug info or NULL.
93 * @param target_prefix Path to the root file system of the target
94 * or NULL.
95 * @returns Pointer to the new bin_info on success,
96 * NULL on failure.
97 */
98 struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path,
99 uint64_t low_addr, uint64_t memsz, bool is_pic,
100 const char *debug_info_dir, const char *target_prefix,
101 bt_logging_level log_level, bt_self_component *self_comp);
102
103 /**
104 * Destroy the given bin_info instance
105 *
106 * @param bin bin_info instance to destroy
107 */
108 void bin_info_destroy(struct bin_info *bin);
109
110 /**
111 * Sets the build ID information for a given bin_info instance.
112 *
113 * @param bin The bin_info instance for which to set
114 * the build ID
115 * @param build_id Array of bytes containing the actual ID
116 * @param build_id_len Length in bytes of the build_id
117 * @returns 0 on success, -1 on failure
118 */
119 int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id,
120 size_t build_id_len);
121
122 /**
123 * Sets the debug link information for a given bin_info instance.
124 *
125 * @param bin The bin_info instance for which to set
126 * the debug link
127 * @param filename Name of the separate debug info file
128 * @param crc Checksum for the debug info file
129 * @returns 0 on success, -1 on failure
130 */
131 int bin_info_set_debug_link(struct bin_info *bin, const char *filename,
132 uint32_t crc);
133
134 /**
135 * Returns whether or not the given bin info \p bin contains the
136 * address \p addr.
137 *
138 * @param bin bin_info instance
139 * @param addr Address to lookup
140 * @returns 1 if \p bin contains \p addr, 0 if it does not,
141 * -1 on failure
142 */
143 static inline
144 int bin_info_has_address(struct bin_info *bin, uint64_t addr)
145 {
146 if (!bin) {
147 return -1;
148 }
149
150 return addr >= bin->low_addr && addr < bin->high_addr;
151 }
152
153 /**
154 * Get the name of the function containing a given address within an
155 * executable.
156 *
157 * If no DWARF info is available, the function falls back to ELF
158 * symbols and the "function name" is in fact the name of the closest
159 * symbol, followed by the offset between the symbol and the address.
160 *
161 * On success, if found, the out parameter `func_name` is set. The ownership
162 * of `func_name` is passed to the caller. On failure, `func_name` remains
163 * unchanged.
164 *
165 * @param bin bin_info instance for the executable containing
166 * the address
167 * @param addr Virtual memory address for which to find the
168 * function name
169 * @param func_name Out parameter, the function name.
170 * @returns 0 on success, -1 on failure
171 */
172 int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr,
173 char **func_name);
174
175 /**
176 * Get the source location (file name and line number) for a given
177 * address within an executable.
178 *
179 * If no DWARF info is available, the source location cannot be found
180 * and the function will return unsuccessfully.
181 *
182 * On success, if found, the out parameter `src_loc` is set. The ownership
183 * of `src_loc` is passed to the caller. On failure, `src_loc` remains
184 * unchanged.
185 *
186 * @param bin bin_info instance for the executable containing
187 * the address
188 * @param addr Virtual memory address for which to find the
189 * source location
190 * @param src_loc Out parameter, the source location
191 * @returns 0 on success, -1 on failure
192 */
193 int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr,
194 struct source_location **src_loc);
195 /**
196 * Get a string representing the location within the binary of a given
197 * address.
198 *
199 * In the case of a PIC binary, the location is relative (+0x1234).
200 * For a non-PIC binary, the location is absolute (@0x1234)
201 *
202 * On success, the out parameter `bin_loc` is set. The ownership is
203 * passed to the caller. On failure, `bin_loc` remains unchanged.
204 *
205 * @param bin bin_info instance for the executable containing
206 * the address
207 * @param addr Virtual memory address for which to find the
208 * binary location
209 * @param bin_loc Out parameter, the binary location
210 * @returns 0 on success, -1 on failure
211 */
212 int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc);
213
214 /**
215 * Destroy the given source_location instance
216 *
217 * @param src_loc source_location instance to destroy
218 */
219 void source_location_destroy(struct source_location *src_loc);
220
221 #endif /* _BABELTRACE_BIN_INFO_H */
This page took 0.033566 seconds and 4 git commands to generate.