1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/* main dyninst header file
* Copyright (C) 2012 Red Hat Inc.
*
* This file is part of systemtap, and is free software. You can
* redistribute it and/or modify it under the terms of the GNU General
* Public License (GPL); either version 2, or (at your option) any
* later version.
*/
#ifndef _STAPDYN_RUNTIME_H_
#define _STAPDYN_RUNTIME_H_
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <pthread.h>
#include <sched.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/ptrace.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "loc2c-runtime.h"
#include "stapdyn.h"
#if __WORDSIZE == 64
#define CONFIG_64BIT 1
#endif
#define BITS_PER_LONG __WORDSIZE
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#include "linux_types.h"
#include "offset_list.h"
#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000L
#endif
#define _stp_timespec_sub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
if ((result)->tv_nsec < 0) { \
--(result)->tv_sec; \
(result)->tv_nsec += NSEC_PER_SEC; \
} \
} while (0)
#define simple_strtol strtol
// segments don't matter in dyninst...
#define USER_DS (1)
#define KERNEL_DS (-1)
typedef int mm_segment_t;
static inline mm_segment_t get_fs(void) { return 0; }
static inline void set_fs(mm_segment_t seg) { (void)seg; }
static inline int atomic_add_return(int i, atomic_t *v)
{
return __sync_add_and_fetch(&(v->counter), i);
}
static inline int atomic_sub_return(int i, atomic_t *v)
{
return __sync_sub_and_fetch(&(v->counter), i);
}
static inline int pseudo_atomic_cmpxchg(atomic_t *v, int oldval, int newval)
{
return __sync_val_compare_and_swap(&(v->counter), oldval, newval);
}
#include "linux_defs.h"
#define MODULE_DESCRIPTION(str)
#define MODULE_LICENSE(str)
#define MODULE_INFO(tag,info)
/* Semi-forward declarations from runtime_context.h, needed by stat.c/shm.c. */
static int _stp_runtime_num_contexts;
/* Semi-forward declarations from this file, needed by stat.c/transport.c. */
static int stp_pthread_mutex_init_shared(pthread_mutex_t *mutex);
static int stp_pthread_cond_init_shared(pthread_cond_t *cond);
#define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < _stp_runtime_num_contexts; (cpu)++)
#define yield() sched_yield()
#define access_ok(type, addr, size) 1
#define preempt_disable() 0
#define preempt_enable_no_resched() 0
static int _stp_sched_getcpu(void)
{
/* We prefer sched_getcpu directly, of course. It wasn't added until glibc
* 2.6 though, and has no direct feature indication, but CPU_ZERO_S was
* added shortly after too. */
#ifdef CPU_ZERO_S
return sched_getcpu();
#elif defined(SYS_getcpu)
/* A manual getcpu is fine too, though not necessarily as fast since it
* can't be optimized as a vdso/vsyscall. */
unsigned cpu;
int ret = syscall(SYS_getcpu, &cpu, NULL, NULL);
return (ret < 0) ? ret : (int)cpu;
#else
/* XXX Any other way to find our cpu? Manual vgetcpu? */
return -2;
#endif
}
/* see common_session_state.h */
static inline struct _stp_transport_session_data *stp_transport_data(void);
static inline struct _stp_session_attributes *stp_session_attributes(void);
/*
* By definition, we can only debug our own processes with dyninst, so
* assert_is_myproc() will never assert.
*
* FIXME: We might want to add the check back, to get a better error
* message.
*/
#define assert_is_myproc() do {} while (0)
#include "debug.h"
#include "io.c"
#include "alloc.c"
#include "shm.c"
#include "print.h"
#include "stp_string.c"
#include "arith.c"
#include "copy.c"
#include "../regs.c"
#include "task_finder.c"
#include "sym.c"
#include "perf.c"
#include "addr-map.c"
#include "stat.c"
#include "unwind.c"
#include "session_attributes.c"
/* Support function for int64_t module parameters. */
static int set_int64_t(const char *val, int64_t *mp)
{
char *endp;
long long ll;
if (!val)
return -EINVAL;
ll = strtoull(val, &endp, 0);
if ((endp == val) || ((int64_t)ll != ll) || (*endp != '\0'))
return -EINVAL;
*mp = (int64_t)ll;
return 0;
}
static int systemtap_module_init(void);
static void systemtap_module_exit(void);
static inline unsigned long _stap_hash_seed(); /* see common_session_state.h */
#define stap_hash_seed _stap_hash_seed()
typedef pthread_rwlock_t rwlock_t; /* for globals */
static int stp_pthread_mutex_init_shared(pthread_mutex_t *mutex)
{
int rc;
pthread_mutexattr_t attr;
rc = pthread_mutexattr_init(&attr);
if (rc != 0) {
_stp_error("pthread_mutexattr_init failed");
return rc;
}
rc = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
if (rc != 0) {
_stp_error("pthread_mutexattr_setpshared failed");
goto err_attr;
}
rc = pthread_mutex_init(mutex, &attr);
if (rc != 0) {
_stp_error("pthread_mutex_init failed");
goto err_attr;
}
err_attr:
(void)pthread_mutexattr_destroy(&attr);
return rc;
}
static int stp_pthread_cond_init_shared(pthread_cond_t *cond)
{
int rc;
pthread_condattr_t attr;
rc = pthread_condattr_init(&attr);
if (rc != 0) {
_stp_error("pthread_condattr_init failed");
return rc;
}
rc = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
if (rc != 0) {
_stp_error("pthread_condattr_setpshared failed");
goto err_attr;
}
rc = pthread_cond_init(cond, &attr);
if (rc != 0) {
_stp_error("pthread_cond_init failed");
goto err_attr;
}
err_attr:
(void)pthread_condattr_destroy(&attr);
return rc;
}
static int stp_pthread_rwlock_init_shared(pthread_rwlock_t *rwlock)
{
int rc;
pthread_rwlockattr_t attr;
rc = pthread_rwlockattr_init(&attr);
if (rc != 0) {
_stp_error("pthread_rwlockattr_init failed");
return rc;
}
rc = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
if (rc != 0) {
_stp_error("pthread_rwlockattr_setpshared failed");
goto err_attr;
}
rc = pthread_rwlock_init(rwlock, &attr);
if (rc != 0) {
_stp_error("pthread_rwlock_init failed");
goto err_attr;
}
err_attr:
(void)pthread_rwlockattr_destroy(&attr);
return rc;
}
static inline void stp_synchronize_sched(void) { }
/*
* For stapdyn to work in a multiprocess environment, the module must be
* prepared to be loaded multiple times in different processes. Thus, we have
* to distinguish between process-level resource initialization and
* "session"-level (like probe begin/end/error).
*
* So stp_dyninst_ctor/dtor are the process-level functions, using gcc
* attributes to get called at the right time. One startup exception is
* stp_dyninst_shm_connect, which has to later be called manually with the shm
* path being used in this session.
*
* The session-level resources have to be started by the stapdyn mutator, and
* are called within stapdyn itself. This primarily involves allocating the
* shared memory and initializing its contents, but also running those global
* begin/end/error probes.
*
* NB: We used to keep code that tried to deal with stapdyn 2.0, which didn't
* know about shm initialization, and ran everything in the mutatees only.
* We've now broken that tie, and stp_dyninst_session_init will detect that
* shm wasn't initialized and bow out.
*/
static int _stp_runtime_contexts_init(void);
static int stp_dyninst_ctor_rc = 0;
__attribute__((constructor))
static void stp_dyninst_ctor(void)
{
int rc = 0;
_stp_mem_fd = open("/proc/self/mem", O_RDWR /*| O_LARGEFILE*/);
if (_stp_mem_fd != -1) {
fcntl(_stp_mem_fd, F_SETFD, FD_CLOEXEC);
}
else {
rc = -errno;
}
if (rc == 0)
rc = _stp_runtime_contexts_init();
if (rc == 0)
rc = _stp_print_init();
stp_dyninst_ctor_rc = rc;
}
const char* stp_dyninst_shm_init(void)
{
return _stp_shm_init();
}
int stp_dyninst_shm_connect(const char* name)
{
int rc;
/* We don't have a chance to indicate errors in the ctor, so do it here. */
if (stp_dyninst_ctor_rc != 0) {
return stp_dyninst_ctor_rc;
}
rc = _stp_shm_connect(name);
return rc;
}
int stp_dyninst_session_init(void)
{
/* We don't have a chance to indicate errors in the ctor, so do it here. */
if (stp_dyninst_ctor_rc != 0) {
return stp_dyninst_ctor_rc;
}
/* If shared memory has not been initialized yet, we're probably dealing
* with stapdyn 2.0 -- we no longer support this case. */
if (_stp_shm_base == NULL)
return -ENOMEM;
return systemtap_module_init();
}
/* This is called during systemtap_module_init, after globals/etc are set up,
* but before any probes are actually executed.
* (Perhaps it would be cleaner if the translator split those stages?)
*/
static int stp_dyninst_session_init_finished(void)
{
_stp_shm_finalize();
/* Now that the shared memory is finalized, start the
* transport. If we started it before now, allocations could have
* caused the base address of the shared memory to move around,
* which would cause the addresses of the mutexes to move
* around. */
return _stp_dyninst_transport_session_start();
}
void stp_dyninst_session_exit(void)
{
systemtap_module_exit();
}
static int _stp_exit_status = 0;
int stp_dyninst_exit_status(void)
{
return _stp_exit_status;
}
__attribute__((destructor))
static void stp_dyninst_dtor(void)
{
_stp_print_cleanup();
_stp_shm_destroy();
if (_stp_mem_fd != -1) {
close (_stp_mem_fd);
}
}
#endif /* _STAPDYN_RUNTIME_H_ */
|