Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make debugging work a bit better on OpenBSD: nudge get_base_from_maps… #6455

Merged
merged 1 commit into from
Jan 9, 2017
Merged

Conversation

nevun
Copy link
Contributor

@nevun nevun commented Jan 7, 2017

…() so it does not return 0 on OpenBSD just because the memory mapping does not contain the file name.

Copied the fallback code from cmd_debug.c:r_debug_get_baddr().

getBaddrFromDebugger() from radare2.c also does the right thing. r_debug_get_baddr() probably should be exposed through the api so everyone can use one, maybe correct, version.

Before patch:

$ r2 -version
radare2 1.2.0-git 13429 @ openbsd-x86-64 git.1.1.0-142-g7595aad0a
commit: 7595aad0a4608abf26bd5a6032eb01b9f3007735 build: 2017-01-07
$ r2 -d ~/tmp/a.out
Process with PID 87025 started...
= attach 87025 87025
bin.baddr 0x8cd55000000
USING 8cd55000000
Assuming filepath /home/gk/tmp/a.out
asm.bits 64
 -- Documentation is for weak people.
[0x8cfb4a002b0]> aa
[Cannot find function 'entry0' at 0x000003c0 entry0 (aa)
[x] Analyze all flags starting with sym. and entry0 (aa)
[0x8cfb4a002b0]> f~main
0x0000058f 256 main
0x0000058f 44 sym.main
[0x8cfb4a002b0]> s main 
[0x0000058f]> pdf
p: Cannot find function at 0x0000058f
[0x0000058f]>

After patch:

$ r2 -d ~/tmp/a.out
Process with PID 4403 started...
= attach 4403 4403
bin.baddr 0x110b46f00000
USING 110b46f00000
Assuming filepath /home/gk/tmp/a.out
asm.bits 64
 -- -bash: r2: command not found
[0x110dc89002b0]> aa
[x] Analyze all flags starting with sym. and entry0 (aa)
[0x110dc89002b0]> f~main
0x110b46f0058f 256 main
0x110b46f0058f 44 sym.main
[0x110dc89002b0]> s main
[0x110b46f0058f]> pdf
            ;-- main:
/ (fcn) sym.main 44
|   sym.main ();
|              ; CALL XREF from 0x110b46f0041d (entry0)
|           0x110b46f0058f      55             push rbp
|           0x110b46f00590      4889e5         mov rbp, rsp
|           0x110b46f00593      4883ec10       sub rsp, 0x10
|           0x110b46f00597      b800000000     mov eax, 0
|           0x110b46f0059c      e8e3ffffff     call sym.lol
|           0x110b46f005a1      89c6           mov esi, eax
|           0x110b46f005a3      488d3d560410.  lea rdi, 0x110b47000a00 ; 0x110b47000a00 ; section..rodata ; "%d$
|           0x110b46f005aa      b800000000     mov eax, 0
|           0x110b46f005af      e8bcfdffff     call sym.imp.printf    ; sym.imp._csu_finish-0x20
|           0x110b46f005b4      b800000000     mov eax, 0
|           0x110b46f005b9      c9             leave
\           0x110b46f005ba      c3             ret
[0x110b46f0058f]>

…() so it does not return 0 on OpenBSD just because the memory mapping does not contain the file name.

Copied the fallback code from cmd_debug.c:r_debug_get_baddr().

getBaddrFromDebugger() from radare2.c also does the right thing. r_debug_get_baddr() probably should be exposed through the api so everyone can use one, maybe correct, version.
@radare
Copy link
Collaborator

radare commented Jan 8, 2017 via email

@nevun
Copy link
Contributor Author

nevun commented Jan 8, 2017

I know it is not pretty but do not shoot the messenger :)

Both other places in the code getting the base address does it like my patch.

cmd_debug.c:r_debug_get_baddr() does it this way:

        r_list_foreach (r->dbg->maps, iter, map) {
                if (!strcmp (abspath, map->name)) {
                        free (abspath);
                        return map->addr;
                }
        }
        free (abspath);
        // fallback resolution (osx/w32?)
        // we asume maps to be loaded in order, so lower addresses come first
        r_list_foreach (r->dbg->maps, iter, map) {
                if (map->perm == 5) { // r-x
                        return map->addr;
                }
        }
        return 0LL;

and binr/radare2/radare2.c:getBaddrFromDebugger() ALSO does it this way:

        r_list_foreach (r->dbg->maps, iter, map) {
                if (!strcmp (abspath, map->name)) {
                        free (abspath);
                        return map->addr;
                }
        }
        free (abspath);
        // fallback resolution (osx/w32?)
        // we asume maps to be loaded in order, so lower addresses come first
        r_list_foreach (r->dbg->maps, iter, map) {
                if (map->perm == 5) { // r-x
                        return map->addr;
                }
        }
        return 0LL;

..also get_base_from_maps() it is only called if !windows and !linux so it will not break anything which is not already broken. Actually maybe the fix is remove the call to get_base_from_maps() completely in core/file.c ?

@nevun
Copy link
Contributor Author

nevun commented Jan 9, 2017

I tested it on macOS and FreeBSD now, it does not break macOS and it fixes FreeBSD in the same way that it fixes OpenBSD.

@radare
Copy link
Collaborator

radare commented Jan 9, 2017

what im saying is that your patch iterates twice the maps, when the same behaviour can be implemented by just uncommenting the line after the XXX in the loop.

i think the problem was happening when the bin is not the first one listed in maps.. iirc it was happening in some linux distros.. didnt tested yet

@nevun
Copy link
Contributor Author

nevun commented Jan 9, 2017 via email

@radare radare merged commit d26a4cf into radareorg:master Jan 9, 2017
@radare
Copy link
Collaborator

radare commented Jan 9, 2017

i was about to use if (!b) b = map->addr; instead of this loop. but meh

@radare
Copy link
Collaborator

radare commented Jan 9, 2017

thanks!

@radare radare mentioned this pull request Jan 31, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants