【发布时间】:2017-02-24 13:08:05
【问题描述】:
我正在尝试将一个小型 c 程序从 hpux 迁移到 linux。该项目编译良好,但在运行时崩溃,向我显示分段错误。我已经尝试使用 strace 和 gdb 查看镜子后面的内容,但仍然不明白。相关(截断)部分:
tts_send_2.c
包含一个方法
int sequenznummernabgleich(int sockfd, char *snd_id, char *rec_id, int timeout_quit) {
TS_TEL_TAB tel_tab_S01;
int n;
# truncated
}
从该文件中调用,如下所示:
. . .
. . .
switch(sequenznummernabgleich(sockfd,c_snd_id,c_rec_id,c_timeout_quit)) {
/* kritischer Fehler */
case -1:
. . .
. . .
在调用该方法时,我遇到了分段错误(gdb 输出):
Program received signal SIGSEGV, Segmentation fault.
0x0000000000403226 in sequenznummernabgleich (sockfd=<error reading variable: Cannot access memory at address 0x7fffff62f94c>,
snd_id=<error reading variable: Cannot access memory at address 0x7fffff62f940>, rec_id=<error reading variable: Cannot access memory at address 0x7fffff62f938>,
timeout_quit=<error reading variable: Cannot access memory at address 0x7fffff62f934>) at tts_snd_2.c:498
498 int sequenznummernabgleich(int sockfd, char *snd_id, char *rec_id, int timeout_quit) {
我就是不明白。当我进入使用 gdb 调用该方法的行时,所有变量看起来都很好:
1008 switch(sequenznummernabgleich(sockfd,c_snd_id,c_rec_id,c_timeout_quit)) {
(gdb) p sockfd
$9 = 8
(gdb) p &sockfd
$10 = (int *) 0x611024 <sockfd>
(gdb) p c_snd_id
$11 = "KR", '\000' <repeats 253 times>
(gdb) p &c_snd_id
$12 = (char (*)[256]) 0xfde220 <c_snd_id>
(gdb) p c_rec_id
$13 = "CO", '\000' <repeats 253 times>
(gdb) p &c_rec_id
$14 = (char (*)[256]) 0xfde560 <c_rec_id>
(gdb) p c_timeout_quit
$15 = 20
(gdb) p &c_timeout_quit
$16 = (int *) 0xfde660 <c_timeout_quit>
我还创建了一个 strace 输出。这是关于上面显示的代码的最后一部分:
有什么想法吗?我已经在网上搜索了几个小时,当然也搜索了 stackoverflow,但没有找到一个非常相似的案例。
谢谢
克里兹
【问题讨论】:
-
崩溃在哪一行?您可以在调用函数之前发布代码(即变量声明和在调用时将它们设置为它们的值的代码)吗?
-
我认为你需要进入函数,然后看看变量是什么样的。
-
C 不支持方法。只有函数。
-
在调用该函数之前是否声明了 VLA?
-
@Andy Schweig:那是相当多的初始化代码,但是当我展示 gdb 的 sysout 时,它们在跳入函数之前都充满了有用的数据。
标签: c linux segmentation-fault