//-I意为include 指定头文件搜索路径 -l:告诉编译器链接时需要的库
gcc *.c -I /usr/include/fastdfs/ -I /usr/include/fastcommon/ -l fdfsclient
//调试
gcc -g -rdynamic main.c
如何解决 “ 段错误(吐核) ” ???
【线上排错】记一次Linux的“段错误(吐核)”排查步骤
Centos 开始Core调试
[Linux] 什么是 段错误(吐核)?
Linux环境下段错误的产生原因及调试方法小结
跟着视频操作,运行代码出现
[fish@localhost mytest]$ ./a.out
段错误(吐核)
参考了上述连接不断调试,并且反复校对代码和视频以及资料中的差别,无果
最后得到的调试信息是
(gdb) run
Starting program: /home/fish/mytest/./a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7874a4a in __GI__IO_str_overflow (fp=0x7fffffffd2c0, c=91) at strops.c:133
133 *fp->_IO_write_ptr++ = (unsigned char) c;
个人感觉是哪个地方的读写出了问题,导致内存访问溢出了之类的,但是没找到具体是哪个语句的调用出了问题。先记一下,后续有时间再调。要是有知道哪儿出问题的大佬,请不吝赐教!
对应调用出错位置代码:
(gdb) l
128 fp->_IO_write_end = fp->_IO_buf_end;
129 }
130 }
131
132 if (!flush_only)
133 *fp->_IO_write_ptr++ = (unsigned char) c;
134 if (fp->_IO_write_ptr > fp->_IO_read_end)
135 fp->_IO_read_end = fp->_IO_write_ptr;
136 return c;
137 }
(gdb) l
138 libc_hidden_def (_IO_str_overflow)
139
140 int
141 _IO_str_underflow (_IO_FILE *fp)
142 {
143 if (fp->_IO_write_ptr > fp->_IO_read_end)
144 fp->_IO_read_end = fp->_IO_write_ptr;
145 if ((fp->_flags & _IO_TIED_PUT_GET) && (fp->_flags & _IO_CURRENTLY_PUTTING))
146 {
147 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
贴下源码:
main.c
#include
#include
#include
#include
#include
#include
#include
#include "fdfs_upload_file.h"
int main(int argc,const char* argv[]){
char fileID[1024] = {0};
upload_file1("/etc/fdfs/client.conf","main.c",fileID);
printf("fileID:%s\n",fileID);
printf("==================\n");
upload_file2("/etc/fdfs/client.conf","main.c",fileID,sizeof(fileID));
printf("fileID:%s\n",fileID);
return 0;
}
fdfs_upload_file.h
#ifndef _FDFS_UPLOAD_FILE_H
#define _FDFS_UPLOAD_FILE_H
int upload_file1(const char* confFile, const char* localFile, char* fileID);
int upload_file2(const char* confFile, const char* localFile, char* fileID, int bufLen);
#endif
fdfs_upload_file.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "fdfs_client.h"
#include "fdfs_upload_file.h"
int upload_file1(const char* confFile,const char* localFile,char* fileID)
{
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
ConnectionInfo *pTrackerServer;
int result;
int store_path_index;
ConnectionInfo storageServer;
if ((result=fdfs_client_init(confFile)) != 0)
{
return result;
}
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
fdfs_client_destroy();
return errno != 0 ? errno : ECONNREFUSED;
}
*group_name = '\0';
if ((result=tracker_query_storage_store(pTrackerServer, \
&storageServer, group_name, &store_path_index)) != 0)
{
fdfs_client_destroy();
fprintf(stderr, "tracker_query_storage fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
return result;
}
result = storage_upload_by_filename1(pTrackerServer, \
&storageServer, store_path_index, \
localFile, NULL, \
NULL, 0, group_name, fileID);
if (result == 0)
{
printf("%s\n", fileID);
}
else
{
fprintf(stderr, "upload file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
}
tracker_disconnect_server_ex(pTrackerServer, true);
fdfs_client_destroy();
return result;
}
//多进程实现
int upload_file2(const char* confFile,const char* localFile,char* fileID,int bufLen){
//创建管道
int fd[2];
int ret=pipe(fd);
if(ret==-1){
perror("pipe reeor");
exit(0);
}
//创建子进程
pid_t pid = fork();
//如果是子进程
if(pid==0){
//3.标准输出重定向 -》管道的写端
dup2(fd[1],STDOUT_FILENO);
//4.关闭读端
close(fd[0]);
//5。执行execlp命令
execlp("fdfs_upload_file","xxx",confFile, localFile, NULL);
perror("execlp error");
}
else{
//父进程,读管道
//关闭写端
close(fd[1]);
read(fd[0],fileID,bufLen);
//回收pcb-》子进程
wait(NULL);
}
}