16 #if __linux__ || __APPLE__ || __WINDOWS__ || __NetBSD__ || __KFBSD__ || __OpenBSD__
17 #define DEBUGGER_SUPPORTED 1
19 #define DEBUGGER_SUPPORTED 0
22 #if DEBUGGER && DEBUGGER_SUPPORTED
23 #define MAGIC_EXIT 123
27 #include <sys/ptrace.h>
28 #include <sys/types.h>
36 #include <sys/types.h>
38 #include <mach/exception_types.h>
39 #include <mach/mach_init.h>
40 #include <mach/mach_port.h>
41 #include <mach/mach_traps.h>
42 #include <mach/task.h>
43 #include <mach/task_info.h>
44 #include <mach/thread_act.h>
45 #include <mach/thread_info.h>
46 #include <mach/vm_map.h>
47 #include <mach-o/loader.h>
48 #include <mach-o/nlist.h>
68 static int setup_tokens(
void) {
73 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &tok)) {
76 tp.PrivilegeCount = 1;
77 if (!LookupPrivilegeValue(
NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid)) {
81 tp.Privileges[0].Attributes = 0;
82 if (!AdjustTokenPrivileges(tok, 0, &tp,
sizeof(tp),
NULL,
NULL)) {
96 struct __createprocess_params {
99 PROCESS_INFORMATION *pi;
103 static int __createprocess_wrap(
void *params) {
104 STARTUPINFO
si = { 0 };
106 struct __createprocess_params *
p = params;
111 static int fork_and_ptraceme(
RzIO *io,
int bits,
const char *
cmd) {
113 PROCESS_INFORMATION pi;
114 STARTUPINFO
si = { 0 };
123 char *cmdline =
NULL;
132 LPTSTR appname_ = rz_sys_conv_utf8_to_win(
argv[0]);
133 LPTSTR cmdline_ = rz_sys_conv_utf8_to_win(cmdline);
137 struct __createprocess_params
p = { appname_, cmdline_, &pi,
flags };
140 wrap->
params.
func.func = __createprocess_wrap;
150 CloseHandle(pi.hThread);
156 pid = pi.dwProcessId;
159 eprintf(
"Spawned new process with pid %d, tid = %d\n",
pid, tid);
162 c->dbg->plugin_data = wrap;
164 int ret =
c->dbg->cur->wait(
c->dbg, pi.dwProcessId);
167 TerminateProcess(pi.hProcess, 1);
169 CloseHandle(pi.hProcess);
172 CloseHandle(pi.hProcess);
177 #if (__APPLE__ && __POWERPC__) || !__APPLE__
179 #if __APPLE__ || __BSD__
180 static void inferior_abort_handler(
int pid) {
181 eprintf(
"Inferior received signal SIGABRT. Executing BKPT.\n");
185 static void trace_me(
void) {
189 #if __APPLE__ || __BSD__
207 #if __APPLE__ && !__POWERPC__
208 static void handle_posix_error(
int err) {
214 eprintf(
"posix_spawnp: Invalid argument\n");
217 eprintf(
"Unsupported architecture. Please specify -b 32\n");
220 eprintf(
"posix_spawnp: unknown error %d\n",
err);
221 perror(
"posix_spawnp");
256 eprintf(
"Can't parse default rz-run profile\n");
263 }
else if (
bits == 32) {
268 eprintf(
"Can't config the environment.\n");
275 #if __APPLE__ && !__POWERPC__
277 static void handle_posix_redirection(
RzRunProfile *
rp, posix_spawn_file_actions_t *fileActions) {
278 const int mode = S_IRUSR | S_IWUSR;
291 static int fork_and_ptraceme_for_mac(
RzIO *io,
int bits,
const char *
cmd) {
294 posix_spawn_file_actions_t fileActions;
295 ut32 ps_flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
300 posix_spawnattr_t attr = { 0 };
301 posix_spawnattr_init(&attr);
303 sigemptyset(&no_signals);
304 sigfillset(&all_signals);
305 posix_spawnattr_setsigmask(&attr, &no_signals);
306 posix_spawnattr_setsigdefault(&attr, &all_signals);
308 posix_spawn_file_actions_init(&fileActions);
309 posix_spawn_file_actions_addinherit_np(&fileActions,
STDIN_FILENO);
310 posix_spawn_file_actions_addinherit_np(&fileActions,
STDOUT_FILENO);
311 posix_spawn_file_actions_addinherit_np(&fileActions,
STDERR_FILENO);
313 ps_flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
314 ps_flags |= POSIX_SPAWN_START_SUSPENDED;
315 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
319 posix_spawn_file_actions_destroy(&fileActions);
325 posix_spawn_file_actions_destroy(&fileActions);
328 handle_posix_redirection(
rp, &fileActions);
331 ps_flags |= _POSIX_SPAWN_DISABLE_ASLR;
334 if (
rp->_bits == 32) {
338 (void)posix_spawnattr_setflags(&attr, ps_flags);
339 posix_spawnattr_setbinpref_np(&attr, 1, &
cpu, &copied);
340 ret = posix_spawnp(&
p,
rp->_args[0], &fileActions, &attr,
rp->_args,
NULL);
341 handle_posix_error(ret);
345 posix_spawn_file_actions_destroy(&fileActions);
350 #if (!(__APPLE__ && !__POWERPC__))
351 typedef struct fork_child_data_t {
357 static void fork_child_callback(
void *user) {
358 fork_child_data *data = user;
376 static int fork_and_ptraceme_for_unix(
RzIO *io,
int bits,
const char *
cmd) {
377 int ret,
status, child_pid;
379 fork_child_data child_data;
382 child_data.cmd =
cmd;
383 child_pid = rz_io_ptrace_fork(io, fork_child_callback, &child_data);
386 perror(
"fork_and_ptraceme");
393 ret = waitpid(child_pid, &
status, WNOHANG);
403 eprintf(
"Process with PID %d started...\n", (
int)child_pid);
404 }
else if (WEXITSTATUS(
status) == MAGIC_EXIT) {
407 kill(child_pid, SIGSTOP);
409 eprintf(
"Killing child process %d due to an error\n", (
int)child_pid);
410 kill(child_pid, SIGSTOP);
418 static int fork_and_ptraceme(
RzIO *io,
int bits,
const char *
cmd) {
423 #if __APPLE__ && !__POWERPC__
424 r = fork_and_ptraceme_for_mac(io,
bits, _eff_cmd);
426 r = fork_and_ptraceme_for_unix(io,
bits, _eff_cmd);
435 if (!strncmp(
file,
"waitfor://", 10)) {
438 if (!strncmp(
file,
"pidof://", 8)) {
441 return (!strncmp(
file,
"dbg://", 6) &&
file[6]);
445 static int get_pid_of(
RzIO *io,
const char *procname) {
447 if (
c &&
c->dbg &&
c->dbg->cur) {
452 rz_list_foreach (pids,
iter,
proc) {
453 if (strstr(
proc->path, procname)) {
459 eprintf(
"Cannot enumerate processes\n");
468 if (!strncmp(
file,
"waitfor://", 10)) {
469 const char *procname =
file + 10;
470 eprintf(
"Waiting for %s\n", procname);
472 int target_pid = get_pid_of(io, procname);
473 if (target_pid != -1) {
474 snprintf(uri,
sizeof(uri),
"dbg://%d", target_pid);
480 }
else if (!strncmp(
file,
"pidof://", 8)) {
481 const char *procname =
file + 8;
482 int target_pid = get_pid_of(io, procname);
483 if (target_pid == -1) {
484 eprintf(
"Cannot find matching process for %s\n",
file);
487 snprintf(uri,
sizeof(uri),
"dbg://%d", target_pid);
505 if (!_plugin || !_plugin->
open) {
508 ret = _plugin->
open(io, uri, rw,
mode);
512 if (!_plugin || !_plugin->
open || !_plugin->
close) {
515 ret = _plugin->
open(io, uri, rw,
mode);
520 if (!_plugin || !_plugin->
open) {
523 ret = _plugin->
open(io, uri, rw,
mode);
528 if (!_plugin || !_plugin->
open) {
531 ret = _plugin->
open(io, uri, rw,
mode);
535 c->dbg->plugin_data = ret->
data;
549 eprintf(
"something went wrong\n");
551 eprintf(
"trying to close %d with io_debug\n",
desc->fd);
560 .desc =
"Attach to native debugger instance",
562 .uris =
"dbg://,pidof://,waitfor://",
573 .desc =
"Debug a program or pid. (NOT SUPPORTED FOR THIS PLATFORM)",
577 #ifndef RZ_PLUGIN_INCORE
int bits(struct state *s, int need)
static RzNumCalcValue expr(RzNum *, RzNumCalc *, int)
RZ_API void * rz_cons_sleep_begin(void)
RZ_API bool rz_cons_is_breaked(void)
RZ_API void rz_cons_sleep_end(void *user)
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
RZ_API void Ht_() free(HtName_(Ht) *ht)
static bool __plugin_open(RzIO *io, const char *pathname, bool many)
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
static int __close(RzIODesc *fd)
RZ_API RzLibStruct rizin_plugin
RzIOPlugin rz_io_plugin_debug
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc kill
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
static static fork const void static count static fd const char const char static newpath char char argv
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
static const char struct stat static buf struct stat static buf static vhangup int status
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid ptrace
@ RZ_DEBUG_REASON_NEW_PID
RZ_API bool rz_file_rm(const char *file)
RZ_API RzIOPlugin * rz_io_plugin_resolve(RzIO *io, const char *filename, bool many)
RZ_API void rz_run_free(RzRunProfile *r)
RZ_API RzRunProfile * rz_run_new(const char *str)
RZ_API bool rz_run_parsefile(RzRunProfile *p, const char *b)
RZ_API bool rz_run_parseline(RzRunProfile *p, const char *b)
RZ_API int rz_run_start(RzRunProfile *p)
RZ_API int rz_run_config_env(RzRunProfile *p)
RZ_API bool rz_run_parse(RzRunProfile *pf, const char *profile)
RZ_API char * rz_str_appendf(char *ptr, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API int rz_str_arg_unescape(char *arg)
RZ_API char ** rz_str_argv(const char *str, int *_argc)
RZ_API void rz_str_argv_free(char **argv)
RZ_API char RZ_API void rz_sys_backtrace(void)
Print the backtrace at the point this function is called from.
RZ_API int rz_sys_clearenv(void)
Clean all environment variables in the calling process.
RZ_API int rz_sys_usleep(int usecs)
Sleep for usecs microseconds.
RZ_API int rz_sys_signal(int sig, void(*handler)(int))
static struct sockaddr static addrlen static backlog const void static flags void flags
int(* detach)(RzDebug *dbg, int pid)
struct rz_debug_plugin_t * cur
struct rz_io_plugin_t * plugin
int(* close)(RzIODesc *desc)
RzIODesc *(* open)(RzIO *io, const char *, int perm, int mode)
int w32dbg_wrap_wait_ret(W32DbgWInst *inst)
#define w32dbgw_err(inst)
#define w32dbgw_ret(inst)
ut64(WINAPI *w32_GetEnabledXStateFeatures)()