| 1 |
// stapdyn mutatee functions |
| 2 |
// Copyright (C) 2012-2019 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 "mutatee.h" |
| 10 |
|
| 11 |
extern "C" { |
| 12 |
#include <signal.h> |
| 13 |
#include <sys/types.h> |
| 14 |
} |
| 15 |
|
| 16 |
#include <dyninst/BPatch_function.h> |
| 17 |
#include <dyninst/BPatch_image.h> |
| 18 |
#include <dyninst/BPatch_module.h> |
| 19 |
#include <dyninst/BPatch_point.h> |
| 20 |
#include <dyninst/BPatch_thread.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 |
// Create snippets for all the DWARF registers, |
| 33 |
// in their architecture-specific order. |
| 34 |
static void |
| 35 |
get_dwarf_registers(BPatch_process *app, |
| 36 |
vector<BPatch_snippet*>& registers) |
| 37 |
{ |
| 38 |
static const char* const names[] = { |
| 39 |
#if defined(__i386__) |
| 40 |
"eax", "ecx", "edx", "ebx", |
| 41 |
"esp", "ebp", "esi", "edi", |
| 42 |
#elif defined(__x86_64__) |
| 43 |
"rax", "rdx", "rcx", "rbx", |
| 44 |
"rsi", "rdi", "rbp", "rsp", |
| 45 |
"r8", "r9", "r10", "r11", |
| 46 |
"r12", "r13", "r14", "r15", |
| 47 |
#elif defined(__powerpc__) || defined(__powerpc64__) |
| 48 |
"r0", "r1", "r2", "r3", |
| 49 |
"r4", "r5", "r6", "r7", |
| 50 |
"r8", "r9", "r10", "r11", |
| 51 |
"r12", "r13", "r14", "r15", |
| 52 |
"r16", "r17", "r18", "r19", |
| 53 |
"r20", "r21", "r22", "r23", |
| 54 |
"r24", "r25", "r26", "r27", |
| 55 |
"r28", "r29", "r30", "r31", |
| 56 |
#elif defined(__aarch64__) |
| 57 |
"r0", "r1", "r2", "r3", |
| 58 |
"r4", "r5", "r6", "r7", |
| 59 |
"r8", "r9", "r10", "r11", |
| 60 |
"r12", "r13", "r14", "r15", |
| 61 |
"r16", "r17", "r18", "r19", |
| 62 |
"r20", "r21", "r22", "r23", |
| 63 |
"r24", "r25", "r26", "r27", |
| 64 |
"r28", "r29", "r30", "r31", |
| 65 |
#endif |
| 66 |
NULL }; |
| 67 |
|
| 68 |
// First push the original PC, before instrumentation mucked anything up. |
| 69 |
// (There's also BPatch_actualAddressExpr for the instrumented result...) |
| 70 |
registers.push_back(new BPatch_originalAddressExpr()); |
| 71 |
|
| 72 |
vector<BPatch_register> bpregs; |
| 73 |
app->getRegisters(bpregs); |
| 74 |
|
| 75 |
ostream& debug_reg = staplog(4); |
| 76 |
debug_reg << "pid " << app->getPid() << " has " |
| 77 |
<< bpregs.size() << " registers available:"; |
| 78 |
for (size_t i = 0; i < bpregs.size(); ++i) |
| 79 |
debug_reg << " " << bpregs[i].name(); |
| 80 |
debug_reg << endl; |
| 81 |
|
| 82 |
// Look for each DWARF register in BPatch's register set. |
| 83 |
// O(m*n) loop, but neither array is very large |
| 84 |
for (const char* const* name = names; *name; ++name) |
| 85 |
{ |
| 86 |
size_t i; |
| 87 |
for (i = 0; i < bpregs.size(); ++i) |
| 88 |
if (bpregs[i].name() == *name) |
| 89 |
{ |
| 90 |
// Found it, add a snippet. |
| 91 |
registers.push_back(new BPatch_registerExpr(bpregs[i])); |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Simple object to temporarily make sure a process is stopped |
| 98 |
class mutatee_freezer { |
| 99 |
mutatee& m; |
| 100 |
bool already_stopped; |
| 101 |
|
| 102 |
public: |
| 103 |
mutatee_freezer(mutatee& m): |
| 104 |
m(m), already_stopped(m.is_stopped()) |
| 105 |
{ |
| 106 |
// If process is currently running, stop it. |
| 107 |
if (!already_stopped && !m.stop_execution()) |
| 108 |
{ |
| 109 |
staplog(3) << "stopping process failed, stopped=" |
| 110 |
<< m.is_stopped() << ", terminated=" |
| 111 |
<< m.is_terminated() << endl; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
~mutatee_freezer() |
| 116 |
{ |
| 117 |
// Let the process continue (if it wasn't stopped when we started). |
| 118 |
if (!already_stopped) |
| 119 |
m.continue_execution(); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
mutatee::mutatee(BPatch_process* process): |
| 125 |
pid(process? process->getPid() : 0), |
| 126 |
process(process), stap_dso(NULL), |
| 127 |
utrace_enter_function(NULL) |
| 128 |
{ |
| 129 |
get_dwarf_registers(process, registers); |
| 130 |
} |
| 131 |
|
| 132 |
mutatee::~mutatee() |
| 133 |
{ |
| 134 |
remove_instrumentation(); |
| 135 |
unload_stap_dso(); |
| 136 |
if (process) |
| 137 |
process->detach(true); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
// Inject the stap module into the target process |
| 142 |
bool |
| 143 |
mutatee::load_stap_dso(const string& filename) |
| 144 |
{ |
| 145 |
stap_dso = process->loadLibrary(filename.c_str()); |
| 146 |
if (!stap_dso) |
| 147 |
{ |
| 148 |
staperror() << "Couldn't load " << filename |
| 149 |
<< " into the target process" << endl; |
| 150 |
return false; |
| 151 |
} |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
// Unload the stap module from the target process |
| 156 |
void |
| 157 |
mutatee::unload_stap_dso() |
| 158 |
{ |
| 159 |
if (!process || !stap_dso) |
| 160 |
return; |
| 161 |
|
| 162 |
// XXX Dyninst has no unloadLibrary() yet, as of 8.0 |
| 163 |
stap_dso = NULL; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
void |
| 169 |
mutatee::update_semaphores(unsigned short delta, size_t start) |
| 170 |
{ |
| 171 |
if (!process || process->isTerminated()) |
| 172 |
return; |
| 173 |
|
| 174 |
for (size_t i=start; i < semaphores.size(); ++i) |
| 175 |
{ |
| 176 |
unsigned short value = 0; |
| 177 |
BPatch_variableExpr* semaphore = semaphores[i]; |
| 178 |
|
| 179 |
// Read-modify-write the semaphore remotely. |
| 180 |
semaphore->readValue(&value, (int)sizeof(value)); |
| 181 |
value += delta; |
| 182 |
semaphore->writeValue(&value, (int)sizeof(value)); |
| 183 |
} |
| 184 |
// NB: Alternatively, we could build up a oneTimeCode snippet to do them all |
| 185 |
// at once within the target process. For now, remote seems good enough. |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
void |
| 190 |
mutatee::call_utrace_dynprobes(const vector<dynprobe_location>& probes, |
| 191 |
BPatch_thread* thread) |
| 192 |
{ |
| 193 |
if (!stap_dso || probes.empty()) |
| 194 |
return; |
| 195 |
|
| 196 |
if (utrace_enter_function == NULL) |
| 197 |
{ |
| 198 |
vector<BPatch_function *> functions; |
| 199 |
stap_dso->findFunction("enter_dyninst_utrace_probe", |
| 200 |
functions); |
| 201 |
if (!functions.empty()) |
| 202 |
utrace_enter_function = functions[0]; |
| 203 |
else |
| 204 |
{ |
| 205 |
staplog(1) << "no utrace enter function in pid " << pid << "!" << endl; |
| 206 |
return; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
for (size_t i = 0; i < probes.size(); ++i) |
| 211 |
{ |
| 212 |
const dynprobe_location& probe = probes[i]; |
| 213 |
vector<BPatch_snippet *> args; |
| 214 |
args.push_back(new BPatch_constExpr((int64_t)probe.index)); |
| 215 |
args.push_back(new BPatch_constExpr((void*)NULL)); // pt_regs |
| 216 |
BPatch_funcCallExpr call(*utrace_enter_function, args); |
| 217 |
staplog(3) << "calling utrace function in pid " << pid |
| 218 |
<< " for probe index " << probe.index << endl; |
| 219 |
if (thread) |
| 220 |
thread->oneTimeCode(call); |
| 221 |
else |
| 222 |
process->oneTimeCode(call); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
// Remember utrace probes. They get handled when the associated |
| 228 |
// callback hits. |
| 229 |
void |
| 230 |
mutatee::instrument_utrace_dynprobe(const dynprobe_location& probe) |
| 231 |
{ |
| 232 |
// Just remember this probe. It will get called from a callback function. |
| 233 |
attached_probes.push_back(probe); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
// Handle "global" targets. They aren't really global, but non-path |
| 238 |
// based probes, like: |
| 239 |
// probe process.begin { ... } |
| 240 |
// probe process(PID).begin { ... } |
| 241 |
void |
| 242 |
mutatee::instrument_global_dynprobe_target(const dynprobe_target& target) |
| 243 |
{ |
| 244 |
staplog(1) << "found global target in pid " << pid << ", inserting " |
| 245 |
<< target.probes.size() << " probes" << endl; |
| 246 |
|
| 247 |
for (size_t j = 0; j < target.probes.size(); ++j) |
| 248 |
{ |
| 249 |
const dynprobe_location& probe = target.probes[j]; |
| 250 |
|
| 251 |
// We already know this isn't a path-based probe. We've got 2 |
| 252 |
// other qualifications here: |
| 253 |
// (1) Make sure this is a utrace probe by checking the flags. |
| 254 |
// (2) If PID was specified, does the pid match? |
| 255 |
if (((probe.flags & (STAPDYN_PROBE_FLAG_PROC_BEGIN |
| 256 |
| STAPDYN_PROBE_FLAG_PROC_END |
| 257 |
| STAPDYN_PROBE_FLAG_THREAD_BEGIN |
| 258 |
| STAPDYN_PROBE_FLAG_THREAD_END)) != 0) |
| 259 |
&& (probe.offset == 0 || (int)probe.offset == process->getPid())) |
| 260 |
instrument_utrace_dynprobe(probe); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
// Given a target and the matching object, instrument all of the probes |
| 266 |
// with calls to the stap_dso's entry function. |
| 267 |
void |
| 268 |
mutatee::instrument_dynprobe_target(BPatch_object* object, |
| 269 |
const dynprobe_target& target) |
| 270 |
{ |
| 271 |
if (!process || !stap_dso || !object) |
| 272 |
return; |
| 273 |
|
| 274 |
vector<BPatch_function *> functions; |
| 275 |
BPatch_function* enter_function = NULL; |
| 276 |
bool use_pt_regs = false; |
| 277 |
|
| 278 |
staplog(1) << "found target \"" << target.path << "\" in pid " << pid |
| 279 |
<< ", inserting " << target.probes.size() << " probes" << endl; |
| 280 |
|
| 281 |
process->beginInsertionSet(); |
| 282 |
for (size_t j = 0; j < target.probes.size(); ++j) |
| 283 |
{ |
| 284 |
const dynprobe_location& probe = target.probes[j]; |
| 285 |
// powerpc64 or arch64 (with r31 patch): 32 registers + IP |
| 286 |
const int ppc_and_aarch_register_count = 33; |
| 287 |
|
| 288 |
if ((probe.flags & (STAPDYN_PROBE_FLAG_PROC_BEGIN |
| 289 |
| STAPDYN_PROBE_FLAG_PROC_END |
| 290 |
| STAPDYN_PROBE_FLAG_THREAD_BEGIN |
| 291 |
| STAPDYN_PROBE_FLAG_THREAD_END)) != 0) |
| 292 |
{ |
| 293 |
instrument_utrace_dynprobe(probe); |
| 294 |
continue; |
| 295 |
} |
| 296 |
|
| 297 |
if (! enter_function) |
| 298 |
{ |
| 299 |
// XXX Until we know how to build pt_regs from here, we'll |
| 300 |
// try the entry function for individual registers first. |
| 301 |
if (!registers.empty()) |
| 302 |
stap_dso->findFunction("enter_dyninst_uprobe_regs", functions, |
| 303 |
false); |
| 304 |
if (!functions.empty()) |
| 305 |
enter_function = functions[0]; |
| 306 |
|
| 307 |
// If the other entry wasn't found, or we don't have |
| 308 |
// registers for it anyway, try the form that takes pt_regs* |
| 309 |
// and we'll just pass NULL. |
| 310 |
if (!enter_function) |
| 311 |
{ |
| 312 |
stap_dso->findFunction("enter_dyninst_uprobe", functions, |
| 313 |
false); |
| 314 |
if (functions.empty()) |
| 315 |
{ |
| 316 |
stapwarn() << "Couldn't find the uprobe entry function (either " << endl |
| 317 |
<< "\"enter_dyninst_uprobe_regs\" or \"enter_dyninst_uprobe\"). Uprobe probes" |
| 318 |
<< endl << "disabled." << endl; |
| 319 |
return; |
| 320 |
} |
| 321 |
use_pt_regs = true; |
| 322 |
enter_function = functions[0]; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
// Convert the file offset to a memory address. |
| 327 |
Dyninst::Address address = object->fileOffsetToAddr(probe.offset); |
| 328 |
if (address == BPatch_object::E_OUT_OF_BOUNDS) |
| 329 |
{ |
| 330 |
stapwarn() << "Couldn't convert " << target.path << "+" |
| 331 |
<< lex_cast_hex(probe.offset) << " to an address" << endl; |
| 332 |
continue; |
| 333 |
} |
| 334 |
|
| 335 |
// Turn the address into instrumentation points. |
| 336 |
// NB: There may be multiple results if Dyninst determined that multiple |
| 337 |
// concrete functions have overlapping ranges. Rare, but possible. |
| 338 |
vector<BPatch_point*> points; |
| 339 |
object->findPoints(address, points); |
| 340 |
if (points.empty()) |
| 341 |
{ |
| 342 |
stapwarn() << "Couldn't find an instrumentation point at " |
| 343 |
<< lex_cast_hex(address) << ", " << target.path |
| 344 |
<< "+" << lex_cast_hex(probe.offset) << endl; |
| 345 |
continue; |
| 346 |
} |
| 347 |
|
| 348 |
// Check that the functions containing each point are actually |
| 349 |
// instrumentable. Unfortunately, findPoints doesn't make any |
| 350 |
// distinction, and insertSnippet doesn't tell us either, so this |
| 351 |
// is the best way we have to let the user know. |
| 352 |
vector<BPatch_point*> instrumentable_points; |
| 353 |
for (size_t i = 0; i < points.size(); ++i) |
| 354 |
if (points[i]->getFunction()->isInstrumentable()) |
| 355 |
instrumentable_points.push_back(points[i]); |
| 356 |
else |
| 357 |
stapwarn() << "Couldn't instrument the function containing " |
| 358 |
<< lex_cast_hex(address) << ", " << target.path |
| 359 |
<< "+" << lex_cast_hex(probe.offset) << endl; |
| 360 |
points.swap(instrumentable_points); |
| 361 |
|
| 362 |
if (probe.return_p) |
| 363 |
{ |
| 364 |
// Transform the address points into function exits |
| 365 |
vector<BPatch_point*> return_points; |
| 366 |
for (size_t i = 0; i < points.size(); ++i) |
| 367 |
{ |
| 368 |
vector<BPatch_point*>* exits = |
| 369 |
points[i]->getFunction()->findPoint(BPatch_locExit); |
| 370 |
if (!exits || exits->empty()) |
| 371 |
{ |
| 372 |
stapwarn() << "Couldn't find a return point from " |
| 373 |
<< lex_cast_hex(address) << ", " << target.path |
| 374 |
<< "+" << lex_cast_hex(probe.offset) << endl; |
| 375 |
continue; |
| 376 |
} |
| 377 |
return_points.insert(return_points.end(), |
| 378 |
exits->begin(), exits->end()); |
| 379 |
} |
| 380 |
points.swap(return_points); |
| 381 |
} |
| 382 |
|
| 383 |
if (points.empty()) |
| 384 |
continue; |
| 385 |
|
| 386 |
// The entry function needs the index of this particular probe, then |
| 387 |
// the registers in whatever form we chose above. |
| 388 |
vector<BPatch_snippet *> args; |
| 389 |
args.push_back(new BPatch_constExpr((int64_t)probe.index)); |
| 390 |
if (use_pt_regs) { |
| 391 |
args.push_back(new BPatch_constExpr((void*)NULL)); // pt_regs |
| 392 |
BPatch_funcCallExpr call(*enter_function, args); |
| 393 |
BPatchSnippetHandle* handle = process->insertSnippet(call, points); |
| 394 |
if (handle) |
| 395 |
snippets.push_back(handle); |
| 396 |
} |
| 397 |
else if (registers.size() == ppc_and_aarch_register_count) |
| 398 |
{ |
| 399 |
// Save registers in chunks of six. |
| 400 |
vector<BPatch_function *> functions; |
| 401 |
stap_dso->findFunction("enter_dyninst_uprobe_partial_regs", functions, false); |
| 402 |
if (functions.empty()) |
| 403 |
{ |
| 404 |
stapwarn() << "Couldn't find the uprobe entry function \"enter_dyninst_uprobe_partial_regs\"" |
| 405 |
<< endl << "disabled." << endl; |
| 406 |
return; |
| 407 |
} |
| 408 |
BPatch_function* enter_function_partial_regs = functions[0]; |
| 409 |
args.insert(args.end(), 7, new BPatch_constExpr((int64_t)0)); |
| 410 |
|
| 411 |
bool ip_added = false; |
| 412 |
long unsigned first_reg [] = {0, 6, 12, 18, 24, 30, 32}; |
| 413 |
for (int first = (sizeof(first_reg)/sizeof(long))-2; first >= 0; first--) |
| 414 |
{ |
| 415 |
args[1] = new BPatch_constExpr((int64_t)first_reg[first]); |
| 416 |
int argidx = 2; |
| 417 |
for (long unsigned regidx = first_reg[first]; regidx < first_reg[first+1]; regidx++) |
| 418 |
{ |
| 419 |
args[argidx++] = registers[regidx+1]; |
| 420 |
} |
| 421 |
// registers[0] is BPatch_originalAddressExpr; append to the end |
| 422 |
if (!ip_added) { |
| 423 |
ip_added = true; |
| 424 |
args[argidx] = registers[0]; |
| 425 |
} |
| 426 |
BPatch_funcCallExpr call(*enter_function_partial_regs, args); |
| 427 |
BPatchSnippetHandle* handle = process->insertSnippet(call, points); |
| 428 |
if (handle) |
| 429 |
snippets.push_back(handle); |
| 430 |
} |
| 431 |
} |
| 432 |
else { |
| 433 |
#if (defined(__powerpc__) || defined(__powerpc64__)) || defined(__aarch64__) |
| 434 |
// In EmitterPOWER::emitCall(), Dyninst only uses register parm args. |
| 435 |
// The case above should handle this except for older aarch64 backends |
| 436 |
// which supported neither multiple calls nor the sp register |
| 437 |
while (registers.size() > 6) |
| 438 |
{ |
| 439 |
delete registers.back(); |
| 440 |
registers.pop_back(); |
| 441 |
} |
| 442 |
#endif |
| 443 |
args.push_back(new BPatch_constExpr((unsigned long)registers.size())); |
| 444 |
args.insert(args.end(), registers.begin(), registers.end()); |
| 445 |
|
| 446 |
BPatch_funcCallExpr call(*enter_function, args); |
| 447 |
BPatchSnippetHandle* handle = process->insertSnippet(call, points); |
| 448 |
if (handle) |
| 449 |
snippets.push_back(handle); |
| 450 |
} |
| 451 |
|
| 452 |
// Update SDT semaphores as needed. |
| 453 |
if (probe.semaphore) |
| 454 |
{ |
| 455 |
Dyninst::Address sem_address = object->fileOffsetToAddr(probe.semaphore); |
| 456 |
if (sem_address == BPatch_object::E_OUT_OF_BOUNDS) |
| 457 |
stapwarn() << "Couldn't convert semaphore " << target.path << "+" |
| 458 |
<< lex_cast_hex(probe.offset) << " to an address" << endl; |
| 459 |
else |
| 460 |
{ |
| 461 |
// Create a variable to represent this semaphore |
| 462 |
BPatch_type *sem_type = process->getImage()->findType("unsigned short"); |
| 463 |
BPatch_variableExpr *semaphore = process->createVariable(sem_address, sem_type); |
| 464 |
if (semaphore) |
| 465 |
semaphores.push_back(semaphore); |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
process->finalizeInsertionSet(false); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
// Look for "global" (non-path based) probes and handle them. |
| 474 |
void |
| 475 |
mutatee::instrument_global_dynprobes(const vector<dynprobe_target>& targets) |
| 476 |
{ |
| 477 |
if (!process || !stap_dso || targets.empty()) |
| 478 |
return; |
| 479 |
|
| 480 |
// Look for global (non path-based probes), and remember them. |
| 481 |
for (size_t i = 0; i < targets.size(); ++i) |
| 482 |
{ |
| 483 |
const dynprobe_target& target = targets[i]; |
| 484 |
|
| 485 |
// Do the real work... |
| 486 |
if (target.path.empty()) |
| 487 |
instrument_global_dynprobe_target(target); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
// Look for all matches between this object and the targets |
| 493 |
// we want to probe, then do the instrumentation. |
| 494 |
void |
| 495 |
mutatee::instrument_object_dynprobes(BPatch_object* object, |
| 496 |
const vector<dynprobe_target>& targets) |
| 497 |
{ |
| 498 |
if (!process || !stap_dso || !object || targets.empty()) |
| 499 |
return; |
| 500 |
|
| 501 |
// We want to map objects by their full path, but the pathName from |
| 502 |
// Dyninst might be relative, so fill it out. |
| 503 |
string path = resolve_path(object->pathName()); |
| 504 |
staplog(2) << "found object \"" << path << "\" in pid " << pid << endl; |
| 505 |
|
| 506 |
size_t semaphore_start = semaphores.size(); |
| 507 |
|
| 508 |
// Match the object to our targets, and instrument matches. |
| 509 |
for (size_t i = 0; i < targets.size(); ++i) |
| 510 |
{ |
| 511 |
const dynprobe_target& target = targets[i]; |
| 512 |
|
| 513 |
// Do the real work... |
| 514 |
if (path == target.path) |
| 515 |
instrument_dynprobe_target(object, target); |
| 516 |
} |
| 517 |
|
| 518 |
// Increment new semaphores |
| 519 |
update_semaphores(1, semaphore_start); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
void |
| 524 |
mutatee::begin_callback(BPatch_thread *thread) |
| 525 |
{ |
| 526 |
const vector<dynprobe_location>& proc_begin_probes = |
| 527 |
find_attached_probes(STAPDYN_PROBE_FLAG_PROC_BEGIN); |
| 528 |
|
| 529 |
// Shortcut out if there aren't any relevant probes |
| 530 |
if (proc_begin_probes.empty()) |
| 531 |
return; |
| 532 |
|
| 533 |
// process->oneTimeCode() requires that the process be stopped |
| 534 |
mutatee_freezer mf(*this); |
| 535 |
if (!is_stopped()) |
| 536 |
return; |
| 537 |
|
| 538 |
staplog(2) << "firing " << proc_begin_probes.size() |
| 539 |
<< " process.begin probes in pid " << pid << endl; |
| 540 |
call_utrace_dynprobes(proc_begin_probes, thread); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
// FIXME: When dyninst's registerExitCallback() hits, it's too late |
| 545 |
// to stop the process and inject oneTimeCode() for process.end. |
| 546 |
// So while the code here works for post-exec "end", for now the |
| 547 |
// mutator::exit_callback() will run its probes locally in stapdyn. |
| 548 |
void |
| 549 |
mutatee::exit_callback(BPatch_thread *thread, bool exec_p) |
| 550 |
{ |
| 551 |
const vector<dynprobe_location>& proc_end_probes = |
| 552 |
exec_p ? exec_proc_end_probes |
| 553 |
: find_attached_probes(STAPDYN_PROBE_FLAG_PROC_END); |
| 554 |
|
| 555 |
// Shortcut out if there aren't any relevant probes |
| 556 |
if (proc_end_probes.empty()) |
| 557 |
return; |
| 558 |
|
| 559 |
// thread->oneTimeCode() requires that the process (not just the |
| 560 |
// thread) be stopped. So, stop the process if needed. |
| 561 |
mutatee_freezer mf(*this); |
| 562 |
if (!is_stopped()) |
| 563 |
return; |
| 564 |
|
| 565 |
staplog(2) << "firing " << proc_end_probes.size() |
| 566 |
<< " process.end probes in pid " << pid << endl; |
| 567 |
call_utrace_dynprobes(proc_end_probes, thread); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
void |
| 572 |
mutatee::thread_callback(BPatch_thread *thread, bool create_p) |
| 573 |
{ |
| 574 |
const vector<dynprobe_location>& probes = |
| 575 |
find_attached_probes(create_p |
| 576 |
? STAPDYN_PROBE_FLAG_THREAD_BEGIN |
| 577 |
: STAPDYN_PROBE_FLAG_THREAD_END); |
| 578 |
|
| 579 |
// Shortcut out if there aren't any relevant probes |
| 580 |
if (probes.empty()) |
| 581 |
return; |
| 582 |
|
| 583 |
// If 'thread' is the main process, just return. We can't stop the |
| 584 |
// process before it terminates. |
| 585 |
if (thread->getLWP() == process->getPid()) |
| 586 |
return; |
| 587 |
|
| 588 |
// thread->oneTimeCode() requires that the process (not just the |
| 589 |
// thread) be stopped. So, stop the process if needed. |
| 590 |
mutatee_freezer mf(*this); |
| 591 |
if (!is_stopped()) |
| 592 |
return; |
| 593 |
|
| 594 |
staplog(2) << "firing " << probes.size() |
| 595 |
<< " process.thread." << (create_p ? "begin" : "end") |
| 596 |
<< " probes in pid " << pid << endl; |
| 597 |
call_utrace_dynprobes(probes, thread); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
vector<dynprobe_location> |
| 602 |
mutatee::find_attached_probes(uint64_t flag) |
| 603 |
{ |
| 604 |
vector<dynprobe_location> probes; |
| 605 |
for (size_t i = 0; i < attached_probes.size(); ++i) |
| 606 |
{ |
| 607 |
const dynprobe_location& probe = attached_probes[i]; |
| 608 |
if (probe.flags & flag) |
| 609 |
probes.push_back(probe); |
| 610 |
} |
| 611 |
return probes; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
// Look for probe matches in all objects. |
| 616 |
void |
| 617 |
mutatee::instrument_dynprobes(const vector<dynprobe_target>& targets) |
| 618 |
{ |
| 619 |
if (!process || !stap_dso || targets.empty()) |
| 620 |
return; |
| 621 |
|
| 622 |
BPatch_image* image = process->getImage(); |
| 623 |
if (!image) |
| 624 |
return; |
| 625 |
|
| 626 |
// Match non object/path specific probes. |
| 627 |
instrument_global_dynprobes(targets); |
| 628 |
|
| 629 |
// Read all of the objects in the process. |
| 630 |
vector<BPatch_object *> objects; |
| 631 |
image->getObjects(objects); |
| 632 |
for (size_t i = 0; i < objects.size(); ++i) |
| 633 |
instrument_object_dynprobes(objects[i], targets); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
// Copy data for forked instrumentation |
| 638 |
void |
| 639 |
mutatee::copy_forked_instrumentation(mutatee& other) |
| 640 |
{ |
| 641 |
if (!process) |
| 642 |
return; |
| 643 |
|
| 644 |
// Freeze both processes, so we have a stable base. |
| 645 |
mutatee_freezer mf_parent(other), mf(*this); |
| 646 |
|
| 647 |
// Find the same stap module in the fork |
| 648 |
if (other.stap_dso) |
| 649 |
{ |
| 650 |
BPatch_image* image = process->getImage(); |
| 651 |
if (!image) |
| 652 |
return; |
| 653 |
|
| 654 |
string dso_path = other.stap_dso->pathName(); |
| 655 |
|
| 656 |
vector<BPatch_object *> objects; |
| 657 |
image->getObjects(objects); |
| 658 |
for (size_t i = 0; i < objects.size(); ++i) |
| 659 |
if (objects[i]->pathName() == dso_path) |
| 660 |
{ |
| 661 |
stap_dso = objects[i]; |
| 662 |
break; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
// Get new handles for all inserted snippets |
| 667 |
for (size_t i = 0; i < other.snippets.size(); ++i) |
| 668 |
{ |
| 669 |
BPatchSnippetHandle *handle = |
| 670 |
process->getInheritedSnippet(*other.snippets[i]); |
| 671 |
if (handle) |
| 672 |
snippets.push_back(handle); |
| 673 |
} |
| 674 |
|
| 675 |
// Get new variable representations of semaphores |
| 676 |
for (size_t i = 0; i < other.semaphores.size(); ++i) |
| 677 |
{ |
| 678 |
BPatch_variableExpr *semaphore = |
| 679 |
process->getInheritedVariable(*other.semaphores[i]); |
| 680 |
if (semaphore) |
| 681 |
semaphores.push_back(semaphore); |
| 682 |
} |
| 683 |
|
| 684 |
// Update utrace probes to match, except PID-based probes. |
| 685 |
// (A forked PID will never be the same as the parent.) |
| 686 |
for (size_t i = 0; i < other.attached_probes.size(); ++i) |
| 687 |
{ |
| 688 |
const dynprobe_location& probe = other.attached_probes[i]; |
| 689 |
if (probe.offset == 0) |
| 690 |
instrument_utrace_dynprobe(probe); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
// Reset instrumentation after an exec |
| 696 |
void |
| 697 |
mutatee::exec_reset_instrumentation() |
| 698 |
{ |
| 699 |
// Reset members that are now out of date |
| 700 |
stap_dso = NULL; |
| 701 |
snippets.clear(); |
| 702 |
semaphores.clear(); |
| 703 |
|
| 704 |
// NB: the utrace process.end probes are saved, so they can run right |
| 705 |
// before the new process does its process.begin. |
| 706 |
exec_proc_end_probes = find_attached_probes(STAPDYN_PROBE_FLAG_PROC_END); |
| 707 |
|
| 708 |
attached_probes.clear(); |
| 709 |
utrace_enter_function = NULL; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
// Remove all BPatch snippets we've instrumented in the target |
| 714 |
void |
| 715 |
mutatee::remove_instrumentation() |
| 716 |
{ |
| 717 |
if (!process || snippets.empty()) |
| 718 |
return; |
| 719 |
|
| 720 |
process->beginInsertionSet(); |
| 721 |
for (size_t i = 0; i < snippets.size(); ++i) |
| 722 |
process->deleteSnippet(snippets[i]); |
| 723 |
process->finalizeInsertionSet(false); |
| 724 |
snippets.clear(); |
| 725 |
|
| 726 |
// Decrement all semaphores |
| 727 |
update_semaphores(-1); |
| 728 |
semaphores.clear(); |
| 729 |
|
| 730 |
unload_stap_dso(); |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
// Look up a function by name in the target and invoke it without parameters. |
| 735 |
void |
| 736 |
mutatee::call_function(const string& name) |
| 737 |
{ |
| 738 |
vector<BPatch_snippet *> args; |
| 739 |
call_function(name, args); |
| 740 |
} |
| 741 |
|
| 742 |
// Look up a function by name in the target and invoke it with parameters. |
| 743 |
void |
| 744 |
mutatee::call_function(const string& name, |
| 745 |
const vector<BPatch_snippet *>& args) |
| 746 |
{ |
| 747 |
if (!stap_dso) |
| 748 |
return; |
| 749 |
|
| 750 |
// process->oneTimeCode() requires that the process be stopped |
| 751 |
mutatee_freezer mf(*this); |
| 752 |
if (!is_stopped()) |
| 753 |
return; |
| 754 |
|
| 755 |
vector<BPatch_function *> functions; |
| 756 |
stap_dso->findFunction(name.c_str(), functions); |
| 757 |
|
| 758 |
// XXX Dyninst can return multiple results, but we're not really |
| 759 |
// expecting that... should we really call them all anyway? |
| 760 |
for (size_t i = 0; i < functions.size(); ++i) |
| 761 |
{ |
| 762 |
BPatch_funcCallExpr call(*functions[i], args); |
| 763 |
staplog(3) << "calling function '" << name << "' in pid " << pid << endl; |
| 764 |
process->oneTimeCode(call); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
// Send a signal to the process. |
| 770 |
int |
| 771 |
mutatee::kill(int signal) |
| 772 |
{ |
| 773 |
return pid ? ::kill(pid, signal) : -2; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
void |
| 778 |
mutatee::continue_execution() |
| 779 |
{ |
| 780 |
if (is_stopped()) |
| 781 |
{ |
| 782 |
staplog(2) << "continuing execution of pid " << pid << endl; |
| 783 |
process->continueExecution(); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
bool |
| 789 |
mutatee::stop_execution() |
| 790 |
{ |
| 791 |
if (process->isStopped()) |
| 792 |
{ |
| 793 |
// Process is already stopped, no need to do anything else. |
| 794 |
return true; |
| 795 |
} |
| 796 |
|
| 797 |
staplog(2) << "stopping execution of pid " << pid << endl; |
| 798 |
if (! process->stopExecution()) |
| 799 |
{ |
| 800 |
staplog(1) << "stopExecution on pid " << pid << " failed!" << endl; |
| 801 |
return false; |
| 802 |
} |
| 803 |
if (! process->isStopped() || process->isTerminated()) |
| 804 |
{ |
| 805 |
staplog(1) << "couldn't stop pid " << pid << "!" << endl; |
| 806 |
return false; |
| 807 |
} |
| 808 |
return true; |
| 809 |
} |
| 810 |
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |