简体   繁体   English

在Jonesforth中使用GDB显示字典单词

[英]Displaying dictionary word with GDB in Jonesforth

In Jonesforth , a dictionary entry is laid out as follows: 琼斯福斯 ,字典条目的布局如下:

<--- DICTIONARY ENTRY (HEADER) ----------------------->
+------------------------+--------+---------- - - - - +----------- - - - -
| LINK POINTER           | LENGTH/| NAME          | DEFINITION
|                        | FLAGS  |               |
+--- (4 bytes) ----------+- byte -+- n bytes  - - - - +----------- - - - -

We can take a peek at one of these entries using GDB. 我们可以使用GDB查看其中一个条目。 (See this question for details on using GDB with Jonesforth.) (有关在Jonesforth中使用GDB的详细信息,请参阅此问题 。)

Let's display the first 16 bytes of the dictionary entry for SWAP as characters: 让我们将SWAP字典条目的前16个字节显示为字符:

>>> x/16cb &name_SWAP
0x105cc:    -68 '\274'    5 '\005'    1 '\001'    0 '\000'    4 '\004'    83 'S'    87 'W'    65 'A'
0x105d4:    80 'P'    0 '\000'    0 '\000'    0 '\000'    43 '+'    0 '\000'    1 '\001'    0 '\000'

You can kind of see what's going on here. 你可以看到这里发生了什么。

The first four bytes are the pointer to the previous word in the dictionary: 前四个字节是指向字典中前一个单词的指针:

-68 '\274'    5 '\005'    1 '\001'    0 '\000'

Then comes the length of the name: 然后是名字的长度:

4 '\004'

Then we see the characters of the word name, "SWAP": 然后我们看到单词名称“SWAP”的字符:

83 'S'    87 'W'    65 'A'   80 'P'

And finally some padding to align on a 32-bit boundary: 最后一些填充在32位边界上对齐:

0 '\000'    0 '\000'    0 '\000'

It would be nice if there was a way to format the word entry in a nicer manner. 如果有一种方法以更好的方式格式化单词条目,那将是很好的。

If we do the following: 如果我们执行以下操作:

>>> x/1xw &name_SWAP
0x105cc:    0x000105bc

we note that name_SWAP is at 0x105cc . 我们注意到name_SWAP0x105cc

Let's use GDB's printf to display the word entry: 让我们使用GDB的printf来显示单词条目:

>>> printf "link: %#010x   name length: %i   name: %s\n", *(0x105cc), (char)*(0x105cc+4), (0x105cc+5)
link: 0x000105bc   name length: 4   name: SWAP

OK, that's not bad! 好的,那还不错! We see the link, the name length, and name, all nicely displayed and labeled. 我们看到链接,名称长度和名称,都很好地显示和标记。

The downside here is that I have to use the explicit address in the call to printf : 这里的缺点是我必须在printf的调用中使用显式地址:

printf "link: %#010x   name length: %i   name: %s\n", *(0x105cc), (char)*(0x105cc+4), (0x105cc+5)

Ideally, I'd just be able to say something like: 理想情况下,我只能说:

show_forth_word name_SWAP

and it'd display the above. 它会显示上面的内容。

What's the best way to go about this? 最好的方法是什么? Is this doable with a GDB user-defined command ? 这是否适用于GDB用户定义的命令 Or is it something more appropriate for the GDB Python interface? 或者它更适合GDB Python界面?

My question is, what's the best way to go about this? 我的问题是,最好的方法是什么?

It depends on whether GDB knows about the type of name_SWAP . 这取决于GDB是否知道name_SWAP类型 If it does, the Python pretty-printer is the way to go. 如果是这样,那么Python漂亮的打印机就是最佳选择。

If it doesn't, something as simple as user-defined command is likely easier. 如果没有,那么像用户定义的命令这样简单的东西可能更容易。 Assuming 32-bit mode: 假设32位模式:

define print_key
 set var $v = (char*)$arg0
 printf "link: %#010x   name length: %i   name: %s\n", *((char**)$v), *($v+4), ($v+5)
end

我刚刚使用了nm ,这是一款优秀的Unix命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM