Skip to content

Commit

Permalink
ultask: --jmp: dump original data first
Browse files Browse the repository at this point in the history
The Original data use to restore --jmp.

    $ ./tests/hello/hello

    $ ultask -p $(pidof hello) --jmp from=0x000000004012e3,to=0x00000000401283
    Original data bytes: f3 0f 1e fa 55 48 89 e5 48 83 ec 10 48 89 7d f8

Signed-off-by: Rong Tao <rtoax@foxmail.com>
  • Loading branch information
Rtoax committed Jan 4, 2025
1 parent abeb4e6 commit b19abc0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/tests/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ TEST(Utils_str, memshow, 0)
return 0;
}

TEST(Utils_str, fmembytes, 0)
{
return fmembytes(stdout, docs, sizeof(docs));
}

TEST(Utils_str, print, 0)
{
print_string_hex(stdout, NULL, (void *)docs, sizeof(docs));
Expand Down
12 changes: 12 additions & 0 deletions src/ultask.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,18 @@ int ultask(int argc, char *argv[])
new_insn = (void *)&jmp_entry;
insn_sz = sizeof(struct jmp_table_entry);

char buf[sizeof(struct jmp_table_entry)] = {};

n = memcpy_from_task(target_task, buf, jmp_addr_from, insn_sz);
if (n == -1 || n < insn_sz) {
ulp_error("failed read target process.\n");
ret = -1;
goto done;
}

fprintf(stdout, "Original data bytes: ");
fmembytes(stdout, buf, insn_sz);

n = memcpy_to_task(target_task, jmp_addr_from, new_insn,
insn_sz);
if (n == -1 || n < insn_sz) {
Expand Down
15 changes: 14 additions & 1 deletion src/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ int print_bytes(FILE *fp, void *mem, size_t len)
return ret;
}

int fmembytes(FILE *fp, const void *data, int data_len)
{
int i;
const uint8_t *b;

if (!fp)
fp = stdout;

for (i = 0, b = data; i < data_len; i++, b++)
fprintf(fp, "%02x%s", *b, i < (data_len - 1) ? " " : "");
fprintf(fp, "\n");
return 0;
}

/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise. */
int ulp_startswith(const char *str, const char *prefix)
{
Expand Down Expand Up @@ -201,4 +215,3 @@ unsigned long str2addr(const char *str)

return addr;
}

1 change: 1 addition & 0 deletions src/utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ void free_strstr_list(struct list_head *list);
unsigned long str2size(const char *str);
unsigned long str2addr(const char *str);

int fmembytes(FILE *fp, const void *data, int data_len);

#define strstr_for_each_node_safe(iter, tmp, list) \
list_for_each_entry_safe(iter, tmp, list, node)
Expand Down
3 changes: 3 additions & 0 deletions tests/hello/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void sig_handler(int sig)
}
}

/**
* This maybe use to test 'ultash --jmp ...'
*/
void patch_hello2(unsigned long ul)
{
printf("Hello World. %ld, %ld, patched\n", count, ul);
Expand Down

0 comments on commit b19abc0

Please sign in to comment.