Skip to content

Commit

Permalink
simplify coding
Browse files Browse the repository at this point in the history
  • Loading branch information
P403n1x87 committed Aug 29, 2023
1 parent 3512439 commit edfce19
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 207 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ jobs:
source .venv/bin/activate
pip install --upgrade pip
pip install -r test/requirements.txt
.venv/bin/pytest --pastebin=failed --no-flaky-report -sr fE -n auto || true
sudo -E env PATH="$PATH" .venv/bin/pytest --pastebin=failed --no-flaky-report -sr fE -n auto || true
.venv/bin/pytest --pastebin=failed -sr fE -n auto || true
sudo -E env PATH="$PATH" .venv/bin/pytest --pastebin=failed -sr fE -n auto || true
deactivate
- name: Generate Cobertura report
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ jobs:
run: |
ulimit -c unlimited
source .venv/bin/activate
sudo -E env PATH="$PATH" .venv/bin/pytest --pastebin=failed --no-flaky-report -svr a
.venv/bin/pytest --pastebin=failed --no-flaky-report -svr a
sudo -E env PATH="$PATH" .venv/bin/pytest --pastebin=failed -svr a
.venv/bin/pytest --pastebin=failed -svr a
deactivate
wheels-linux:
Expand Down Expand Up @@ -235,8 +235,8 @@ jobs:
- name: Run tests
run: |
source .venv/bin/activate
sudo -E pytest --ignore=test/cunit --pastebin=failed --no-flaky-report -svr a
pytest --ignore=test/cunit --pastebin=failed --no-flaky-report -svr a -k _darwin
sudo -E pytest --ignore=test/cunit --pastebin=failed -svr a
pytest --ignore=test/cunit --pastebin=failed -svr a -k _darwin
deactivate
wheels-osx:
Expand Down Expand Up @@ -338,7 +338,7 @@ jobs:
- name: Run tests
run: |
venv\Scripts\Activate.ps1
python -m pytest --ignore=test\cunit --pastebin=failed --no-flaky-report -svr a
python -m pytest --ignore=test\cunit --pastebin=failed -svr a
deactivate
wheels-win:
Expand Down
12 changes: 7 additions & 5 deletions src/austin.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ do_child_processes(py_proc_t * py_proc) {
// enough so we can try to attach to child processes straight away.
// TODO: In the future, we might want to consider adding the option to wait
// for child processes, as they might be spawned only much later.
pargs.timeout = 1;
pargs.timeout = 100000; // 0.1s

// Store the PID before it gets deleted by the update.
pid_t ppid = py_proc->pid;
Expand Down Expand Up @@ -215,8 +215,13 @@ do_child_processes(py_proc_t * py_proc) {
static inline int
handle_error() {
int retval = 0;

log_d("Last error: %d :: %s", austin_errno, get_last_error());
if (is_fatal(austin_errno)) {

if (interrupt == SIGTERM) {
retval = SIGTERM;

Check warning on line 222 in src/austin.c

View check run for this annotation

Codecov / codecov/patch

src/austin.c#L222

Added line #L222 was not covered by tests
}
else {
retval = austin_errno;

switch(retval) {
Expand Down Expand Up @@ -255,9 +260,6 @@ handle_error() {
}
}

if (interrupt == SIGTERM)
retval = SIGTERM;

return retval;
} /* handle_error */

Expand Down
6 changes: 3 additions & 3 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const char * _error_msg_tab[MAXERROR] = {
"Failed to create thread object",
"Failed to get top frame for thread",
"Invalid thread",
NULL,
"No next thread",
NULL,
NULL,
NULL,
Expand All @@ -88,7 +88,7 @@ const int _fatal_error_tab[MAXERROR] = {
// generic error messages
0,
1,
0,
1,
1,
0,
1,
Expand Down Expand Up @@ -126,7 +126,7 @@ const int _fatal_error_tab[MAXERROR] = {
0,

// py_proc_t
1,
0,
1,
1,
1,
Expand Down
1 change: 1 addition & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#define ETHREAD ((3 << 3) + 0)
#define ETHREADNOFRAME ((3 << 3) + 1)
#define ETHREADINV ((3 << 3) + 2)
#define ETHREADNONEXT ((3 << 3) + 3)

// py_proc_t
#define EPROC ((4 << 3) + 0)
Expand Down
37 changes: 20 additions & 17 deletions src/linux/py_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
#define BIN_MAP (1 << 0)
#define DYNSYM_MAP (1 << 1)
#define RODATA_MAP (1 << 2)
#define HEAP_MAP (1 << 3)
#define BSS_MAP (1 << 4)
#define BSS_MAP (1 << 3)


// Get the offset of the ith section header
Expand Down Expand Up @@ -361,8 +360,8 @@ _py_proc__parse_maps_file(py_proc_t * self) {
sfree(self->bin_path);
sfree(self->lib_path);

self->map.elf.base = NULL;
self->map.elf.size = 0;
self->map.exe.base = NULL;
self->map.exe.size = 0;

sprintf(file_name, "/proc/%d/exe", self->pid);

Expand Down Expand Up @@ -403,6 +402,7 @@ _py_proc__parse_maps_file(py_proc_t * self) {
size_t page_size = getpagesize();
map->bss_base = (void *) lower - page_size;
map->bss_size = upper - lower + page_size;
maps_flag |= BSS_MAP;
log_d("Inferred BSS for %s: %lx-%lx", map->path, lower, upper);
}

Expand All @@ -416,16 +416,6 @@ _py_proc__parse_maps_file(py_proc_t * self) {
if ((void *) upper > self->max_raddr) self->max_raddr = (void *) upper;
}

if ((maps_flag & HEAP_MAP) == 0 && strstr(line, "[heap]\n") != NULL) {
self->map.heap.base = (void *) lower;
self->map.heap.size = upper - lower;

maps_flag |= HEAP_MAP;

log_d("HEAP bounds " ADDR_FMT "-" ADDR_FMT, lower, upper);
continue;
}

if (pathname[0] == '[')
continue;

Expand Down Expand Up @@ -524,13 +514,26 @@ _py_proc__parse_maps_file(py_proc_t * self) {
for (int i = 0; i < MAP_COUNT; i++) {
map = &(pd->maps[i]);
if (map->has_symbols) {
self->map.elf.base = map->base;
self->map.elf.size = map->size;
self->map.exe.base = map->base;
self->map.exe.size = map->size;
maps_flag |= BIN_MAP;
self->sym_loaded = TRUE;
break;
}
}

if (!(maps_flag & BIN_MAP) && !isvalid(pd->maps[MAP_LIBNEEDLE].path)) {
// We don't have symbols and we don't have a needle path so it's quite
// unlikely that we can work out a Python version in this case.
if (isvalid(pd->maps[MAP_BIN].path) && strstr(pd->maps[MAP_BIN].path, "python")) {
log_d("No symbols but binary seems to be Python.");
maps_flag |= BIN_MAP;
} else {
log_d("No symbols and no needle path. Giving up.");
FAIL;
}
}

// Work out BSS map
int map_index = isvalid(pd->maps[MAP_LIBSYM].path) ? MAP_LIBSYM : MAP_BIN;
self->map.bss.base = pd->maps[map_index].bss_base;
Expand All @@ -539,7 +542,7 @@ _py_proc__parse_maps_file(py_proc_t * self) {
log_d("BSS map %d from %s @ %p", map_index, pd->maps[map_index].path, self->map.bss.base);
log_d("VM maps parsing result: bin=%s lib=%s flags=%d", self->bin_path, self->lib_path, maps_flag);

return maps_flag != (BIN_MAP | HEAP_MAP);
return !(maps_flag & (BIN_MAP | BSS_MAP));
} /* _py_proc__parse_maps_file */


Expand Down
27 changes: 11 additions & 16 deletions src/mac/py_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ _py_proc__analyze_macho32(py_proc_t * self, void * base, void * map) {
next_lc(cmd);
}

if (self->sym_loaded > 0)
if (self->sym_loaded)
bin_attrs |= B_SYMBOLS;

return bin_attrs;
Expand Down Expand Up @@ -396,7 +396,7 @@ _py_proc__analyze_macho(py_proc_t * self, char * path, void * base, mach_vm_size
return _py_proc__analyze_fat(self, base, map_addr);

default:
self->sym_loaded = 0;
self->sym_loaded = FALSE;
}

return 0;
Expand Down Expand Up @@ -434,8 +434,7 @@ pid_to_task(pid_t pid) {
kern_return_t result;

if (fail(check_pid(pid))) {
log_d("No such process: %d", pid);
set_error(EPROCNPID);
log_d("Process ID %d is not valid", pid);
return 0;
}

Expand Down Expand Up @@ -473,9 +472,6 @@ _py_proc__get_maps(py_proc_t * self) {
self->min_raddr = (void *) -1;
self->max_raddr = NULL;

self->map.heap.base = NULL;
self->map.heap.size = 0;

sfree(self->bin_path);
sfree(self->lib_path);

Expand Down Expand Up @@ -607,16 +603,10 @@ _py_proc__get_maps(py_proc_t * self) {
}

next:
// Make a best guess for the heap boundary.
if (self->map.heap.base == NULL)
self->map.heap.base = (void *) address;
self->map.heap.size += size;

address += size;
}

log_d("BSS bounds [%p - %p]", self->map.bss.base, self->map.bss.base + self->map.bss.size);
log_d("HEAP bounds [%p - %p]", self->map.heap.base, self->map.heap.base + self->map.heap.size);

// If the library map is not valid, use the needle map
if (!isvalid(pd->maps[MAP_LIBSYM].path)) {
Expand All @@ -632,8 +622,8 @@ _py_proc__get_maps(py_proc_t * self) {
for (int i = 0; i < MAP_COUNT; i++) {
map = &(pd->maps[i]);
if (map->has_symbols) {
self->map.elf.base = map->base;
self->map.elf.size = map->size;
self->map.exe.base = map->base;
self->map.exe.size = map->size;
self->sym_loaded = TRUE;
break;
}
Expand All @@ -644,7 +634,12 @@ _py_proc__get_maps(py_proc_t * self) {
self->map.bss.base = pd->maps[map_index].bss_base;
self->map.bss.size = pd->maps[map_index].bss_size;

return !self->sym_loaded;
if (!self->sym_loaded) {
set_error(EPROC);
FAIL;
}

SUCCESS;
} // _py_proc__get_maps


Expand Down
16 changes: 9 additions & 7 deletions src/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
#include "error.h"
#include "logging.h"

#define OUT_OF_BOUND -1


/**
* Copy a data structure from the given remote address structure.
Expand Down Expand Up @@ -174,11 +172,15 @@ copy_memory(proc_ref_t proc_ref, void * addr, ssize_t len, void * buf) {
// the PID that was used must have been valid. Therefore this call can only
// fail if the process no longer exists. However, if the return value is
// MACH_SEND_INVALID_DEST, we probably tried an invalid memory area.
if (kr != MACH_SEND_INVALID_DEST) {
set_error(EPROCNPID);
}
else {
set_error(EMEMCOPY);
switch(kr) {
case KERN_PROTECTION_FAILURE:
set_error(EPROCPERM);
break;
case KERN_INVALID_ARGUMENT:
set_error(EPROCNPID);
break;
default:
set_error(EMEMCOPY);
}
FAIL;
}
Expand Down
Loading

0 comments on commit edfce19

Please sign in to comment.