Skip to content

Commit

Permalink
Fix debugger regression in Android ##debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
radare authored Jan 23, 2019
1 parent 334d0b3 commit 18931ff
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 82 deletions.
11 changes: 11 additions & 0 deletions libr/core/cconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,17 @@ R_API int r_core_config_init(RCore *core) {
SETCB ("dir.prefix", pfx, (RConfigCallback)&cb_dirpfx, "Default prefix r2 was compiled for");
free (pfx);
}
#if __ANDROID__
{ // use dir.home and also adjust check for permissions in directory before choosing a home
char *h = r_sys_getenv (R_SYS_HOME);
if (h) {
if (!strcmp (h, "/")) {
r_sys_setenv (R_SYS_HOME, "/data/local/tmp");
}
free (h);
}
}
#endif
SETPREF ("cmd.times", "", "Run when a command is repeated (number prefix)");
/* pdb */
SETPREF ("pdb.useragent", "Microsoft-Symbol-Server/6.11.0001.402", "User agent for Microsoft symbol server");
Expand Down
2 changes: 1 addition & 1 deletion libr/debug/p/debug_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static int r_debug_native_attach (RDebug *dbg, int pid) {
if (!dbg || pid == dbg->pid)
return dbg->tid;
#endif
#if __linux__
#if __linux__ || __ANDROID__
return linux_attach (dbg, pid);
#elif __WINDOWS__ && !__CYGWIN__
int ret;
Expand Down
130 changes: 59 additions & 71 deletions libr/debug/p/native/linux/linux_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,46 +80,46 @@ int linux_handle_signals (RDebug *dbg) {
// siginfo.si_code -> HWBKPT, USER, KERNEL or WHAT
#warning DO MORE RDEBUGREASON HERE
switch (dbg->reason.signum) {
case SIGTRAP:
{
if (dbg->glob_libs || dbg->glob_unlibs) {
ut64 pc_addr = r_debug_reg_get (dbg, "PC");
RBreakpointItem *b = r_bp_get_at (dbg->bp, pc_addr - dbg->bpsize);
if (b && b->internal) {
char *p = strstr (b->data, "dbg.");
if (p) {
if (r_str_startswith (p, "dbg.libs")) {
const char *name;
if (strstr (b->data, "sym.imp.dlopen")) {
name = r_reg_get_name (dbg->reg, R_REG_NAME_A0);
} else {
name = r_reg_get_name (dbg->reg, R_REG_NAME_A1);
}
b->data = r_str_appendf (b->data, ";ps@r:%s", name);
dbg->reason.type = R_DEBUG_REASON_NEW_LIB;
} else if (r_str_startswith (p, "dbg.unlibs")) {
dbg->reason.type = R_DEBUG_REASON_EXIT_LIB;
case SIGTRAP:
{
if (dbg->glob_libs || dbg->glob_unlibs) {
ut64 pc_addr = r_debug_reg_get (dbg, "PC");
RBreakpointItem *b = r_bp_get_at (dbg->bp, pc_addr - dbg->bpsize);
if (b && b->internal) {
char *p = strstr (b->data, "dbg.");
if (p) {
if (r_str_startswith (p, "dbg.libs")) {
const char *name;
if (strstr (b->data, "sym.imp.dlopen")) {
name = r_reg_get_name (dbg->reg, R_REG_NAME_A0);
} else {
name = r_reg_get_name (dbg->reg, R_REG_NAME_A1);
}
b->data = r_str_appendf (b->data, ";ps@r:%s", name);
dbg->reason.type = R_DEBUG_REASON_NEW_LIB;
} else if (r_str_startswith (p, "dbg.unlibs")) {
dbg->reason.type = R_DEBUG_REASON_EXIT_LIB;
}
}
}
if (dbg->reason.type != R_DEBUG_REASON_NEW_LIB &&
dbg->reason.type != R_DEBUG_REASON_EXIT_LIB) {
dbg->reason.bp_addr = (ut64)(size_t)siginfo.si_addr;
dbg->reason.type = R_DEBUG_REASON_BREAKPOINT;
}
}
break;
case SIGABRT: // 6 / SIGIOT // SIGABRT
dbg->reason.type = R_DEBUG_REASON_ABORT;
break;
case SIGSEGV:
dbg->reason.type = R_DEBUG_REASON_SEGFAULT;
break;
case SIGCHLD:
dbg->reason.type = R_DEBUG_REASON_SIGNAL;
default:
break;
if (dbg->reason.type != R_DEBUG_REASON_NEW_LIB &&
dbg->reason.type != R_DEBUG_REASON_EXIT_LIB) {
dbg->reason.bp_addr = (ut64)(size_t)siginfo.si_addr;
dbg->reason.type = R_DEBUG_REASON_BREAKPOINT;
}
}
break;
case SIGABRT: // 6 / SIGIOT // SIGABRT
dbg->reason.type = R_DEBUG_REASON_ABORT;
break;
case SIGSEGV:
dbg->reason.type = R_DEBUG_REASON_SEGFAULT;
break;
case SIGCHLD:
dbg->reason.type = R_DEBUG_REASON_SIGNAL;
default:
break;
}
if (dbg->reason.signum != SIGTRAP) {
eprintf ("[+] SIGNAL %d errno=%d addr=0x%08"PFMT64x
Expand Down Expand Up @@ -406,10 +406,9 @@ static int linux_stop_process(int pid) {
}

static int linux_attach_single_pid(RDebug *dbg, int ptid) {
int ret = 0;
linux_set_options (dbg, ptid);
ret = r_debug_ptrace (dbg, PTRACE_ATTACH, ptid, NULL, (r_ptrace_data_t)(size_t)NULL);
return ret;
return r_debug_ptrace (dbg, PTRACE_ATTACH, ptid, NULL,
(r_ptrace_data_t)(size_t)NULL);
}

static RList *get_pid_thread_list (RDebug *dbg, int main_pid) {
Expand Down Expand Up @@ -456,7 +455,7 @@ int linux_attach(RDebug *dbg, int pid) {
}
int ret = linux_attach_single_pid (dbg, pid);
if (ret == -1) {
// ignore perror ("ptrace (PT_ATTACH)");
perror ("ptrace (PT_ATTACH)");
}
}
out:
Expand Down Expand Up @@ -678,7 +677,20 @@ void print_fpu (void *f, int r){
int i;
struct user_fpregs_struct fpregs = *(struct user_fpregs_struct*)f;
#if __x86_64__
#if !__ANDROID__
#if __ANDROID__
PRINT_FPU (fpregs);
for (i = 0;i < 8; i++) {
ut64 *b = (ut64 *)&fpregs.st_space[i*4];
ut32 *c = (ut32*)&fpregs.st_space;
float *f = (float *)&fpregs.st_space;
c = c + (i * 4);
f = f + (i * 4);
eprintf ("st%d =%0.3lg (0x%016"PFMT64x") | %0.3f (%08x) | \
%0.3f (%08x) \n", i,
(double)*((double*)&fpregs.st_space[i*4]), *b, (float) f[0],
c[0], (float) f[1], c[1]);
}
#else
eprintf ("---- x86-64 ----\n");
PRINT_FPU (fpregs);
eprintf ("size = 0x%08x\n", (ut32)sizeof (fpregs));
Expand All @@ -701,20 +713,7 @@ void print_fpu (void *f, int r){
eprintf ("\n");
}
}
#else
PRINT_FPU (fpregs);
for (i = 0;i < 8; i++) {
ut64 *b = (ut64 *)&fpregs.st_space[i*4];
ut32 *c = (ut32*)&fpregs.st_space;
float *f = (float *)&fpregs.st_space;
c = c + (i * 4);
f = f + (i * 4);
eprintf ("st%d =%0.3lg (0x%016"PFMT64x") | %0.3f (%08x) | \
%0.3f (%08x) \n", i,
(double)*((double*)&fpregs.st_space[i*4]), *b, (float) f[0],
c[0], (float) f[1], c[1]);
}
#endif // !__ANDROID__
#endif // __ANDROID__
#elif __i386__
if (!r) {
#if !__ANDROID__
Expand Down Expand Up @@ -814,7 +813,6 @@ int linux_reg_read (RDebug *dbg, int type, ut8 *buf, int size) {
struct user_fpregs_struct fpregs;
if (type == R_REG_TYPE_FPU) {
#if __x86_64__
#if !__ANDROID__
ret1 = r_debug_ptrace (dbg, PTRACE_GETFPREGS, pid, NULL, &fpregs);
if (showfpu) {
print_fpu ((void *)&fpregs, 0);
Expand All @@ -827,14 +825,6 @@ int linux_reg_read (RDebug *dbg, int type, ut8 *buf, int size) {
}
memcpy (buf, &fpregs, size);
return sizeof(fpregs);
#else
ret1 = r_debug_ptrace (dbg, PTRACE_GETFPREGS, pid, NULL, &fpregs);
if (showfpu) print_fpu ((void *)&fpregs, 0);
if (ret1 != 0) return false;
if (sizeof(fpregs) < size) size = sizeof(fpregs);
memcpy (buf, &fpregs, size);
return sizeof(fpregs);
#endif // !__ANDROID__
#elif __i386__
#if !__ANDROID__
struct user_fpxregs_struct fpxregs;
Expand Down Expand Up @@ -875,14 +865,16 @@ int linux_reg_read (RDebug *dbg, int type, ut8 *buf, int size) {
memset (&regs, 0, sizeof (regs));
memset (buf, 0, size);
#if __arm64__ || __aarch64__
{
struct iovec io = {
.iov_base = &regs,
.iov_len = sizeof (regs)
};
ret = r_debug_ptrace (dbg, PTRACE_GETREGSET, pid, (void*)(size_t)NT_PRSTATUS, (r_ptrace_data_t)(size_t)&io);
ret = r_debug_ptrace (dbg, PTRACE_GETREGSET, pid, 1, &io);
// ret = ptrace (PTRACE_GETREGSET, pid, (void*)(size_t)(NT_PRSTATUS), NULL); // &io);
if (ret != 0) {
perror("ptrace");
}
#elif __BSD__ && __POWERPC__ || __sparc__
#elif __BSD__ && (__POWERPC__ || __sparc__)
ret = r_debug_ptrace (dbg, PTRACE_GETREGS, pid, &regs, NULL);
#else
/* linux -{arm/x86/x86_64} */
Expand All @@ -897,16 +889,12 @@ int linux_reg_read (RDebug *dbg, int type, ut8 *buf, int size) {
if (ret != 0) {
return false;
}
if (sizeof (regs) < size) {
size = sizeof (regs);
}
memcpy (buf, &regs, size);
memcpy (buf, &regs, R_MIN (sizeof (regs), size));
return sizeof (regs);
}
break;
}
return true;

}

int linux_reg_write (RDebug *dbg, int type, const ut8 *buf, int size) {
Expand Down
8 changes: 7 additions & 1 deletion libr/include/r_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@
#endif
#endif

#if defined(__GLIBC__) && defined(__linux__)
#if (defined(__GLIBC__) && defined(__linux__))
typedef enum __ptrace_request r_ptrace_request_t;
typedef void * r_ptrace_data_t;
#define R_PTRACE_NODATA NULL
#else
#if __ANDROID__
typedef int r_ptrace_request_t;
typedef void * r_ptrace_data_t;
#define R_PTRACE_NODATA NULL
#else
typedef int r_ptrace_request_t;
typedef int r_ptrace_data_t;
#define R_PTRACE_NODATA 0
#endif
#endif
#endif

#if __cplusplus
extern "C" {
Expand Down
1 change: 0 additions & 1 deletion libr/io/p/io_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
w32->winbase = winbase;
w32->tid = wintid;
}

#elif __APPLE__
sprintf (uri, "smach://%d", pid); //s is for spawn
_plugin = r_io_plugin_resolve (io, (const char *)uri + 1, false);
Expand Down
4 changes: 2 additions & 2 deletions libr/io/p/io_procpid.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
int fd, ret = -1;
if (__plugin_open (io, file, 0)) {
int pid = atoi (file + 10);
if (file[0]=='a') {
if (file[0] == 'a') {
ret = ptrace (PTRACE_ATTACH, pid, 0, 0);
if (ret == -1) {
switch (errno) {
Expand Down Expand Up @@ -139,7 +139,7 @@ RIOPlugin r_io_plugin_procpid = {
};

#else
struct r_io_plugin_t r_io_plugin_procpid = {
RIOPlugin r_io_plugin_procpid = {
.name = NULL
};
#endif
Expand Down
16 changes: 10 additions & 6 deletions libr/io/p/io_ptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ static int __read(RIO *io, RIODesc *desc, ut8 *buf, int len) {
/* reopen procpidmem if necessary */
#if USE_PROC_PID_MEM
fd = RIOPTRACE_FD (desc);
if (RIOPTRACE_PID(desc) != RIOPTRACE_OPID(desc)) {
if (fd != -1)
if (RIOPTRACE_PID (desc) != RIOPTRACE_OPID (desc)) {
if (fd != -1) {
close (fd);
}
open_pidmem ((RIOPtrace*)desc->data);
fd = RIOPTRACE_FD (desc);
RIOPTRACE_OPID(desc) = RIOPTRACE_PID(desc);
Expand Down Expand Up @@ -139,8 +140,9 @@ static void open_pidmem (RIOPtrace *iop) {
char pidmem[32];
snprintf (pidmem, sizeof (pidmem), "/proc/%d/mem", iop->pid);
iop->fd = open (pidmem, O_RDWR);
if (iop->fd == -1)
if (iop->fd == -1) {
iop->fd = open (pidmem, O_RDONLY);
}
#if 0
if (iop->fd == -1)
eprintf ("Warning: Cannot open /proc/%d/mem. "
Expand Down Expand Up @@ -171,12 +173,14 @@ static bool __plugin_open(RIO *io, const char *file, bool many) {
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
RIODesc *desc = NULL;
int ret = -1;
if (__plugin_open (io, file,0)) {
int pid = atoi (file+9);
if (__plugin_open (io, file, 0)) {
int pid = atoi (file + 9);
// ret = r_io_ptrace (io, PTRACE_ATTACH, pid, 0, 0);
ret = r_io_ptrace (io, PTRACE_ATTACH, pid, 0, 0);
if (file[0] == 'p') { //ptrace
ret = 0;
} else if (ret == -1) {
} else
if (ret == -1) {
#ifdef __ANDROID__
eprintf ("ptrace_attach: Operation not permitted\n");
#else
Expand Down

0 comments on commit 18931ff

Please sign in to comment.