baddr: get session under lock
[lttng-ust.git] / liblttng-ust / lttng-ust-baddr.c
CommitLineData
b13d93c2
PW
1/*
2 * Copyright (C) 2013 Paul Woegerer <paul_woegerer@mentor.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#define _GNU_SOURCE
20#include <dlfcn.h>
21#include <link.h>
22
23#include <sys/types.h>
24#include <sys/stat.h>
b13d93c2
PW
25#include <unistd.h>
26#include <limits.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <stdint.h>
30#include <stddef.h>
31#include <stdio.h>
32#include "usterr.h"
33
13436238
PW
34#include "lttng-ust-baddr.h"
35
b13d93c2 36#define TRACEPOINT_DEFINE
95c25348 37#include "ust_baddr_statedump.h"
b13d93c2 38
37dddb65
MD
39static
40int extract_soinfo_events(struct dl_phdr_info *info, size_t size, void *data)
95c25348
PW
41{
42 int j;
43 int num_loadable_segment = 0;
37dddb65
MD
44 void *owner = data;
45 struct cds_list_head *sessionsp;
46
95c25348
PW
47 for (j = 0; j < info->dlpi_phnum; j++) {
48 char resolved_path[PATH_MAX];
49 struct stat sostat;
50 void *base_addr_ptr;
37dddb65 51 struct lttng_session *session;
95c25348
PW
52
53 if (info->dlpi_phdr[j].p_type != PT_LOAD)
54 continue;
55
56 /* Calculate virtual memory address of the loadable segment */
57 base_addr_ptr = (void *) info->dlpi_addr
58 + info->dlpi_phdr[j].p_vaddr;
59
60 num_loadable_segment += 1;
61 if ((info->dlpi_name == NULL || info->dlpi_name[0] == 0)
62 && num_loadable_segment == 1) {
63 /*
64 * If the iterated element is the executable itself we
65 * have to use Dl_info to determine its full path
66 */
67 Dl_info dl_info = { 0 };
68 if (!dladdr(base_addr_ptr, &dl_info))
69 return 0;
70 if (!realpath(dl_info.dli_fname, resolved_path))
71 return 0;
72 } else {
73 /*
74 * For regular dl_phdr_info entries we have to check if
75 * the path to the shared object really exists
76 */
77 if (!realpath(info->dlpi_name, resolved_path)) {
78 /* Found vDSO, put the 'path' into brackets */
79 snprintf(resolved_path, PATH_MAX - 1, "[%s]",
80 info->dlpi_name);
81 }
82 }
83
84 if (stat(resolved_path, &sostat)) {
85 sostat.st_size = 0;
86 sostat.st_mtime = -1;
87 }
88
37dddb65
MD
89 /*
90 * UST lock needs to be nested within dynamic loader
91 * lock.
92 */
93 ust_lock();
76b82fc0 94 sessionsp = _lttng_get_sessions();
37dddb65
MD
95 cds_list_for_each_entry(session, sessionsp, node) {
96 if (session->owner != owner)
97 continue;
98 if (!session->statedump_pending)
99 continue;
100 tracepoint(ust_baddr_statedump, soinfo,
101 session, base_addr_ptr,
102 resolved_path, sostat.st_size,
103 sostat.st_mtime);
104 }
105 ust_unlock();
95c25348
PW
106
107 /*
108 * We are only interested in the base address (lowest virtual
109 * address associated with the memory image), skip the rest
110 */
111 break;
112 }
113 return 0;
114}
115
37dddb65 116int lttng_ust_baddr_statedump(void *owner)
95c25348 117{
ca7643d6
PW
118 if (getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
119 return 0;
95c25348
PW
120 /*
121 * Iterate through the list of currently loaded shared objects and
37dddb65
MD
122 * generate events for loadable segments using
123 * extract_soinfo_events.
95c25348 124 */
37dddb65 125 dl_iterate_phdr(extract_soinfo_events, owner);
95c25348
PW
126 return 0;
127}
This page took 0.028551 seconds and 4 git commands to generate.