Code cleanup, somewhat related to windows code

This commit is contained in:
pancake 2021-08-27 02:46:18 +02:00 committed by pancake
parent 3159cccc6a
commit ba375f5702
15 changed files with 87 additions and 83 deletions

View File

@ -138,7 +138,7 @@ fail:
}
static bool load_buffer(RBinFile *bf, void **bin_obj, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
r_return_val_if_fail (bf && buf, NULL);
r_return_val_if_fail (bf && buf, false);
const ut64 la = bf->loadaddr;
ut64 sz = 0;
const ut8 *bytes = r_buf_data (buf, &sz);

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2018 - pancake, jduck, TheLemonMan, saucec0de */
/* radare - LGPL - Copyright 2009-2021 - pancake, jduck, TheLemonMan, saucec0de */
#include <r_debug.h>
#include <r_drx.h>
@ -11,13 +11,11 @@ R_LIB_VERSION(r_debug);
#define DBG_BUF_SIZE 512
R_API RDebugInfo *r_debug_info(RDebug *dbg, const char *arg) {
if (!dbg || !dbg->h || !dbg->h->info) {
return NULL;
}
r_return_val_if_fail (dbg && dbg->h, NULL);
if (dbg->pid < 0) {
return NULL;
}
return dbg->h->info (dbg, arg);
return dbg->h->info? dbg->h->info (dbg, arg): NULL;
}
R_API void r_debug_info_free(RDebugInfo *rdi) {
@ -326,7 +324,7 @@ R_API RBreakpointItem *r_debug_bp_add(RDebug *dbg, ut64 addr, int hw, bool watch
}
}
if (watch) {
hw = 1; //XXX
hw = 1; // XXX
bpi = r_bp_watch_add (dbg->bp, addr, bpsz, hw, rw);
} else {
bpi = hw
@ -347,7 +345,7 @@ R_API RBreakpointItem *r_debug_bp_add(RDebug *dbg, ut64 addr, int hw, bool watch
static const char *r_debug_str_callback(RNum *userptr, ut64 off, int *ok) {
// RDebug *dbg = (RDebug *)userptr;
eprintf ("STR CALLBACK WTF WTF WTF\n");
// TODO: implement the rnum callback for str or just get rid of it as we dont need it
return NULL;
}
@ -413,7 +411,7 @@ R_API void r_debug_tracenodes_reset(RDebug *dbg) {
sdb_reset (dbg->tracenodes);
}
R_API RDebug *r_debug_free(RDebug *dbg) {
R_API void r_debug_free(RDebug *dbg) {
if (dbg) {
// TODO: free it correctly.. we must ensure this is an instance and not a reference..
r_bp_free (dbg->bp);
@ -440,7 +438,6 @@ R_API RDebug *r_debug_free(RDebug *dbg) {
free (dbg->glob_unlibs);
free (dbg);
}
return NULL;
}
R_API bool r_debug_attach(RDebug *dbg, int pid) {

View File

@ -1,3 +1,5 @@
/* radare - LGPL - Copyright 2019-2021 - gustavo */
#include "windows_maps.h"
#include "../windows/windows_debug.h"
@ -131,14 +133,11 @@ static int set_mod_inf(HANDLE h_proc, RDebugMap *map, RWinModInfo *mod) {
IMAGE_DOS_HEADER *dos_hdr;
IMAGE_NT_HEADERS *nt_hdrs;
IMAGE_NT_HEADERS32 *nt_hdrs32;
IMAGE_SECTION_HEADER *sect_hdr;
IMAGE_SECTION_HEADER *sect_hdr = NULL;
ut8 pe_hdr[0x1000];
SIZE_T len;
int mod_inf_fill;
SIZE_T len = 0;
int mod_inf_fill = 1;
len = 0;
sect_hdr = NULL;
mod_inf_fill = -1;
ReadProcessMemory (h_proc, (LPCVOID)(size_t)map->addr, (LPVOID)pe_hdr, sizeof (pe_hdr), &len);
if (len == (SIZE_T)sizeof (pe_hdr) && is_pe_hdr (pe_hdr)) {
dos_hdr = (IMAGE_DOS_HEADER *)pe_hdr;
@ -247,6 +246,7 @@ static void proc_mem_map(HANDLE h_proc, RList *map_list, MEMORY_BASIC_INFORMATIO
}
R_API RList *r_w32_dbg_maps(RDebug *dbg) {
r_return_val_if_fail (dbg, NULL);
if (dbg->pid == -1) {
return NULL;
}
@ -274,6 +274,7 @@ R_API RList *r_w32_dbg_maps(RDebug *dbg) {
break;
default:
add_map_reg (map_list, "", &mbi);
break;
}
}
cur_addr = (LPVOID)(size_t)((ut64)(size_t)mbi.BaseAddress + mbi.RegionSize);

View File

@ -1,14 +1,23 @@
/* radare - LGPL - Copyright 2010-2021 - pancake, xvilka, gustavo */
#include "w32.h"
// TODO: please kill those globals
#define PLIB_MAX 512
LPVOID lstLib = 0;
PLIB_ITEM lstLibPtr = 0;
LPVOID lstThread = 0;
PTHREAD_ITEM lstThreadPtr = 0;
#if 0
static HANDLE w32_t2h(pid_t tid) {
TH_INFO *th = get_th (tid);
if(!th) {
if (!th) {
/* refresh thread list */
w32_dbg_threads (tid);
/* try to search thread */
if(!(th = get_th (tid)))
if (!(th = get_th (tid)))
return NULL;
}
return th->ht;
@ -91,7 +100,7 @@ static char *get_w32_excep_name(unsigned long code) {
return desc;
}
static int debug_exception_event (DEBUG_EVENT *de) {
static bool debug_exception_event(DEBUG_EVENT *de) {
unsigned long code = de->u.Exception.ExceptionRecord.ExceptionCode;
switch (code) {
/* fatal exceptions */
@ -109,13 +118,13 @@ static int debug_exception_event (DEBUG_EVENT *de) {
case 0x406D1388:
eprintf ("(%d) MS_VC_EXCEPTION (%x) in thread %d\n",
(int)de->dwProcessId, (int)code, (int)de->dwThreadId);
return 1;
return true;
default:
eprintf ("(%d) Unknown exception %x in thread %d\n",
(int)de->dwProcessId, (int)code, (int)de->dwThreadId);
break;
}
return 0;
return false;
}
static char *get_file_name_from_handle (HANDLE handle_file) {
@ -147,7 +156,7 @@ static char *get_file_name_from_handle (HANDLE handle_file) {
goto err_get_file_name_from_handle;
}
TCHAR name[MAX_PATH];
TCHAR drive[3] = TEXT (" :");
TCHAR drive[3] = TEXT (" :");
LPTSTR cur_drive = temp_buffer;
while (*cur_drive) {
/* Look up each device name */
@ -184,23 +193,22 @@ err_get_file_name_from_handle:
}
return NULL;
}
LPVOID lstLib = 0;
PLIB_ITEM lstLibPtr = 0;
/*
static char * r_debug_get_dll(void) {
return lstLibPtr->Path;
}
*/
static PLIB_ITEM r_debug_get_lib_item(void) {
static PLIB_ITEM r_debug_get_lib_item(void) {
return lstLibPtr;
}
#define PLIB_MAX 512
static void r_debug_lstLibAdd(DWORD pid,LPVOID lpBaseOfDll, HANDLE hFile,char * dllname) {
int x;
if (lstLib == 0)
if (lstLib == 0) {
lstLib = VirtualAlloc (0, PLIB_MAX * sizeof (LIB_ITEM), MEM_COMMIT, PAGE_READWRITE);
}
lstLibPtr = (PLIB_ITEM)lstLib;
for (x=0; x<PLIB_MAX; x++) {
for (x = 0; x < PLIB_MAX; x++) {
if (!lstLibPtr->hFile) {
lstLibPtr->pid = pid;
lstLibPtr->hFile = hFile; //DBGEvent->u.LoadDll.hFile;
@ -208,7 +216,7 @@ static void r_debug_lstLibAdd(DWORD pid,LPVOID lpBaseOfDll, HANDLE hFile,char *
strncpy (lstLibPtr->Path,dllname,MAX_PATH-1);
int i = strlen (dllname);
int n = i;
while(dllname[i] != '\\' && i >= 0) {
while (dllname[i] != '\\' && i >= 0) {
i--;
}
strncpy (lstLibPtr->Name, &dllname[i+1], n-i);
@ -216,33 +224,37 @@ static void r_debug_lstLibAdd(DWORD pid,LPVOID lpBaseOfDll, HANDLE hFile,char *
}
lstLibPtr++;
}
eprintf("r_debug_lstLibAdd: Cannot find slot\n");
eprintf ("r_debug_lstLibAdd: Cannot find slot\n");
}
static void * r_debug_findlib (void * BaseOfDll) {
static void * r_debug_findlib(void * BaseOfDll) {
PLIB_ITEM libPtr = NULL;
if (lstLib) {
libPtr = (PLIB_ITEM)lstLib;
while (libPtr->hFile != NULL) {
if (libPtr->hFile != (HANDLE)-1)
if (libPtr->BaseOfDll == BaseOfDll)
while (libPtr->hFile) {
if (libPtr->hFile != (HANDLE)-1) {
if (libPtr->BaseOfDll == BaseOfDll) {
return ((void*)libPtr);
}
}
libPtr = (PLIB_ITEM)((ULONG_PTR)libPtr + sizeof (LIB_ITEM));
}
}
return NULL;
}
LPVOID lstThread = 0;
PTHREAD_ITEM lstThreadPtr = 0;
static PTHREAD_ITEM r_debug_get_thread_item (void) {
static PTHREAD_ITEM r_debug_get_thread_item (void) {
return lstThreadPtr;
}
#define CONST_ThreadQuerySetWin32StartAddress 9
#define PTHREAD_MAX 1024
static void r_debug_lstThreadAdd (DWORD pid, DWORD tid, HANDLE hThread, LPVOID lpThreadLocalBase, LPVOID lpStartAddress, BOOL bFinished) {
int x;
PVOID startAddress = 0;
if (lstThread == 0)
if (lstThread == 0) {
lstThread = VirtualAlloc (0, PTHREAD_MAX * sizeof (THREAD_ITEM), MEM_COMMIT, PAGE_READWRITE);
}
lstThreadPtr = (PTHREAD_ITEM)lstThread;
for (x = 0; x < PTHREAD_MAX; x++) {
if (!lstThreadPtr->tid) {
@ -280,10 +292,10 @@ static void * r_debug_findthread (int pid, int tid) {
int w32_dbg_wait(RDebug *dbg, int pid) {
DEBUG_EVENT de;
int tid, next_event = 0;
bool next_event = false;
unsigned int code;
char *dllname = NULL;
int ret = R_DEBUG_REASON_UNKNOWN;
int tid, ret = R_DEBUG_REASON_UNKNOWN;
static int exited_already = 0;
/* handle debug events */
do {
@ -308,7 +320,7 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
pid, w32_h2t (de.u.CreateProcessInfo.hProcess),
de.u.CreateProcessInfo.lpStartAddress);
r_debug_native_continue (dbg, pid, tid, -1);
next_event = 1;
next_event = true;
ret = R_DEBUG_REASON_NEW_PID;
break;
case EXIT_PROCESS_DEBUG_EVENT:
@ -318,7 +330,7 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
(int)de.u.ExitProcess.dwExitCode);
r_cons_flush ();
//debug_load();
next_event = 0;
next_event = false;
exited_already = pid;
ret = R_DEBUG_REASON_EXIT_PID;
break;
@ -327,7 +339,7 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
r_debug_lstThreadAdd (pid, tid, de.u.CreateThread.hThread, de.u.CreateThread.lpThreadLocalBase, de.u.CreateThread.lpStartAddress, FALSE);
//r_debug_native_continue (dbg, pid, tid, -1);
ret = R_DEBUG_REASON_NEW_TID;
next_event = 0;
next_event = false;
break;
case EXIT_THREAD_DEBUG_EVENT:
//eprintf ("(%d) Finished thread %d\n", pid, tid);
@ -339,7 +351,7 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
r_debug_lstThreadAdd (pid, tid, de.u.CreateThread.hThread, de.u.CreateThread.lpThreadLocalBase, de.u.CreateThread.lpStartAddress, TRUE);
}
//r_debug_native_continue (dbg, pid, tid, -1);
next_event = 0;
next_event = false;
ret = R_DEBUG_REASON_EXIT_TID;
break;
case LOAD_DLL_DEBUG_EVENT:
@ -349,7 +361,7 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
if (dllname) {
free (dllname);
}
next_event = 0;
next_event = false;
ret = R_DEBUG_REASON_NEW_LIB;
break;
case UNLOAD_DLL_DEBUG_EVENT:
@ -362,7 +374,7 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
if (dllname)
free (dllname);
}
next_event = 0;
next_event = false;
ret = R_DEBUG_REASON_EXIT_LIB;
break;
case OUTPUT_DEBUG_STRING_EVENT:
@ -371,14 +383,14 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
r_cons_flush ();
r_debug_native_continue (dbg, pid, tid, -1);
next_event = 1;
next_event = true;
break;
case RIP_EVENT:
//eprintf ("(%d) RIP event\n", pid);
r_cons_printf ("(%d) RIP event\n", pid);
r_cons_flush ();
r_debug_native_continue (dbg, pid, tid, -1);
next_event = 1;
next_event = true;
// XXX unknown ret = R_DEBUG_REASON_TRAP;
break;
case EXCEPTION_DEBUG_EVENT:
@ -388,22 +400,22 @@ int w32_dbg_wait(RDebug *dbg, int pid) {
#endif
case EXCEPTION_BREAKPOINT:
ret = R_DEBUG_REASON_BREAKPOINT;
next_event = 0;
next_event = false;
break;
#if _WIN64
case 0x4000001e: /* STATUS_WX86_SINGLE_STEP */
#endif
case EXCEPTION_SINGLE_STEP:
ret = R_DEBUG_REASON_STEP;
next_event = 0;
next_event = false;
break;
default:
if (!debug_exception_event (&de)) {
ret = R_DEBUG_REASON_TRAP;
next_event = 0;
next_event = false;
}
else {
next_event = 1;
next_event = true;
r_debug_native_continue (dbg, pid, tid, -1);
}
@ -525,10 +537,7 @@ RList *w32_pids(int pid, RList *list) {
return list;
}
do {
if (show_all_pids ||
pe.th32ProcessID == pid ||
pe.th32ParentProcessID == pid) {
if (show_all_pids || pe.th32ProcessID == pid || pe.th32ParentProcessID == pid) {
RDebugPid *debug_pid = build_debug_pid (&pe);
if (debug_pid) {
r_list_append (list, debug_pid);
@ -898,6 +907,9 @@ err_w32_info_exe:
RDebugInfo *w32_info(RDebug *dbg, const char *arg) {
RDebugInfo *rdi = R_NEW0 (RDebugInfo);
if (!rdi) {
return NULL;
}
rdi->status = R_DBG_PROC_SLEEP; // TODO: Fix this
rdi->pid = dbg->pid;
rdi->tid = dbg->tid;
@ -905,10 +917,6 @@ RDebugInfo *w32_info(RDebug *dbg, const char *arg) {
rdi->thread = (void *)r_debug_get_thread_item ();
rdi->uid = -1;
rdi->gid = -1;
rdi->cwd = NULL;
rdi->exe = NULL;
rdi->cmdline = NULL;
rdi->libname = NULL;
w32_info_user (dbg, rdi);
w32_info_exe (dbg, rdi);
return rdi;

View File

@ -462,7 +462,7 @@ static char *__get_file_name_from_handle(HANDLE handle_file) {
goto err_get_file_name_from_handle;
}
TCHAR name[MAX_PATH];
TCHAR drive[3] = TEXT (" :");
TCHAR drive[3] = {' ', ':', 0};
LPTSTR cur_drive = temp_buffer;
while (*cur_drive) {
/* Look up each device name */
@ -534,7 +534,8 @@ static char *__resolve_path(HANDLE ph, HANDLE mh) {
length = tmp - filename;
TCHAR device[MAX_PATH];
char *ret = NULL;
for (TCHAR drv[] = TEXT("A:"); drv[0] <= TEXT ('Z'); drv[0]++) {
TCHAR drv[3] = {'A', ':', 0};
for (; drv[0] <= TEXT ('Z'); drv[0]++) {
if (QueryDosDevice (drv, device, maxlength) > 0) {
if (!_tcsncmp (filename, device, length)) {
TCHAR path[MAX_PATH];
@ -1223,13 +1224,13 @@ static void __w32_info_user(RDebug *dbg, RDebugInfo *rdi) {
r_sys_perror ("__w32_info_user/GetTokenInformation");
goto err___w32_info_user;
}
usr = (LPTSTR)malloc (usr_len * sizeof (TCHAR));
usr = (LPTSTR)calloc (usr_len, sizeof (TCHAR));
if (!usr) {
perror ("__w32_info_user/malloc usr");
goto err___w32_info_user;
}
*usr = '\0';
usr_dom = (LPTSTR)malloc (usr_dom_len * sizeof (TCHAR));
usr_dom = (LPTSTR)calloc (usr_dom_len, sizeof (TCHAR));
if (!usr_dom) {
perror ("__w32_info_user/malloc usr_dom");
goto err___w32_info_user;

View File

@ -426,7 +426,7 @@ typedef struct r_debug_pid_t {
*/
#ifdef R_API
R_API RDebug *r_debug_new(int hard);
R_API RDebug *r_debug_free(RDebug *dbg);
R_API void r_debug_free(RDebug *dbg);
R_API bool r_debug_attach(RDebug *dbg, int pid);
R_API int r_debug_detach(RDebug *dbg, int pid);

View File

@ -35,7 +35,7 @@
#if __sun
#include <sys/types.h>
#else
#if DEBUGGER && HAVE_PTRACE
#if DEBUGGER && HAVE_PTRACE && !__WINDOWS__
#include <sys/ptrace.h>
#endif
#endif

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2016 - pancake */
/* radare - LGPL - Copyright 2008-2021 - pancake */
#include <r_userconf.h>

View File

@ -240,7 +240,8 @@ R_API char *r_file_abspath_rel(const char *cwd, const char *file) {
PTCHAR f = r_sys_conv_utf8_to_win (file);
int s = GetFullPathName (f, MAX_PATH, abspath, NULL);
if (s > MAX_PATH) {
R_LOG_ERROR ("r_file_abspath/GetFullPathName: Path to file too long.\n");
// R_LOG_ERROR ("r_file_abspath/GetFullPathName: Path to file too long.\n");
eprintf ("r_file_abspath/GetFullPathName: Path to file too long.\n");
} else if (!s) {
r_sys_perror ("r_file_abspath/GetFullPathName");
} else {
@ -1284,7 +1285,6 @@ R_API bool r_file_copy(const char *src, const char *dst) {
PTCHAR s = r_sys_conv_utf8_to_win (src);
PTCHAR d = r_sys_conv_utf8_to_win (dst);
if (!s || !d) {
R_LOG_ERROR ("r_file_copy: Failed to allocate memory\n");
free (s);
free (d);
return false;

View File

@ -51,7 +51,6 @@ R_API void *r_lib_dl_open(const char *libname) {
} else {
libname_ = calloc (MAX_PATH, sizeof (TCHAR));
if (!libname_) {
R_LOG_ERROR ("lib/r_lib_dl_open: Failed to allocate memory.\n");
return NULL;
}
if (!GetModuleFileName (NULL, libname_, MAX_PATH)) {

View File

@ -138,8 +138,7 @@ R_API void r_vlog(const char *funcname, const char *filename,
This function is used by the R_LOG_* preprocessor macros for logging
*/
R_API void r_log(const char *funcname, const char *filename,
ut32 lineno, RLogLevel level, const char *tag, const char *fmtstr, ...) {
R_API void r_log(const char *funcname, const char *filename, ut32 lineno, RLogLevel level, const char *tag, const char *fmtstr, ...) {
va_list args;
va_start (args, fmtstr);

View File

@ -1140,7 +1140,8 @@ R_API char *r_sys_pid_to_path(int pid) {
strncpy (tmp, name, length);
tmp[length] = '\0';
TCHAR device[MAX_PATH];
for (TCHAR drv[] = TEXT("A:"); drv[0] <= TEXT('Z'); drv[0]++) {
TCHAR drv[3] = {'A',':', 0};
for (; drv[0] <= TEXT('Z'); drv[0]++) {
if (QueryDosDevice (drv, device, maxlength) > 0) {
char *dvc = r_sys_conv_win_to_utf8 (device);
if (!dvc) {

View File

@ -1,3 +1,5 @@
/* radare2 - LGPL - Copyright 2021 - pancake */
#include <r_types.h>
#include <r_util.h>
@ -98,7 +100,6 @@ R_API bool r_sys_cmd_str_full_w32(const char *cmd, const char *input, int ilen,
if (sterr) {
*sterr = ReadFromPipe (fe, NULL);
}
if (fi && !CloseHandle (fi)) {
ErrorExit ("PipeIn CloseHandle");
}
@ -121,7 +122,6 @@ R_API bool r_sys_create_child_proc_w32(const char *cmdline, HANDLE in, HANDLE ou
LPTSTR _cmdline_ = malloc (max_length);
if (!_cmdline_) {
R_LOG_ERROR ("Failed to allocate memory\n");
return false;
}
@ -159,14 +159,13 @@ char *ReadFromPipe(HANDLE fh, int *outlen) {
DWORD dwRead;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
char *str;
int strl = 0;
int strsz = BUFSIZE+1;
int strsz = BUFSIZE + 1;
if (outlen) {
*outlen = 0;
}
str = malloc (strsz);
char *str = malloc (strsz);
if (!str) {
return NULL;
}

View File

@ -47,7 +47,7 @@ R_API ut64 r_time_now_mono(void) {
}
R_API char *r_time_stamp_to_str(ut32 timeStamp) {
#ifdef _MSC_VER
#if __WINDOWS__
time_t rawtime;
struct tm *tminfo;
rawtime = (time_t)timeStamp;
@ -195,9 +195,8 @@ R_API int r_print_date_w32(RPrint *p, const ut8 *buf, int len) {
return ret;
}
R_API const char *r_time_to_string (ut64 ts) {
time_t l;
l = ts >> 20;
R_API const char *r_time_to_string(ut64 ts) {
time_t l = ts >> 20;
return r_time_stamp_to_str (l);
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2020 - pancake, nikolai */
/* radare - LGPL - Copyright 2009-2021 - pancake, nikolai */
#include <r_diff.h>