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