dis-gdb.fs 2.8 KB
Newer Older
1 2
\ dis-gdb.fs	disassembler using gdb
\
3
\ Authors: Anton Ertl, Bernd Paysan
Anton Ertl's avatar
Anton Ertl committed
4
\ Copyright (C) 2004,2007,2008,2010,2011,2014,2015,2016,2019 Free Software Foundation, Inc.
5 6 7 8 9

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
10
\ as published by the Free Software Foundation, either version 3
11 12 13 14 15 16 17 18
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
19
\ along with this program. If not, see http://www.gnu.org/licenses/.
20

21 22 23
: append-extend-string ( addr1 u1 addr2 u2 -- addr3 u1+u2 )
    \ append string2 to string1 with dynamic memory reallocation.
    swap >r dup >r extend-mem ( to addr3 u1+u2 r: addr2 u2 )
24 25
    rot r> r> rot rot chars move ;

26 27
get-current also see-voc definitions

28 29 30 31 32 33 34 35
defer gdb-addr-sep-char ( -- c )

',' constant #comma

: check-gdb-syntax ( -- c )
    \ gdb-7.0 and earlier do what we want with "disassemble addr1 addr2"
    \ gdb-7.1 and later only work with         "disaesemble addr1,addr2"
    \ try the old syntax to see if it works
36
    s" gdb -ex 'disassemble 0, 1' -ex 'quit' 2>/dev/null" r/o open-pipe throw
37
    dup slurp-fid rot close-pipe throw drop
38
    s" Dump of assembler code from" search nip nip if
39
        ['] #comma
40 41
    else
        ['] bl
42 43 44 45 46 47
    then
    dup is gdb-addr-sep-char
    execute ;

' check-gdb-syntax is gdb-addr-sep-char

48 49
set-current

50
: disasm-gdb { addr u -- }
51 52 53 54 55 56 57 58 59 60
    cr addr u
    [: [: { addr u }
            s\" type mktemp >/dev/null && type gdb >/dev/null && file=`mktemp -t gforthdis.XXXXXXXXXX` && file2=`mktemp -t gforthdis.XXXXXXXXXX` && echo \"set verbose off\nset logging file $file\nset logging on\ndisas " save-mem ( addr u addr1 u1 )
            addr 0 <<# gdb-addr-sep-char hold # #s 'x hold # #> append-extend-string #>>
            addr u + 0 <<# # #s 'x hold # #> append-extend-string #>>
        ;] $10 base-execute
    ;] catch if
        ." Gdb does not work, fall back to DUMP"
        2drop ['] dump is discode
        addr u dump exit then
Bernd Paysan's avatar
Bernd Paysan committed
61 62 63 64 65
    [ e? os-type s" cygwin" str= ] [IF]
	s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -q -p `ps -p $$ | grep -v PPID | cut -c 10-17` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file"
    [ELSE]
	s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -q -p `ps -p $$ -o ppid=` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file"
    [THEN]  append-extend-string
66 67 68 69 70
    2dup (system) 2swap drop free throw throw if
	addr u dump
    endif ;

' disasm-gdb is discode
71 72

previous