Commit | Line | Data |
---|---|---|
cf73e0fe AB |
1 | /* |
2 | * Copyright (C) 2013 Paul Woegerer <paul_woegerer@mentor.com> | |
3 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #define _LGPL_SOURCE | |
21 | #define _GNU_SOURCE | |
cf73e0fe | 22 | |
8e2aed3f | 23 | #include <link.h> |
cf73e0fe | 24 | #include <limits.h> |
cf73e0fe | 25 | #include <stdio.h> |
8e2aed3f AB |
26 | #include <stdint.h> |
27 | #include <stdlib.h> | |
28 | #include <sys/types.h> | |
29 | #include <unistd.h> | |
cf73e0fe | 30 | |
8e2aed3f | 31 | #include <lttng/ust-elf.h> |
cf73e0fe AB |
32 | #include "lttng-tracer-core.h" |
33 | #include "lttng-ust-statedump.h" | |
34 | ||
35 | #define TRACEPOINT_DEFINE | |
36 | #define TRACEPOINT_CREATE_PROBES | |
37 | #define TP_SESSION_CHECK | |
38 | #include "lttng-ust-statedump-provider.h" | |
39 | ||
40 | struct dl_iterate_data { | |
41 | void *owner; | |
42 | int exec_found; | |
43 | }; | |
44 | ||
45 | struct soinfo_data { | |
46 | void *owner; | |
47 | void *base_addr_ptr; | |
48 | const char *resolved_path; | |
8e2aed3f AB |
49 | char *dbg_file; |
50 | uint8_t *build_id; | |
51 | uint64_t memsz; | |
52 | size_t build_id_len; | |
cf73e0fe | 53 | int vdso; |
8e2aed3f | 54 | uint32_t crc; |
f5eb039d | 55 | uint8_t is_pic; |
cf73e0fe AB |
56 | }; |
57 | ||
58 | typedef void (*tracepoint_cb)(struct lttng_session *session, void *priv); | |
59 | ||
60 | /* | |
61 | * Trace statedump event into all sessions owned by the caller thread | |
62 | * for which statedump is pending. | |
63 | */ | |
64 | static | |
65 | int trace_statedump_event(tracepoint_cb tp_cb, void *owner, void *priv) | |
66 | { | |
67 | struct cds_list_head *sessionsp; | |
68 | struct lttng_session *session; | |
69 | ||
cf73e0fe AB |
70 | sessionsp = _lttng_get_sessions(); |
71 | cds_list_for_each_entry(session, sessionsp, node) { | |
72 | if (session->owner != owner) | |
73 | continue; | |
74 | if (!session->statedump_pending) | |
75 | continue; | |
76 | tp_cb(session, priv); | |
77 | } | |
cf73e0fe AB |
78 | return 0; |
79 | } | |
80 | ||
81 | static | |
82 | void trace_soinfo_cb(struct lttng_session *session, void *priv) | |
83 | { | |
84 | struct soinfo_data *so_data = (struct soinfo_data *) priv; | |
85 | ||
86 | tracepoint(lttng_ust_statedump, soinfo, | |
8e2aed3f | 87 | session, so_data->base_addr_ptr, |
f5eb039d | 88 | so_data->resolved_path, so_data->memsz, so_data->is_pic); |
8e2aed3f AB |
89 | } |
90 | ||
91 | static | |
92 | void trace_build_id_cb(struct lttng_session *session, void *priv) | |
93 | { | |
94 | struct soinfo_data *so_data = (struct soinfo_data *) priv; | |
95 | ||
96 | tracepoint(lttng_ust_statedump, build_id, | |
97 | session, so_data->base_addr_ptr, | |
98 | so_data->build_id, so_data->build_id_len); | |
99 | } | |
100 | ||
101 | static | |
102 | void trace_debug_link_cb(struct lttng_session *session, void *priv) | |
103 | { | |
104 | struct soinfo_data *so_data = (struct soinfo_data *) priv; | |
105 | ||
106 | tracepoint(lttng_ust_statedump, debug_link, | |
107 | session, so_data->base_addr_ptr, | |
108 | so_data->dbg_file, so_data->crc); | |
cf73e0fe AB |
109 | } |
110 | ||
111 | static | |
112 | void trace_start_cb(struct lttng_session *session, void *priv) | |
113 | { | |
114 | tracepoint(lttng_ust_statedump, start, session); | |
115 | } | |
116 | ||
117 | static | |
118 | void trace_end_cb(struct lttng_session *session, void *priv) | |
119 | { | |
120 | tracepoint(lttng_ust_statedump, end, session); | |
121 | } | |
122 | ||
8e2aed3f AB |
123 | static |
124 | int get_elf_info(struct soinfo_data *so_data, int *has_build_id, | |
125 | int *has_debug_link) { | |
126 | struct lttng_ust_elf *elf; | |
127 | int ret = 0; | |
128 | ||
129 | elf = lttng_ust_elf_create(so_data->resolved_path); | |
130 | if (!elf) { | |
131 | ret = -1; | |
132 | goto end; | |
133 | } | |
134 | ||
135 | ret = lttng_ust_elf_get_memsz(elf, &so_data->memsz); | |
136 | if (ret) { | |
137 | goto end; | |
138 | } | |
139 | ||
140 | ret = lttng_ust_elf_get_build_id(elf, &so_data->build_id, | |
141 | &so_data->build_id_len, has_build_id); | |
142 | if (ret) { | |
143 | goto end; | |
144 | } | |
145 | ret = lttng_ust_elf_get_debug_link(elf, &so_data->dbg_file, | |
146 | &so_data->crc, has_debug_link); | |
147 | if (ret) { | |
148 | goto end; | |
149 | } | |
150 | ||
f5eb039d AB |
151 | so_data->is_pic = lttng_ust_elf_is_pic(elf); |
152 | ||
8e2aed3f AB |
153 | end: |
154 | lttng_ust_elf_destroy(elf); | |
155 | return ret; | |
156 | } | |
157 | ||
cf73e0fe AB |
158 | static |
159 | int trace_baddr(struct soinfo_data *so_data) | |
160 | { | |
8e2aed3f AB |
161 | int ret = 0, has_build_id = 0, has_debug_link = 0; |
162 | ||
163 | if (!so_data->vdso) { | |
164 | ret = get_elf_info(so_data, &has_build_id, &has_debug_link); | |
165 | if (ret) { | |
166 | goto end; | |
167 | } | |
168 | } else { | |
169 | so_data->memsz = 0; | |
170 | } | |
171 | ||
172 | ret = trace_statedump_event(trace_soinfo_cb, so_data->owner, so_data); | |
173 | if (ret) { | |
174 | goto end; | |
175 | } | |
cf73e0fe | 176 | |
8e2aed3f AB |
177 | if (has_build_id) { |
178 | ret = trace_statedump_event( | |
179 | trace_build_id_cb, so_data->owner, so_data); | |
180 | free(so_data->build_id); | |
181 | if (ret) { | |
182 | goto end; | |
183 | } | |
cf73e0fe AB |
184 | } |
185 | ||
8e2aed3f AB |
186 | if (has_debug_link) { |
187 | ret = trace_statedump_event( | |
188 | trace_debug_link_cb, so_data->owner, so_data); | |
189 | free(so_data->dbg_file); | |
190 | if (ret) { | |
191 | goto end; | |
192 | } | |
193 | } | |
cf73e0fe | 194 | |
8e2aed3f AB |
195 | end: |
196 | return ret; | |
cf73e0fe AB |
197 | } |
198 | ||
199 | static | |
200 | int trace_statedump_start(void *owner) | |
201 | { | |
202 | return trace_statedump_event(trace_start_cb, owner, NULL); | |
203 | } | |
204 | ||
205 | static | |
206 | int trace_statedump_end(void *owner) | |
207 | { | |
208 | return trace_statedump_event(trace_end_cb, owner, NULL); | |
209 | } | |
210 | ||
211 | static | |
212 | int extract_soinfo_events(struct dl_phdr_info *info, size_t size, void *_data) | |
213 | { | |
d34e6761 | 214 | int j, ret = 0; |
cf73e0fe AB |
215 | struct dl_iterate_data *data = _data; |
216 | ||
d34e6761 MD |
217 | /* |
218 | * UST lock nests within dynamic loader lock. | |
219 | * | |
220 | * Hold this lock across handling of the entire module to | |
221 | * protect memory allocation at early process start, due to | |
222 | * interactions with libc-wrapper lttng malloc instrumentation. | |
223 | */ | |
224 | if (ust_lock()) { | |
225 | goto end; | |
226 | } | |
227 | ||
cf73e0fe AB |
228 | for (j = 0; j < info->dlpi_phnum; j++) { |
229 | struct soinfo_data so_data; | |
230 | char resolved_path[PATH_MAX]; | |
231 | void *base_addr_ptr; | |
232 | ||
233 | if (info->dlpi_phdr[j].p_type != PT_LOAD) | |
234 | continue; | |
235 | ||
236 | /* Calculate virtual memory address of the loadable segment */ | |
237 | base_addr_ptr = (void *) info->dlpi_addr + | |
238 | info->dlpi_phdr[j].p_vaddr; | |
239 | ||
240 | if ((info->dlpi_name == NULL || info->dlpi_name[0] == 0)) { | |
241 | /* | |
242 | * Only the first phdr without a dlpi_name | |
243 | * encountered is considered as the program | |
244 | * executable. The rest are vdsos. | |
245 | */ | |
246 | if (!data->exec_found) { | |
247 | ssize_t path_len; | |
248 | data->exec_found = 1; | |
249 | ||
250 | /* | |
251 | * Use /proc/self/exe to resolve the | |
252 | * executable's full path. | |
253 | */ | |
254 | path_len = readlink("/proc/self/exe", | |
255 | resolved_path, | |
256 | PATH_MAX - 1); | |
257 | if (path_len <= 0) | |
258 | break; | |
259 | ||
260 | resolved_path[path_len] = '\0'; | |
261 | so_data.vdso = 0; | |
262 | } else { | |
263 | snprintf(resolved_path, PATH_MAX - 1, "[vdso]"); | |
264 | so_data.vdso = 1; | |
265 | } | |
266 | } else { | |
267 | /* | |
268 | * For regular dl_phdr_info entries check if | |
269 | * the path to the SO really exists. If not, | |
270 | * treat as vdso and use dlpi_name as 'path'. | |
271 | */ | |
272 | if (!realpath(info->dlpi_name, resolved_path)) { | |
273 | snprintf(resolved_path, PATH_MAX - 1, "[%s]", | |
274 | info->dlpi_name); | |
275 | so_data.vdso = 1; | |
89be5359 AB |
276 | } else { |
277 | so_data.vdso = 0; | |
cf73e0fe AB |
278 | } |
279 | } | |
280 | ||
281 | so_data.owner = data->owner; | |
282 | so_data.base_addr_ptr = base_addr_ptr; | |
283 | so_data.resolved_path = resolved_path; | |
d34e6761 MD |
284 | ret = trace_baddr(&so_data); |
285 | break; | |
cf73e0fe | 286 | } |
d34e6761 MD |
287 | end: |
288 | ust_unlock(); | |
289 | return ret; | |
cf73e0fe AB |
290 | } |
291 | ||
292 | /* | |
293 | * Generate a statedump of base addresses of all shared objects loaded | |
294 | * by the traced application, as well as for the application's | |
295 | * executable itself. | |
296 | */ | |
297 | static | |
298 | int do_baddr_statedump(void *owner) | |
299 | { | |
300 | struct dl_iterate_data data; | |
301 | ||
302 | if (getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP")) | |
303 | return 0; | |
304 | ||
305 | data.owner = owner; | |
306 | data.exec_found = 0; | |
307 | /* | |
308 | * Iterate through the list of currently loaded shared objects and | |
309 | * generate events for loadable segments using | |
310 | * extract_soinfo_events. | |
311 | */ | |
312 | dl_iterate_phdr(extract_soinfo_events, &data); | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
317 | /* | |
318 | * Generate a statedump of a given traced application. A statedump is | |
319 | * delimited by start and end events. For a given (process, session) | |
320 | * pair, begin/end events are serialized and will match. However, in a | |
321 | * session, statedumps from different processes may be | |
322 | * interleaved. The vpid context should be used to identify which | |
323 | * events belong to which process. | |
324 | */ | |
325 | int do_lttng_ust_statedump(void *owner) | |
326 | { | |
327 | trace_statedump_start(owner); | |
328 | do_baddr_statedump(owner); | |
329 | trace_statedump_end(owner); | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
334 | void lttng_ust_statedump_init(void) | |
335 | { | |
336 | __tracepoints__init(); | |
337 | __tracepoints__ptrs_init(); | |
338 | __lttng_events_init__lttng_ust_statedump(); | |
339 | } | |
340 | ||
341 | void lttng_ust_statedump_destroy(void) | |
342 | { | |
343 | __lttng_events_exit__lttng_ust_statedump(); | |
344 | __tracepoints__ptrs_destroy(); | |
345 | __tracepoints__destroy(); | |
346 | } |