C中由mkfifo()生成的分段错误
我已经试着调试这几个小时了,但我仍然被卡住了 我在这段代码中遇到了一个“mkfifo”调用的分段错误(这只是我整个代码的一部分,因为我认为其余部分与此无关): gdb回溯跟踪产生以下结果:C中由mkfifo()生成的分段错误,c,segmentation-fault,mkfifo,C,Segmentation Fault,Mkfifo,我已经试着调试这几个小时了,但我仍然被卡住了 我在这段代码中遇到了一个“mkfifo”调用的分段错误(这只是我整个代码的一部分,因为我认为其余部分与此无关): gdb回溯跟踪产生以下结果: #0 strchrnul () at ../sysdeps/x86_64/strchr.S:32 #1 0x00007ffff7a5ed82 in __find_specmb (format=0xffffffffffffff60 <error: Cannot access memory at add
#0 strchrnul () at ../sysdeps/x86_64/strchr.S:32
#1 0x00007ffff7a5ed82 in __find_specmb (format=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>)
at printf-parse.h:108
#2 _IO_vfprintf_internal (s=0x7fffffffb5a0, format=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>,
ap=0x7fffffffdd58) at vfprintf.c:1332
#3 0x00007ffff7a63f31 in buffered_vfprintf (s=s@entry=0x7ffff7dd41c0 <_IO_2_1_stderr_>,
format=format@entry=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>, args=args@entry=0x7fffffffdd58)
at vfprintf.c:2356
#4 0x00007ffff7a5eeae in _IO_vfprintf_internal (s=0x7ffff7dd41c0 <_IO_2_1_stderr_>,
format=format@entry=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>, ap=ap@entry=0x7fffffffdd58)
at vfprintf.c:1313
#5 0x00007ffff7b0c595 in error_tail (status=status@entry=4199947, errnum=errnum@entry=1,
message=message@entry=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>, args=args@entry=0x7fffffffdd58)
at error.c:201
#6 0x00007ffff7b0c6ed in __error (status=4199947, errnum=1,
message=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>) at error.c:251
#7 0x0000000000400b78 in main (argc=1, argv=0x7fffffffdf38) at src/executableFile.c:75
#0 strchrnul()位于../sysdeps/x86_64/strchr.S:32
#1 0x00007ffff7a5ed82 in uuuuu find_uspecmb(格式=0xFFFFFFFFFFFF60)
在printf parse.h:108
#内部(s=0x7FFFFFB5A0,格式=0xFFFFFFFFFFFF60,
ap=0x7fffffffdd58)在vfprintf.c:1332处
#3 0x00007ffff7a63f31在缓冲的vfprintf中=s@entry=0x7ffff7dd41c0,
格式=format@entry=0xFFFFFFFFFF60,参数=args@entry=0x7FFFFFDD58)
在vfprintf.c:2356
#内部(s=0x7ffff7dd41c0,
格式=format@entry=0xFFFFFFFFFF60,ap=ap@entry=0x7FFFFFDD58)
在vfprintf.c:1313
#5 0x00007FF7B0C595错误(状态=status@entry=4199947,errnum=errnum@entry=1,
信息=message@entry=0xFFFFFFFFFF60,参数=args@entry=0x7FFFFFDD58)
错了。c:201
#6 0x00007FF7B0C6插入错误(状态=4199947,错误数=1,
错误消息=0xFFFFFFFFFF60)。c:251
#在src/executableFile.c:75的main(argc=1,argv=0x7fffffffdf38)中有7个0x0000000000400b78
虽然创建了“pipe.fifo”文件。。。
提前感谢您的帮助
编辑:
error在error.c中定义为error,在error.h中定义为error的签名:
#include "error.h"
#include <stdlib.h>
#include <stdio.h>
void error(char *msg, int ret)
{
perror(msg);
exit(ret);
}
#包括“error.h”
#包括
#包括
无效错误(字符*msg,整数ret)
{
佩罗尔(味精);
出口(ret);
}
如果查看堆栈跟踪,您将看到对error()
的调用显示为:
0x00007ffff7b0c6ed in __error (status=4199947, errnum=1, message=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>) at error.c:251
看
如您所见,此函数需要一个char*format
作为最后一个参数,它从堆栈0xffffffffffff60
中获取一些垃圾,因为您根本不传递第三个参数。链接器似乎正在将对error()
的调用解析为错误的函数
作为一个快速解决方案,我将重命名如下:
rename your file `error.h` to `error_my.h`
your definition of `error()` to, say, `error_my()`
replace the calls to `error()` with `error_my()`.
您的代码如下所示:
#include "marketfunc.h"
#include "error_my.h" // <=======================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define PIPE_PATH "./pipe.fifo"
struct myStruct
{
int x;
int y;
int z;
};
struct myStruct *s;
int main(int argc, char **argv)
{
s = malloc(sizeof(struct myStruct));
// 'int marketfunc_init(int x)' is defined in a perfectly working extern library
if(marketfunc_init(1) == -1) error_my("failed to initialize marketfunc library", 5); // <=======================
printf("test1\n");
// Segmentation fault
if(mkfifo(PIPE_PATH, 0666) == -1) error_my("failed to create pipe", 1); // <=======================
printf("test2\n");
//...
}
#包括“marketfunc.h”
#包括“error\u my.h”//看起来好像是error
是罪魁祸首,但我们不知道它是如何定义的请求管道路径
还是管道路径
?s
是指向结构的指针还是实际的结构实例?诸如此类的事情会分散你对实际问题的注意力,请学习如何创建一个。MCVE的目的有几个:问题可以很容易地被评论人复制(或不复制);您不会假设“代码的其余部分不会导致错误”(因为您不知道错误在哪里);很多时候,准备MCVE会在你做的时候向你揭示问题所在。我编辑了这篇文章来澄清一下。感谢您的快速回答代码>结构变量s
不是指针。您是否启用了警告?非常感谢,这解决了问题!我是如此专注于mkfifo呼叫,以至于我甚至没有考虑检查这个。。。
void error(int status, int errnum, const char *format, ...);
rename your file `error.h` to `error_my.h`
your definition of `error()` to, say, `error_my()`
replace the calls to `error()` with `error_my()`.
#include "marketfunc.h"
#include "error_my.h" // <=======================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define PIPE_PATH "./pipe.fifo"
struct myStruct
{
int x;
int y;
int z;
};
struct myStruct *s;
int main(int argc, char **argv)
{
s = malloc(sizeof(struct myStruct));
// 'int marketfunc_init(int x)' is defined in a perfectly working extern library
if(marketfunc_init(1) == -1) error_my("failed to initialize marketfunc library", 5); // <=======================
printf("test1\n");
// Segmentation fault
if(mkfifo(PIPE_PATH, 0666) == -1) error_my("failed to create pipe", 1); // <=======================
printf("test2\n");
//...
}