| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-bus.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "analyze.h" |
| 10 |
#include "analyze-unit-gdb.h" |
| 11 |
#include "bus-error.h" |
| 12 |
#include "bus-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "pidref.h" |
| 15 |
#include "process-util.h" |
| 16 |
#include "runtime-scope.h" |
| 17 |
#include "signal-util.h" |
| 18 |
#include "strv.h" |
| 19 |
#include "unit-def.h" |
| 20 |
#include "unit-name.h" |
| 21 |
|
| 22 |
int verb_unit_gdb(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 23 |
static const struct sigaction sa = { |
| 24 |
.sa_sigaction = sigterm_process_group_handler, |
| 25 |
.sa_flags = SA_SIGINFO, |
| 26 |
}; |
| 27 |
|
| 28 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 29 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 30 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 31 |
_cleanup_free_ char *unit = NULL; |
| 32 |
int r; |
| 33 |
|
| 34 |
if (arg_transport != BUS_TRANSPORT_LOCAL) |
| 35 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot spawn a debugger for a remote service."); |
| 36 |
|
| 37 |
r = unit_name_mangle_with_suffix(argv[1], "as unit", UNIT_NAME_MANGLE_WARN, ".service", &unit); |
| 38 |
if (r < 0) |
| 39 |
return log_error_errno(r, "Failed to mangle name '%s': %m", argv[1]); |
| 40 |
|
| 41 |
r = acquire_bus(&bus, /* use_full_bus= */ NULL); |
| 42 |
if (r < 0) |
| 43 |
return bus_log_connect_error(r, arg_transport, arg_runtime_scope); |
| 44 |
|
| 45 |
_cleanup_free_ char *object = unit_dbus_path_from_name(unit); |
| 46 |
if (!object) |
| 47 |
return log_oom(); |
| 48 |
|
| 49 |
r = sd_bus_get_property( |
| 50 |
bus, |
| 51 |
"org.freedesktop.systemd1", |
| 52 |
object, |
| 53 |
"org.freedesktop.systemd1.Service", |
| 54 |
"MainPID", |
| 55 |
&error, |
| 56 |
&reply, |
| 57 |
"u"); |
| 58 |
if (r < 0) |
| 59 |
return log_error_errno(r, "Failed to get the main PID of %s: %s", unit, bus_error_message(&error, r)); |
| 60 |
|
| 61 |
pid_t pid; |
| 62 |
r = sd_bus_message_read(reply, "u", &pid); |
| 63 |
if (r < 0) |
| 64 |
return log_error_errno(r, "Failed to read the main PID of %s from reply: %m", unit); |
| 65 |
if (pid == 0) |
| 66 |
return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Unit %s has no MainPID (hint: inactive?)", unit); |
| 67 |
|
| 68 |
if (!arg_debugger) { |
| 69 |
arg_debugger = strdup(secure_getenv("SYSTEMD_DEBUGGER") ?: "gdb"); |
| 70 |
if (!arg_debugger) |
| 71 |
return log_oom(); |
| 72 |
} |
| 73 |
|
| 74 |
if (!STR_IN_SET(arg_debugger, "gdb", "lldb")) |
| 75 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The debugger must be either 'gdb' or 'lldb'."); |
| 76 |
|
| 77 |
_cleanup_strv_free_ char **debugger_call = NULL; |
| 78 |
r = strv_extend(&debugger_call, arg_debugger); |
| 79 |
if (r < 0) |
| 80 |
return log_oom(); |
| 81 |
|
| 82 |
if (streq(arg_debugger, "gdb")) { |
| 83 |
r = strv_extendf(&debugger_call, "--pid=" PID_FMT, pid); |
| 84 |
if (r < 0) |
| 85 |
return log_oom(); |
| 86 |
|
| 87 |
if (arg_root) { |
| 88 |
_cleanup_free_ char *sysroot_cmd = strjoin("set sysroot ", arg_root); |
| 89 |
r = strv_extend_many(&debugger_call, "-iex", sysroot_cmd); |
| 90 |
if (r < 0) |
| 91 |
return log_oom(); |
| 92 |
} |
| 93 |
|
| 94 |
} else if (streq(arg_debugger, "lldb")) { |
| 95 |
r = strv_extendf(&debugger_call, "--attach-pid=" PID_FMT, pid); |
| 96 |
if (r < 0) |
| 97 |
return log_oom(); |
| 98 |
|
| 99 |
if (arg_root) { |
| 100 |
_cleanup_free_ char *sysroot_cmd = strjoin("platform select --sysroot ", arg_root, " host"); |
| 101 |
r = strv_extend_many(&debugger_call, "-O", sysroot_cmd); |
| 102 |
if (r < 0) |
| 103 |
return log_oom(); |
| 104 |
} |
| 105 |
} else |
| 106 |
assert_not_reached(); |
| 107 |
|
| 108 |
r = strv_extend_strv(&debugger_call, arg_debugger_args, /* filter_duplicates= */ false); |
| 109 |
if (r < 0) |
| 110 |
return log_oom(); |
| 111 |
|
| 112 |
/* Don't interfere with debugger and its handling of SIGINT. */ |
| 113 |
(void) ignore_signals(SIGINT); |
| 114 |
(void) sigaction(SIGTERM, &sa, NULL); |
| 115 |
|
| 116 |
_cleanup_free_ char *fork_name = strjoin("(", debugger_call[0], ")"); |
| 117 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 118 |
r = pidref_safe_fork(fork_name, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pidref); |
| 119 |
if (r < 0) |
| 120 |
return log_error_errno(r, "Fork failed: %m"); |
| 121 |
|
| 122 |
if (r == 0) { |
| 123 |
(void) execvp(debugger_call[0], debugger_call); |
| 124 |
log_error_errno(errno, "Failed to invoke '%s': %m", debugger_call[0]); |
| 125 |
_exit(EXIT_FAILURE); |
| 126 |
} |
| 127 |
|
| 128 |
return pidref_wait_for_terminate_and_check( |
| 129 |
debugger_call[0], |
| 130 |
&pidref, |
| 131 |
WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS); |
| 132 |
} |