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