Parsing Forth Codewords with 'see'

I am preparing a general knowledge of creating a Forth interpreter and want to parse some of the common Forth codewords, such as + , - , * , etc.

My Gforth (I currently have version 0.7.3 installed on Ubuntu Linux) will allow me to parse the colon definitions that I make with the see command, as well as one codeword . . But when I try to use other codewords, see + or see / , I get a Code + error message, and then I can no longer enter my terminal, even when I press control-c,

I should be able to decompile / disassemble code words as shown in the Gforth manual: https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Decompilation-Tutorial.html

Has anyone else had this problem and do you know how to fix it?

+5
source share
3 answers

Going back to the old ptrace method did this for me.

First, from the command line when starting the root user:

 echo 0 >/proc/sys/kernel/yama/ptrace_scope 

After that, see must parse everything that it cannot decompile. Command line example (not necessarily root):

 gforth -e "see + bye" 

Output:

 Code + 0x000055a9bf6dad66 <gforth_engine+2454>: mov %r14,0x21abf3(%rip) # 0x55a9bf8f5960 <saved_ip> 0x000055a9bf6dad6d <gforth_engine+2461>: lea 0x8(%r13),%rax 0x000055a9bf6dad71 <gforth_engine+2465>: mov 0x0(%r13),%rdx 0x000055a9bf6dad75 <gforth_engine+2469>: add $0x8,%r14 0x000055a9bf6dad79 <gforth_engine+2473>: add %rdx,(%rax) 0x000055a9bf6dad7c <gforth_engine+2476>: mov %rax,%r13 0x000055a9bf6dad7f <gforth_engine+2479>: mov -0x8(%r14),%rcx 0x000055a9bf6dad83 <gforth_engine+2483>: jmpq *%rcx end-code 

Credit: Anton Earl

+3
source

SEE is a word that does not have strictly controlled behavior. This is kind of the best way to show the code of the word X if it is called as

WATCH X

It behaves a little differently, depending on how difficult it is. If you yourself defined this word in a session, you are guaranteed to get your code back. If it is an embedded word, especially if it is a very elementary word of type +, it is more complicated. This may not look like the original definition, due to optimization or compilation into machine code.

Especially for gforth, if it is difficult for him, gforth calls the standard tools available in the system for analyzing object files. Therefore, you may need to install gdb and / or find out how gforth is trying to connect to it. For a specific example, Ubuntu and gforth 0.7.3 Lutz Muller gives a recipe.

,

0
source

Most versions of SEE I've seen are for decompiling colon definitions only. + and / and other arithmetic operations are usually written in assembly code, and SEE does not know what to do with them. That's why you got the CODE error message: they are written in code, not in Forth. I've seen several Forth implementations that assemblers have built in, but I don't think I've ever seen a disassembler. The best option for viewing the inner workings of + and / or other similar words might be to use DUMP or another such word to get a list of bytes in a word and either disassemble the word manually, or feed data to an external disassembler, Or see if you can find the source code for your implementation or similar.

0
source

All Articles