对 gdb print ptr 与 print "%s" 感到困惑

发布于 2024-07-06 05:06:05 字数 858 浏览 10 评论 0 原文

1167      ptr = (void*)getcwd(cwd, MAX_PATH_LENGTH-1);
(gdb) n
1168      if (!ptr) {
(gdb) print ptr
$1 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) print &cwd
$2 = (char (*)[3500]) 0xbff2d96c
(gdb) print strlen(cwd)
$3 = 36
(gdb) print "%s",cwd
$4 = "/media/MMC-SD/partition1/aaaaaaaaaaa", '\0' <repeats 912 times>, "��O�001\000\000\000\000��027\000\000\000�3����EL鷠3�000��027\000\000\000\000\000\000\000\027\000\000\000\000��/�027\000\000\000�3����N����\230���鷠3�000��027\000\000\000\000\000\000\000��000\000\000\000\001\000\000\000��M鷠3����\000\000\000\000.\231�027��w\005\b\001\000"...
(gdb) print "%s", ptr
$5 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) Quit

为什么 ptr 可以正确打印字符串而 cwd 却不能; 这也会影响程序,如果我尝试使用 cwd,它就会崩溃...

[编辑:事实证明,崩溃是由该变量上的愚蠢缓冲区溢出引起的... grr...不是 gdb,但打印问题是仍然有效]

1167      ptr = (void*)getcwd(cwd, MAX_PATH_LENGTH-1);
(gdb) n
1168      if (!ptr) {
(gdb) print ptr
$1 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) print &cwd
$2 = (char (*)[3500]) 0xbff2d96c
(gdb) print strlen(cwd)
$3 = 36
(gdb) print "%s",cwd
$4 = "/media/MMC-SD/partition1/aaaaaaaaaaa", '\0' <repeats 912 times>, "��O�001\000\000\000\000��027\000\000\000�3����EL鷠3�000��027\000\000\000\000\000\000\000\027\000\000\000\000��/�027\000\000\000�3����N����\230���鷠3�000��027\000\000\000\000\000\000\000��000\000\000\000\001\000\000\000��M鷠3����\000\000\000\000.\231�027��w\005\b\001\000"...
(gdb) print "%s", ptr
$5 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) Quit

Why is ptr printing the string correctly but cwd not; this also affects the program and it crashes if I try to use the cwd...

[edit: turns out that crash was caused by a stupid buffer overflow on this var... grr...not gdb, but the print question was still valid]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゝ偶尔ゞ 2024-07-13 05:06:05

ptr 显示为格式良好的字符串,而 cwd 显示为“字节缓冲区”,这可能是 gdb 特有的。 无论如何,它不应该影响您的申请; 根据man 3 getcwdptr应该指向cwd(或者如果发生错误,它应该为NULL)。
你能使用ptr而不会使程序崩溃吗?

That ptr is displayed as nicely-formatted string and cwd as "byte buffer" is probably specific to gdb. In any case it shouldn't affect your application; according to man 3 getcwd, ptr should point to cwd (or it should be NULL if an error occurred).
Can you use ptr without crashing the program?

染火枫林 2024-07-13 05:06:05

cwd是什么类型? 上面的代码片段并没有告诉我们这一点。 gdb 可能会以不同方式对待 ptr 作为 void*。

What type is cwd? The above code snippet doesn't tell us that. It could be that ptr being a void* is treated differently by gdb.

笑脸一如从前 2024-07-13 05:06:05

cwdgdb 中打印不同的原因是 gdb 知道 ptr 是一个 char * (我猜)并且 cwd 是一个长度为 3500 的数组(如输出所示)。 因此,当打印 ptr 时,它会打印指针值(作为服务,还会打印它指向的字符串),而当打印 cwd 时,它会打印整个大批。

我不明白为什么使用 cwd 而不是 ptr 会导致问题,但我需要查看一些代码才能确定。

The reason that cwd is printed differently in gdb is because gdb knows that ptr is a char * (I guess) and that cwd is an array of length 3500 (as shown in your output). So when printing ptr it prints the pointer value (and as a service also the string it points to) and when printing cwd it prints the whole array.

I don't see any reason why using cwd instead of ptr would lead to problems, but I would need to see some code to be sure.

别挽留 2024-07-13 05:06:05

我同意姆韦尔登的观点。 尝试一些我认为与您的代码类似的东西,我得到:

(gdb) print cwd
$1 = "/media", '\0' <repeats 782 times>, "\016���" ...
(gdb) print (char*) cwd
$2 = 0xbfc8eb84 "/media"

从gdb,所以看来由于cwd被定义为char cwd[3500],gdb打印整个数组,而如果您告诉 gdb 将其解释为 char*,它将按您的预期工作。 如果您的应用程序崩溃,我会认为这是因为其他原因。

I agree with mweerden. Trying something I believe is similar to your code, I get:

(gdb) print cwd
$1 = "/media", '\0' <repeats 782 times>, "\016���" ...
(gdb) print (char*) cwd
$2 = 0xbfc8eb84 "/media"

from gdb, so it seems that since cwd was defined as char cwd[3500], gdb prints the entire array, while if you tell gdb to interpret it as a char*, it will work as you expect. If your application crashes, I would assume it is because of something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文