| 1 |
// stapdyn mutator functions |
| 2 |
// Copyright (C) 2012-2018 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 |
#include "mutator.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
|
| 13 |
extern "C" { |
| 14 |
#include <dlfcn.h> |
| 15 |
#include <wordexp.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <time.h> |
| 18 |
} |
| 19 |
|
| 20 |
#include <dyninst/BPatch_snippet.h> |
| 21 |
|
| 22 |
#include "dynutil.h" |
| 23 |
#include "../staputil.h" |
| 24 |
|
| 25 |
extern "C" { |
| 26 |
#include "../runtime/dyninst/stapdyn.h" |
| 27 |
} |
| 28 |
|
| 29 |
using namespace std; |
| 30 |
|
| 31 |
|
| 32 |
// NB: since Dyninst callbacks have no context, we have to demux it |
| 33 |
// to every mutator we've created, tracked by this vector. |
| 34 |
static vector<mutator*> g_mutators; |
| 35 |
|
| 36 |
static void |
| 37 |
g_dynamic_library_callback(BPatch_thread *thread, |
| 38 |
BPatch_object *object, |
| 39 |
bool load) |
| 40 |
{ |
| 41 |
for (size_t i = 0; i < g_mutators.size(); ++i) |
| 42 |
g_mutators[i]->dynamic_library_callback(thread, object, load); |
| 43 |
} |
| 44 |
|
| 45 |
#ifndef DYNINST_9_1 |
| 46 |
static void |
| 47 |
g_dynamic_module_callback(BPatch_thread *thread, |
| 48 |
BPatch_module *module, |
| 49 |
bool load) |
| 50 |
{ |
| 51 |
g_dynamic_library_callback(thread, module->getObject(), load); |
| 52 |
} |
| 53 |
#define g_dynamic_library_callback g_dynamic_module_callback |
| 54 |
#endif |
| 55 |
|
| 56 |
|
| 57 |
static void |
| 58 |
g_post_fork_callback(BPatch_thread *parent, BPatch_thread *child) |
| 59 |
{ |
| 60 |
for (size_t i = 0; i < g_mutators.size(); ++i) |
| 61 |
g_mutators[i]->post_fork_callback(parent, child); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static void |
| 66 |
g_exec_callback(BPatch_thread *thread) |
| 67 |
{ |
| 68 |
for (size_t i = 0; i < g_mutators.size(); ++i) |
| 69 |
g_mutators[i]->exec_callback(thread); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
static void |
| 74 |
g_exit_callback(BPatch_thread *thread, BPatch_exitType type) |
| 75 |
{ |
| 76 |
for (size_t i = 0; i < g_mutators.size(); ++i) |
| 77 |
g_mutators[i]->exit_callback(thread, type); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
static void |
| 82 |
g_thread_create_callback(BPatch_process *proc, BPatch_thread *thread) |
| 83 |
{ |
| 84 |
for (size_t i = 0; i < g_mutators.size(); ++i) |
| 85 |
g_mutators[i]->thread_create_callback(proc, thread); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
static void |
| 90 |
g_thread_destroy_callback(BPatch_process *proc, BPatch_thread *thread) |
| 91 |
{ |
| 92 |
for (size_t i = 0; i < g_mutators.size(); ++i) |
| 93 |
g_mutators[i]->thread_destroy_callback(proc, thread); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
static pthread_t g_main_thread = pthread_self(); |
| 98 |
static const sigset_t *g_signal_mask; |
| 99 |
|
| 100 |
static void |
| 101 |
g_signal_handler(int signal) |
| 102 |
{ |
| 103 |
/* We only want the signal on our main thread, so it will interrupt the ppoll |
| 104 |
* loop. If we get it on a different thread, just forward it. */ |
| 105 |
if (!pthread_equal(pthread_self(), g_main_thread)) |
| 106 |
{ |
| 107 |
pthread_kill(g_main_thread, signal); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
for (size_t i = 0; i < g_mutators.size(); ++i) |
| 112 |
g_mutators[i]->signal_callback(signal); |
| 113 |
} |
| 114 |
|
| 115 |
__attribute__((constructor)) |
| 116 |
static void |
| 117 |
setup_signals (void) |
| 118 |
{ |
| 119 |
struct sigaction sa; |
| 120 |
static sigset_t mask; |
| 121 |
static const int signals[] = { |
| 122 |
SIGHUP, SIGINT, SIGTERM, SIGQUIT, |
| 123 |
}; |
| 124 |
|
| 125 |
/* Prepare the global sigmask for future use. */ |
| 126 |
sigemptyset (&mask); |
| 127 |
for (size_t i = 0; i < sizeof(signals) / sizeof(*signals); ++i) |
| 128 |
sigaddset (&mask, signals[i]); |
| 129 |
g_signal_mask = &mask; |
| 130 |
|
| 131 |
/* Prepare the common signal handler. */ |
| 132 |
memset(&sa, 0, sizeof(sa)); |
| 133 |
sa.sa_handler = g_signal_handler; |
| 134 |
sa.sa_flags = SA_RESTART; |
| 135 |
sigemptyset (&sa.sa_mask); |
| 136 |
for (size_t i = 0; i < sizeof(signals) / sizeof(*signals); ++i) |
| 137 |
sigaddset (&sa.sa_mask, signals[i]); |
| 138 |
|
| 139 |
/* Activate the handler for every signal. */ |
| 140 |
for (size_t i = 0; i < sizeof(signals) / sizeof(*signals); ++i) |
| 141 |
sigaction (signals[i], &sa, NULL); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
mutator::mutator (const string& module_name, |
| 146 |
vector<string>& module_options): |
| 147 |
module(NULL), module_name(resolve_path(module_name)), |
| 148 |
modoptions(module_options), p_target_created(false), |
| 149 |
p_target_error(false), utrace_enter_fn(NULL) |
| 150 |
{ |
| 151 |
// NB: dlopen does a library-path search if the filename doesn't have any |
| 152 |
// path components, which is why we use resolve_path(module_name) |
| 153 |
|
| 154 |
sigemptyset(&signals_received); |
| 155 |
|
| 156 |
g_mutators.push_back(this); |
| 157 |
} |
| 158 |
|
| 159 |
mutator::~mutator () |
| 160 |
{ |
| 161 |
// Explicitly drop our mutatee references, so we better |
| 162 |
// control when their instrumentation is removed. |
| 163 |
target_mutatee.reset(); |
| 164 |
mutatees.clear(); |
| 165 |
|
| 166 |
if (module) |
| 167 |
{ |
| 168 |
dlclose(module); |
| 169 |
module = NULL; |
| 170 |
} |
| 171 |
|
| 172 |
g_mutators.erase(find(g_mutators.begin(), g_mutators.end(), this)); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
// Do probes matching 'flag' exist? |
| 177 |
bool |
| 178 |
mutator::matching_probes_exist(uint64_t flag) |
| 179 |
{ |
| 180 |
for (size_t i = 0; i < targets.size(); ++i) |
| 181 |
{ |
| 182 |
for (size_t j = 0; j < targets[i].probes.size(); ++j) |
| 183 |
{ |
| 184 |
if (targets[i].probes[j].flags & flag) |
| 185 |
return true; |
| 186 |
} |
| 187 |
} |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
// Load the stap module and initialize all probe info. |
| 193 |
bool |
| 194 |
mutator::load () |
| 195 |
{ |
| 196 |
int rc; |
| 197 |
|
| 198 |
// Open the module directly, so we can query probes or run simple ones. |
| 199 |
(void)dlerror(); // clear previous errors |
| 200 |
module = dlopen(module_name.c_str(), RTLD_NOW); |
| 201 |
if (!module) |
| 202 |
{ |
| 203 |
staperror() << "dlopen " << dlerror() << endl; |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
if ((rc = find_dynprobes(module, targets))) |
| 208 |
return rc; |
| 209 |
if (!targets.empty()) |
| 210 |
{ |
| 211 |
// Always watch for new libraries to probe. |
| 212 |
patch.registerDynLibraryCallback(g_dynamic_library_callback); |
| 213 |
|
| 214 |
// Always watch for new child processes, even if we don't have |
| 215 |
// STAPDYN_PROBE_FLAG_PROC_BEGIN, because we might want to trigger |
| 216 |
// any of the other types of probes in new processes too. |
| 217 |
patch.registerPostForkCallback(g_post_fork_callback); |
| 218 |
patch.registerExecCallback(g_exec_callback); |
| 219 |
|
| 220 |
// Do we need a exit callback? |
| 221 |
if (matching_probes_exist(STAPDYN_PROBE_FLAG_PROC_END)) |
| 222 |
patch.registerExitCallback(g_exit_callback); |
| 223 |
|
| 224 |
// Do we need a thread create callback? |
| 225 |
if (matching_probes_exist(STAPDYN_PROBE_FLAG_THREAD_BEGIN)) |
| 226 |
patch.registerThreadEventCallback(BPatch_threadCreateEvent, |
| 227 |
g_thread_create_callback); |
| 228 |
|
| 229 |
// Do we need a thread destroy callback? |
| 230 |
if (matching_probes_exist(STAPDYN_PROBE_FLAG_THREAD_END)) |
| 231 |
patch.registerThreadEventCallback(BPatch_threadDestroyEvent, |
| 232 |
g_thread_destroy_callback); |
| 233 |
} |
| 234 |
|
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
// Create a new process with the given command line |
| 239 |
bool |
| 240 |
mutator::create_process(const string& command) |
| 241 |
{ |
| 242 |
if (target_mutatee) |
| 243 |
{ |
| 244 |
staperror() << "Already attached to a target process!" << endl; |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
// Split the command into words. If wordexp can't do it, |
| 249 |
// we'll just run via "sh -c" instead. |
| 250 |
const char** child_argv; |
| 251 |
const char* sh_argv[] = { "/bin/sh", "-c", command.c_str(), NULL }; |
| 252 |
wordexp_t words; |
| 253 |
int rc = wordexp (command.c_str(), &words, WRDE_NOCMD|WRDE_UNDEF); |
| 254 |
if (rc == 0) |
| 255 |
child_argv = (/*cheater*/ const char**) words.we_wordv; |
| 256 |
else if (rc == WRDE_BADCHAR) |
| 257 |
child_argv = sh_argv; |
| 258 |
else |
| 259 |
{ |
| 260 |
staperror() << "wordexp parsing error (" << rc << ")" << endl; |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
// Search the PATH if necessary, then create the target process! |
| 265 |
string fullpath = find_executable(child_argv[0]); |
| 266 |
BPatch_process* app = patch.processCreate(fullpath.c_str(), child_argv); |
| 267 |
if (!app) |
| 268 |
{ |
| 269 |
staperror() << "Couldn't create the target process" << endl; |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
auto m = make_shared<mutatee>(app); |
| 274 |
mutatees.push_back(m); |
| 275 |
target_mutatee = m; |
| 276 |
p_target_created = true; |
| 277 |
|
| 278 |
if (!m->load_stap_dso(module_name)) |
| 279 |
return false; |
| 280 |
|
| 281 |
if (!targets.empty()) |
| 282 |
m->instrument_dynprobes(targets); |
| 283 |
|
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
// Attach to a specific existing process. |
| 288 |
bool |
| 289 |
mutator::attach_process(pid_t pid) |
| 290 |
{ |
| 291 |
if (target_mutatee) |
| 292 |
{ |
| 293 |
staperror() << "Already attached to a target process!" << endl; |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
BPatch_process* app = patch.processAttach(NULL, pid); |
| 298 |
if (!app) |
| 299 |
{ |
| 300 |
staperror() << "Couldn't attach to the target process" << endl; |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
auto m = make_shared<mutatee>(app); |
| 305 |
mutatees.push_back(m); |
| 306 |
target_mutatee = m; |
| 307 |
p_target_created = false; |
| 308 |
|
| 309 |
if (!m->load_stap_dso(module_name)) |
| 310 |
return false; |
| 311 |
|
| 312 |
if (!targets.empty()) |
| 313 |
m->instrument_dynprobes(targets); |
| 314 |
|
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
bool |
| 319 |
mutator::init_modoptions() |
| 320 |
{ |
| 321 |
decltype(&stp_global_setter) global_setter = NULL; |
| 322 |
set_dlsym(global_setter, module, "stp_global_setter", false); |
| 323 |
|
| 324 |
if (global_setter == NULL) |
| 325 |
{ |
| 326 |
// Hypothetical backwards compatibility with older stapdyn: |
| 327 |
stapwarn() << "Compiled module does not support -G globals" << endl; |
| 328 |
return true; // soft warning; let it go on anyway |
| 329 |
} |
| 330 |
|
| 331 |
for (vector<string>::iterator it = modoptions.begin(); |
| 332 |
it != modoptions.end(); it++) |
| 333 |
{ |
| 334 |
string modoption = *it; |
| 335 |
|
| 336 |
// Parse modoption as "name=value" |
| 337 |
// XXX: compare whether this behaviour fits safety regex in buildrun.cxx |
| 338 |
string::size_type separator = modoption.find('='); |
| 339 |
if (separator == string::npos) |
| 340 |
{ |
| 341 |
staperror() << "Could not parse module option '" << modoption << "'" << endl; |
| 342 |
return false; // XXX: perhaps ignore the option instead? |
| 343 |
} |
| 344 |
string name = modoption.substr(0, separator); |
| 345 |
string value = modoption.substr(separator+1); |
| 346 |
|
| 347 |
int rc = global_setter(name.c_str(), value.c_str()); |
| 348 |
if (rc == -ENOENT) |
| 349 |
{ |
| 350 |
stapwarn() << "Ignoring unknown module option '" << name << "'" << endl; |
| 351 |
continue; // soft warning; let it go on anyway |
| 352 |
} |
| 353 |
else if (rc != 0) |
| 354 |
{ |
| 355 |
staperror() << "module option '" << modoption << "': " |
| 356 |
<< strerror(abs(rc)) << endl; |
| 357 |
return false; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
void |
| 365 |
mutator::init_session_attributes() |
| 366 |
{ |
| 367 |
decltype(&stp_global_setter) global_setter = NULL; |
| 368 |
set_dlsym(global_setter, module, "stp_global_setter", false); |
| 369 |
|
| 370 |
if (global_setter == NULL) |
| 371 |
{ |
| 372 |
// Just return. |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
// Note that the list of supported attributes should match with the |
| 377 |
// list in 'struct _stp_sesion_attributes' in |
| 378 |
// runtime/dyninst/session_attributes.h. |
| 379 |
|
| 380 |
int rc = global_setter("@log_level", lex_cast(stapdyn_log_level).c_str()); |
| 381 |
if (rc != 0) |
| 382 |
stapwarn() << "Couldn't set 'log_level' global" << endl; |
| 383 |
|
| 384 |
rc = global_setter("@suppress_warnings", |
| 385 |
lex_cast(stapdyn_suppress_warnings).c_str()); |
| 386 |
if (rc != 0) |
| 387 |
stapwarn() << "Couldn't set 'suppress_warnings' global" << endl; |
| 388 |
|
| 389 |
rc = global_setter("@stp_pid", lex_cast(getpid()).c_str()); |
| 390 |
if (rc != 0) |
| 391 |
stapwarn() << "Couldn't set 'stp_pid' global" << endl; |
| 392 |
|
| 393 |
if (target_mutatee) |
| 394 |
{ |
| 395 |
rc = global_setter("@target", lex_cast(target_mutatee->process_id()).c_str()); |
| 396 |
if (rc != 0) |
| 397 |
stapwarn() << "Couldn't set 'target' global" << endl; |
| 398 |
} |
| 399 |
|
| 400 |
size_t module_endpath = module_name.rfind('/'); |
| 401 |
size_t module_basename_start = |
| 402 |
(module_endpath != string::npos) ? module_endpath + 1 : 0; |
| 403 |
size_t module_basename_end = module_name.find('.', module_basename_start); |
| 404 |
size_t module_basename_len = module_basename_end - module_basename_start; |
| 405 |
string module_basename(module_name, module_basename_start, module_basename_len); |
| 406 |
rc = global_setter("@module_name", module_basename.c_str()); |
| 407 |
if (rc != 0) |
| 408 |
stapwarn() << "Couldn't set 'module_name' global" << endl; |
| 409 |
|
| 410 |
time_t now_t = time(NULL); |
| 411 |
struct tm* now = localtime(&now_t); |
| 412 |
if (now) |
| 413 |
{ |
| 414 |
rc = global_setter("@tz_gmtoff", lex_cast(-now->tm_gmtoff).c_str()); |
| 415 |
if (rc != 0) |
| 416 |
stapwarn() << "Couldn't set 'tz_gmtoff' global" << endl; |
| 417 |
rc = global_setter("@tz_name", now->tm_zone); |
| 418 |
if (rc != 0) |
| 419 |
stapwarn() << "Couldn't set 'tz_name' global" << endl; |
| 420 |
} |
| 421 |
else |
| 422 |
stapwarn() << "Couldn't discover local timezone info" << endl; |
| 423 |
|
| 424 |
if (stapdyn_outfile_name) |
| 425 |
{ |
| 426 |
rc = global_setter("@outfile_name", |
| 427 |
lex_cast(stapdyn_outfile_name).c_str()); |
| 428 |
if (rc != 0) |
| 429 |
stapwarn() << "Couldn't set 'outfile_name' global" << endl; |
| 430 |
} |
| 431 |
|
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
// Initialize the module session |
| 436 |
bool |
| 437 |
mutator::run_module_init() |
| 438 |
{ |
| 439 |
if (!module) |
| 440 |
return false; |
| 441 |
|
| 442 |
// First see if this is a shared-memory, multiprocess-capable module |
| 443 |
decltype(&stp_dyninst_shm_init) shm_init = NULL; |
| 444 |
decltype(&stp_dyninst_shm_connect) shm_connect = NULL; |
| 445 |
set_dlsym(shm_init, module, "stp_dyninst_shm_init", false); |
| 446 |
set_dlsym(shm_connect, module, "stp_dyninst_shm_connect", false); |
| 447 |
if (shm_init && shm_connect) |
| 448 |
{ |
| 449 |
// Initialize the shared-memory locally. |
| 450 |
const char* shmem = shm_init(); |
| 451 |
if (shmem == NULL) |
| 452 |
{ |
| 453 |
stapwarn() << "stp_dyninst_shm_init failed!" << endl; |
| 454 |
return false; |
| 455 |
} |
| 456 |
module_shmem = shmem; |
| 457 |
// After the session is initilized, then we'll map shmem in the target |
| 458 |
} |
| 459 |
else if (target_mutatee) |
| 460 |
{ |
| 461 |
// For modules that don't support shared-memory, but still have a target |
| 462 |
// process, we'll run init/exit in the target. |
| 463 |
target_mutatee->call_function("stp_dyninst_session_init"); |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
// From here, either this is a shared-memory module, |
| 468 |
// or we have no target and thus run init directly anyway. |
| 469 |
|
| 470 |
decltype(&stp_dyninst_session_init) session_init = NULL; |
| 471 |
try |
| 472 |
{ |
| 473 |
set_dlsym(session_init, module, "stp_dyninst_session_init"); |
| 474 |
} |
| 475 |
catch (runtime_error& e) |
| 476 |
{ |
| 477 |
staperror() << e.what() << endl; |
| 478 |
return false; |
| 479 |
} |
| 480 |
|
| 481 |
// Before init runs, set any custom variables |
| 482 |
if (!modoptions.empty() && !init_modoptions()) |
| 483 |
return false; |
| 484 |
|
| 485 |
init_session_attributes(); |
| 486 |
|
| 487 |
int rc = session_init(); |
| 488 |
if (rc) |
| 489 |
{ |
| 490 |
stapwarn() << "stp_dyninst_session_init returned " << rc << endl; |
| 491 |
return false; |
| 492 |
} |
| 493 |
|
| 494 |
// Now we map the shared-memory into the target |
| 495 |
if (target_mutatee && !module_shmem.empty()) |
| 496 |
{ |
| 497 |
vector<BPatch_snippet *> args; |
| 498 |
args.push_back(new BPatch_constExpr(module_shmem.c_str())); |
| 499 |
target_mutatee->call_function("stp_dyninst_shm_connect", args); |
| 500 |
} |
| 501 |
|
| 502 |
return true; |
| 503 |
} |
| 504 |
|
| 505 |
// Shutdown the module session |
| 506 |
bool |
| 507 |
mutator::run_module_exit() |
| 508 |
{ |
| 509 |
if (!module) |
| 510 |
return false; |
| 511 |
|
| 512 |
if (target_mutatee && module_shmem.empty()) |
| 513 |
{ |
| 514 |
// For modules that don't support shared-memory, but still have a target |
| 515 |
// process, we'll run init/exit in the target. |
| 516 |
// XXX This may already have been done in its deconstructor if the process exited. |
| 517 |
target_mutatee->call_function("stp_dyninst_session_exit"); |
| 518 |
return true; |
| 519 |
} |
| 520 |
|
| 521 |
// From here, either this is a shared-memory module, |
| 522 |
// or we have no target and thus run exit directly anyway. |
| 523 |
|
| 524 |
decltype(&stp_dyninst_session_exit) session_exit = NULL; |
| 525 |
try |
| 526 |
{ |
| 527 |
set_dlsym(session_exit, module, "stp_dyninst_session_exit"); |
| 528 |
} |
| 529 |
catch (runtime_error& e) |
| 530 |
{ |
| 531 |
staperror() << e.what() << endl; |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
session_exit(); |
| 536 |
return true; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
// Check the status of all mutatees |
| 541 |
bool |
| 542 |
mutator::update_mutatees() |
| 543 |
{ |
| 544 |
// We'll always break right away for SIGQUIT. We'll also break for any other |
| 545 |
// signal if we didn't create the process. Otherwise, we should give the |
| 546 |
// created process a chance to finish. |
| 547 |
if (sigismember(&signals_received, SIGQUIT) || |
| 548 |
(!sigisemptyset(&signals_received) && !p_target_created)) |
| 549 |
return false; |
| 550 |
|
| 551 |
if (target_mutatee && target_mutatee->is_terminated()) |
| 552 |
return false; |
| 553 |
|
| 554 |
for (size_t i = 0; i < mutatees.size();) |
| 555 |
{ |
| 556 |
auto m = mutatees[i]; |
| 557 |
if (m != target_mutatee && m->is_terminated()) |
| 558 |
{ |
| 559 |
mutatees.erase(mutatees.begin() + i); |
| 560 |
continue; // NB: without ++i |
| 561 |
} |
| 562 |
++i; |
| 563 |
} |
| 564 |
|
| 565 |
return true; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
// Start the actual systemtap session! |
| 570 |
bool |
| 571 |
mutator::run () |
| 572 |
{ |
| 573 |
if (!targets.empty() && !target_mutatee) |
| 574 |
stapwarn() << "process probes require a target (-c or -x)" << endl; |
| 575 |
|
| 576 |
// Get the stap module ready... |
| 577 |
if (!run_module_init()) |
| 578 |
{ |
| 579 |
// Detach from everything |
| 580 |
target_mutatee.reset(); |
| 581 |
mutatees.clear(); |
| 582 |
|
| 583 |
return false; |
| 584 |
} |
| 585 |
|
| 586 |
// And away we go! |
| 587 |
if (target_mutatee) |
| 588 |
{ |
| 589 |
// For our first event, fire the target's process.begin probes (if any) |
| 590 |
target_mutatee->begin_callback(); |
| 591 |
target_mutatee->continue_execution(); |
| 592 |
|
| 593 |
// Dyninst's notification FD was fixed in 8.1; for earlier versions we'll |
| 594 |
// fall back to the fully-blocking wait for now. |
| 595 |
#ifdef DYNINST_8_1 |
| 596 |
// mask signals while we're preparing to poll |
| 597 |
stap_sigmasker masked(g_signal_mask); |
| 598 |
|
| 599 |
pollfd pfd; |
| 600 |
pfd.fd = patch.getNotificationFD(); |
| 601 |
pfd.events = POLLIN; |
| 602 |
pfd.revents = 0; |
| 603 |
|
| 604 |
// Polling with a notification FD lets us wait on Dyninst while still |
| 605 |
// letting signals break us out of the loop. |
| 606 |
while (update_mutatees()) |
| 607 |
{ |
| 608 |
struct timespec timeout = { 10, 0 }; |
| 609 |
|
| 610 |
// When a mutatee exits, the cleanup may cause dyninstAPI to dequeue |
| 611 |
// other events from proccontrol (which clears the poll FD), without |
| 612 |
// actually processing those events yet. Check before we sleep... |
| 613 |
if (patch.pollForStatusChange()) |
| 614 |
continue; |
| 615 |
|
| 616 |
int rc = ppoll (&pfd, 1, &timeout, &masked.old); |
| 617 |
if (rc < 0 && errno != EINTR) |
| 618 |
break; |
| 619 |
|
| 620 |
// Acknowledge and activate whatever events are waiting |
| 621 |
patch.pollForStatusChange(); |
| 622 |
} |
| 623 |
#else |
| 624 |
while (update_mutatees()) |
| 625 |
patch.waitForStatusChange(); |
| 626 |
#endif |
| 627 |
} |
| 628 |
else // !target_mutatee |
| 629 |
{ |
| 630 |
// With no mutatees, we just wait for a signal to exit. |
| 631 |
stap_sigmasker masked(g_signal_mask); |
| 632 |
while (sigisemptyset(&signals_received)) |
| 633 |
sigsuspend(&masked.old); |
| 634 |
} |
| 635 |
|
| 636 |
// Indicate failure if the target had anything but EXIT_SUCCESS |
| 637 |
if (target_mutatee && target_mutatee->is_terminated()) |
| 638 |
p_target_error = !target_mutatee->check_exit(); |
| 639 |
|
| 640 |
// Apply a timeout to the following; dyninst or other bugs can |
| 641 |
// sometimes hang during this stage (PR23572). |
| 642 |
alarm(30); |
| 643 |
// Detach from everything |
| 644 |
target_mutatee.reset(); |
| 645 |
mutatees.clear(); |
| 646 |
// Stand down. |
| 647 |
alarm(0); |
| 648 |
|
| 649 |
// Shutdown the stap module. |
| 650 |
return run_module_exit(); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
// Get the final exit status of this mutator |
| 655 |
int mutator::exit_status () |
| 656 |
{ |
| 657 |
if (!module) |
| 658 |
return EXIT_FAILURE; |
| 659 |
|
| 660 |
// NB: Only shm modules are new enough to have stp_dyninst_exit_status at |
| 661 |
// all, so we don't need to try in-target for old modules like session_exit. |
| 662 |
|
| 663 |
decltype(&stp_dyninst_exit_status) get_exit_status = NULL; |
| 664 |
set_dlsym(get_exit_status, module, "stp_dyninst_exit_status", false); |
| 665 |
if (get_exit_status) |
| 666 |
{ |
| 667 |
int status = get_exit_status(); |
| 668 |
if (status != EXIT_SUCCESS) |
| 669 |
return status; |
| 670 |
} |
| 671 |
|
| 672 |
return p_target_error ? EXIT_FAILURE : EXIT_SUCCESS; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
// Find a mutatee which matches the given process, else return NULL |
| 677 |
shared_ptr<mutatee> |
| 678 |
mutator::find_mutatee(BPatch_process* process) |
| 679 |
{ |
| 680 |
for (size_t i = 0; i < mutatees.size(); ++i) |
| 681 |
if (*mutatees[i] == process) |
| 682 |
return mutatees[i]; |
| 683 |
return {}; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
// Callback to respond to dynamically loaded libraries. |
| 688 |
// Check if it matches our targets, and instrument accordingly. |
| 689 |
void |
| 690 |
mutator::dynamic_library_callback(BPatch_thread *thread, |
| 691 |
BPatch_object *object, |
| 692 |
bool load) |
| 693 |
{ |
| 694 |
if (!load || !thread || !object) |
| 695 |
return; |
| 696 |
|
| 697 |
BPatch_process* process = thread->getProcess(); |
| 698 |
staplog(1) << "dlopen \"" << object->name() |
| 699 |
<< "\", pid = " << process->getPid() << endl; |
| 700 |
auto mut = find_mutatee(process); |
| 701 |
if (mut) |
| 702 |
mut->instrument_object_dynprobes(object, targets); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
// Callback to respond to post fork events. Check if it matches our |
| 707 |
// targets, and handle accordingly. |
| 708 |
void |
| 709 |
mutator::post_fork_callback(BPatch_thread *parent, BPatch_thread *child) |
| 710 |
{ |
| 711 |
if (!child || !parent) |
| 712 |
return; |
| 713 |
|
| 714 |
BPatch_process* child_process = child->getProcess(); |
| 715 |
BPatch_process* parent_process = parent->getProcess(); |
| 716 |
|
| 717 |
staplog(1) << "post fork, parent " << parent_process->getPid() |
| 718 |
<< ", child " << child_process->getPid() << endl; |
| 719 |
|
| 720 |
auto mut = find_mutatee(parent_process); |
| 721 |
if (mut) |
| 722 |
{ |
| 723 |
// Clone the mutatee for the new process. |
| 724 |
auto m = make_shared<mutatee>(child_process); |
| 725 |
mutatees.push_back(m); |
| 726 |
m->copy_forked_instrumentation(*mut); |
| 727 |
|
| 728 |
// Trigger any process.begin probes. |
| 729 |
m->begin_callback(child); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
// Callback to respond to exec events. Check if it matches our |
| 735 |
// targets, and handle accordingly. |
| 736 |
void |
| 737 |
mutator::exec_callback(BPatch_thread *thread) |
| 738 |
{ |
| 739 |
if (!thread) |
| 740 |
return; |
| 741 |
|
| 742 |
BPatch_process* process = thread->getProcess(); |
| 743 |
|
| 744 |
staplog(1) << "exec, pid = " << process->getPid() << endl; |
| 745 |
|
| 746 |
auto mut = find_mutatee(process); |
| 747 |
if (mut) |
| 748 |
{ |
| 749 |
// Clear previous instrumentation |
| 750 |
mut->exec_reset_instrumentation(); |
| 751 |
|
| 752 |
// NB: Until Dyninst commit 2b6c10ac15dc (in 8.2), loadLibrary in a |
| 753 |
// fork-execed would hang waiting for a stopped process to continue. |
| 754 |
#ifdef DYNINST_8_2 |
| 755 |
// Load our module again in the new process |
| 756 |
if (mut->load_stap_dso(module_name)) |
| 757 |
{ |
| 758 |
if (!targets.empty()) |
| 759 |
mut->instrument_dynprobes(targets); |
| 760 |
|
| 761 |
// Now we map the shared-memory into the target |
| 762 |
if (!module_shmem.empty()) |
| 763 |
{ |
| 764 |
vector<BPatch_snippet *> args; |
| 765 |
args.push_back(new BPatch_constExpr(module_shmem.c_str())); |
| 766 |
mut->call_function("stp_dyninst_shm_connect", args); |
| 767 |
} |
| 768 |
|
| 769 |
// Trigger any process.end probes for the pre-exec process. |
| 770 |
mut->exit_callback(thread, true); |
| 771 |
|
| 772 |
// Trigger any process.begin probes. |
| 773 |
mut->begin_callback(thread); |
| 774 |
} |
| 775 |
#endif |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
void |
| 781 |
mutator::exit_callback(BPatch_thread *thread, |
| 782 |
BPatch_exitType type __attribute__((unused))) |
| 783 |
{ |
| 784 |
if (!thread) |
| 785 |
return; |
| 786 |
|
| 787 |
// 'thread' is the thread that requested the exit, not necessarily the |
| 788 |
// main thread. |
| 789 |
BPatch_process* process = thread->getProcess(); |
| 790 |
int pid = process->getPid(); |
| 791 |
staplog(1) << "exit callback, pid = " << pid << endl; |
| 792 |
|
| 793 |
auto mut = find_mutatee(process); |
| 794 |
if (mut) |
| 795 |
{ |
| 796 |
// FIXME: We'd like to call the mutatee's exit_callback() |
| 797 |
// function, but we've got a problem. The mutatee can't stop the |
| 798 |
// process to call the exit probe within the target (it finishes |
| 799 |
// exiting before we can). So, we'll call the probe(s) locally |
| 800 |
// here. This works, but the context is wrong (the mutator, not |
| 801 |
// the mutatee). |
| 802 |
const vector<dynprobe_location>& proc_end_probes = |
| 803 |
mut->find_attached_probes(STAPDYN_PROBE_FLAG_PROC_END); |
| 804 |
if (proc_end_probes.empty()) |
| 805 |
return; |
| 806 |
|
| 807 |
if (utrace_enter_fn == NULL) |
| 808 |
try |
| 809 |
{ |
| 810 |
set_dlsym(utrace_enter_fn, module, "enter_dyninst_utrace_probe"); |
| 811 |
} |
| 812 |
catch (runtime_error& e) |
| 813 |
{ |
| 814 |
staperror() << e.what() << endl; |
| 815 |
return; |
| 816 |
} |
| 817 |
|
| 818 |
staplog(2) << "firing " << proc_end_probes.size() |
| 819 |
<< " process.end probes in the mutator for pid " |
| 820 |
<< pid << endl; |
| 821 |
for (size_t p = 0; p < proc_end_probes.size(); ++p) |
| 822 |
{ |
| 823 |
const dynprobe_location& probe = proc_end_probes[p]; |
| 824 |
staplog(3) << "calling utrace function in the mutator for pid " |
| 825 |
<< pid << ", probe index " << probe.index << endl; |
| 826 |
int rc = utrace_enter_fn(probe.index, NULL); |
| 827 |
if (rc) |
| 828 |
stapwarn() << "enter_dyninst_utrace_probe returned " |
| 829 |
<< rc << endl; |
| 830 |
} |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
void |
| 836 |
mutator::thread_create_callback(BPatch_process *proc, BPatch_thread *thread) |
| 837 |
{ |
| 838 |
if (!proc || !thread) |
| 839 |
return; |
| 840 |
|
| 841 |
auto mut = find_mutatee(proc); |
| 842 |
if (mut) |
| 843 |
mut->thread_callback(thread, true); |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
void |
| 848 |
mutator::thread_destroy_callback(BPatch_process *proc, BPatch_thread *thread) |
| 849 |
{ |
| 850 |
if (!proc || !thread) |
| 851 |
return; |
| 852 |
|
| 853 |
auto mut = find_mutatee(proc); |
| 854 |
if (mut) |
| 855 |
mut->thread_callback(thread, false); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
// Callback to respond to signals. |
| 860 |
void |
| 861 |
mutator::signal_callback(int signal) |
| 862 |
{ |
| 863 |
sigaddset(&signals_received, signal); |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |