| 1 |
// stapdyn main program |
| 2 |
// Copyright (C) 2012-2017 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 <iostream> |
| 10 |
#include <memory> |
| 11 |
|
| 12 |
extern "C" { |
| 13 |
#include <errno.h> |
| 14 |
#include <getopt.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <sys/stat.h> |
| 17 |
#include <sys/types.h> |
| 18 |
#include <unistd.h> |
| 19 |
#include <wordexp.h> |
| 20 |
} |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "../git_version.h" |
| 24 |
#include "../version.h" |
| 25 |
|
| 26 |
#include "mutator.h" |
| 27 |
#include "dynutil.h" |
| 28 |
|
| 29 |
|
| 30 |
#define _STRINGIFY(s) #s |
| 31 |
#define STRINGIFY(s) _STRINGIFY(s) |
| 32 |
#define DYNINST_FULL_VERSION \ |
| 33 |
( STRINGIFY(DYNINST_MAJOR) "." \ |
| 34 |
STRINGIFY(DYNINST_MINOR) "." \ |
| 35 |
STRINGIFY(DYNINST_SUBMINOR) ) |
| 36 |
|
| 37 |
|
| 38 |
using namespace std; |
| 39 |
|
| 40 |
|
| 41 |
// Print the obligatory usage message. |
| 42 |
static void __attribute__ ((noreturn)) |
| 43 |
usage (int rc) |
| 44 |
{ |
| 45 |
cout << "Usage: " << program_invocation_short_name |
| 46 |
<< " MODULE [-v] [-c CMD | -x PID] [-o FILE] [-C WHEN] [globalname=value ...] [-V] [-h]" << endl |
| 47 |
<< "-v Increase verbosity." << endl |
| 48 |
<< "-w Suppress warnings from the script." << endl |
| 49 |
<< "-c cmd Command \'cmd\' will be run and " << program_invocation_short_name << " will" << endl |
| 50 |
<< " exit when it does. The '_stp_target' variable" << endl |
| 51 |
<< " will contain the pid for the command." << endl |
| 52 |
<< "-x pid Sets the '_stp_target' variable to pid." << endl |
| 53 |
<< "-o FILE Send output to FILE. This supports strftime(3)" << endl |
| 54 |
<< " formats for FILE." << endl |
| 55 |
<< "-C WHEN Enable colored errors. WHEN must be either 'auto'," << endl |
| 56 |
<< " 'never', or 'always'. Set to 'auto' by default." << endl |
| 57 |
<< "-V Show version." << endl |
| 58 |
<< "-h Show this help text." << endl; |
| 59 |
|
| 60 |
exit (rc); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
// This is main, the heart and soul of stapdyn, oh yeah! |
| 65 |
int |
| 66 |
main(int argc, char * const argv[]) |
| 67 |
{ |
| 68 |
pid_t pid = 0; |
| 69 |
const char* command = NULL; |
| 70 |
const char* module = NULL; |
| 71 |
|
| 72 |
// Check if error/warning msgs should be colored |
| 73 |
color_errors = isatty(STDERR_FILENO) |
| 74 |
&& strcmp(getenv("TERM") ?: "notdumb", "dumb"); |
| 75 |
|
| 76 |
// First, option parsing. |
| 77 |
int opt; |
| 78 |
while ((opt = getopt (argc, argv, "c:x:vwo:VhC:")) != -1) |
| 79 |
{ |
| 80 |
switch (opt) |
| 81 |
{ |
| 82 |
case 'c': |
| 83 |
command = optarg; |
| 84 |
break; |
| 85 |
|
| 86 |
case 'x': |
| 87 |
pid = atoi(optarg); |
| 88 |
break; |
| 89 |
|
| 90 |
case 'v': |
| 91 |
++stapdyn_log_level; |
| 92 |
break; |
| 93 |
|
| 94 |
case 'w': |
| 95 |
stapdyn_suppress_warnings = true; |
| 96 |
break; |
| 97 |
|
| 98 |
case 'o': |
| 99 |
stapdyn_outfile_name = optarg; |
| 100 |
break; |
| 101 |
|
| 102 |
case 'V': |
| 103 |
printf("Systemtap Dyninst loader/runner (version %s/%s, %s)\n" |
| 104 |
"Copyright (C) 2012-2025 Red Hat, Inc. and others\n" // PRERELEASE |
| 105 |
"This is free software; see the source for copying conditions.\n", |
| 106 |
VERSION, DYNINST_FULL_VERSION, STAP_EXTENDED_VERSION); |
| 107 |
return 0; |
| 108 |
case 'h': |
| 109 |
usage(0); |
| 110 |
break; |
| 111 |
case 'C': |
| 112 |
if (!strcmp(optarg, "never")) |
| 113 |
color_errors = false; |
| 114 |
else if (!strcmp(optarg, "auto")) |
| 115 |
color_errors = isatty(STDERR_FILENO) |
| 116 |
&& strcmp(getenv("TERM") ?: "notdumb", "dumb"); |
| 117 |
else if (!strcmp(optarg, "always")) |
| 118 |
color_errors = true; |
| 119 |
else { |
| 120 |
staperror() << "Invalid option '" << optarg << "' for -C." << endl; |
| 121 |
usage (1); |
| 122 |
} |
| 123 |
break; |
| 124 |
default: |
| 125 |
usage (1); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// The first non-option is our stap module, required. |
| 130 |
if (optind < argc) |
| 131 |
module = argv[optind++]; |
| 132 |
|
| 133 |
// Remaining non-options, if any, specify global variables. |
| 134 |
vector<string> modoptions; |
| 135 |
while (optind < argc) |
| 136 |
{ |
| 137 |
modoptions.push_back(string(argv[optind++])); |
| 138 |
} |
| 139 |
|
| 140 |
if (!module || (command && pid)) |
| 141 |
usage (1); |
| 142 |
|
| 143 |
// Make sure that environment variables and selinux are set ok. |
| 144 |
if (!check_dyninst_rt()) |
| 145 |
return 1; |
| 146 |
if (!check_dyninst_sebools(pid != 0)) |
| 147 |
return 1; |
| 148 |
|
| 149 |
unique_ptr<mutator> session(new mutator(module, modoptions)); |
| 150 |
if (!session.get() || !session->load()) |
| 151 |
{ |
| 152 |
staperror() << "Failed to create the mutator!" << endl; |
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
if (command && !session->create_process(command)) |
| 157 |
return 1; |
| 158 |
|
| 159 |
if (pid && !session->attach_process(pid)) |
| 160 |
return 1; |
| 161 |
|
| 162 |
if (!session->run()) |
| 163 |
return 1; |
| 164 |
|
| 165 |
return session->exit_status(); |
| 166 |
} |
| 167 |
|
| 168 |
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |