gpt4 book ai didi

c - 自定义 C Shell 中的段错误

转载 作者:行者123 更新时间:2023-11-30 15:54:20 24 4
gpt4 key购买 nike

我正在 Unix 命令环境中编写 C shell。到目前为止,一切都按标准运行,但是以下命令将导致我的代码正确执行,但以段错误终止

1. ls | ls | ls
2. ls -al | ls -al > output.txt

底线:我相信段错误来自“XXXX 从这里开始”和“XXXX 从这里结束”之间的行。我还相信它与第一个 while 循环中的 getchar 测试有关,尽管我可能是错的。如果编译并运行此代码,您将在输入“ls -al | ls -al > output.txt”时看到所有内容都将执行,然后打印 shhh> AAAAA,然后是 seg 错误。谁能证实或否认我最初的观察?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>

main()
{
char *path, *argv[20], buf[80], n, *p;
int m, status, inword, continu;
int inFlag, outFlag, bgFlag, k, count, j, pipes, pid, aCount, dirCount,
hCount;
int r_tube[2], l_tube[2];
char *hist;
char *h1[20] = { 0 };
aCount = 0;
dirCount = 0;

while (1)
{
inword = 0;
p = buf;
m = 0;
continu = 0;
inFlag = outFlag = bgFlag = k = count = j = pipes = pid = 0;
hCount = 0;
int loc[20] = { 0 };
char currD[50];

printf("\nshhh> ");

while ((n = getchar()) != '\n' || continu) //XXXXXXXXX Starts here
{
if (n == ' ')
{
if (inword)
{
inword = 0;
*p++ = 0;
}
}
else if (n == '\n')
continu = 0;
else if (n == '\\' && !inword)
continu = 1;
else
{
if (!inword)
{
inword = 1;
argv[m++] = p;
*p++ = n;
}
else
*p++ = n;
}
} //XXXXXXXX Ends here

*p++ = 0;
argv[m] = 0;

getcwd(currD, 50);

while (argv[k] != 0)
{ //stores all entered commands into history array
h1[aCount] = strdup(argv[k]);
aCount++;
k++;
}
k = 0;

if ((strcmp(argv[0], "history") == 0) ||
(strcmp(argv[0], "History") == 0))
{ //tests to see if history is asked for
printf("History: \n");
for (k = 0; k < aCount; k++)
printf("%s\n", h1[k]);
}


if (strcmp(argv[0], "cd") == 0)
{ //tests to see if CD is needed
if (strcmp(argv[1], "..") == 0)
chdir(currD);
else
{
while (argv[dirCount] != 0)
{
getcwd(currD, 50);
chdir(argv[dirCount]);
dirCount++;
}
}
}

char *outFile = NULL;
char *inFile = NULL;
loc[0] = 0;

while (argv[count] != 0)
{ //while loop sets the flags for input redirection,
// output redirection, background operator, and piping
if (strcmp(argv[count], "<") == 0)
{
inFile = strdup(argv[count + 1]);
argv[count] = argv[count + 1] = 0;
inFlag = 1;
}
else if (strcmp(argv[count], ">") == 0)
{
outFile = strdup(argv[count + 1]);
argv[count] = argv[count + 1] = 0;
outFlag = 1;
}
else if (strcmp(argv[count], "&") == 0)
{
argv[count] = 0;
bgFlag = 1;
}
else if (strcmp(argv[count], "|") == 0)
{
argv[count] = 0;
loc[pipes + 1] = count + 1;
pipes++;
printf("LOC0: %d ", loc[0]);
printf("LOC1: %d ", loc[1]);
printf("LOC2: %d ", loc[2]);
printf("DONE WITH IF\n");
}
else
loc[count] = count;

count++;
}

for (k = 0; k <= pipes; k++)
{ //actual execution of commands
printf("IN FOR\n");
if (k < pipes)
{
pipe(r_tube);
j++;
}

pid = fork(); //fork child every time to exec

if (pid > 0)
{
if (j > 0)
{
close(l_tube[0]);
close(l_tube[1]);
}
l_tube[0] = r_tube[0];
l_tube[1] = r_tube[1];
}
else if (pid == 0)
{
if ((k == 0) && (inFlag == 1))
{
int n = open(inFile, O_RDONLY | O_CREAT);
if (n == -1)
{
printf("Couldn't open inFile!\n");
exit(1);
}
close(0);
dup(n);
close(n);
}
else if ((k == pipes) && (outFlag == 1))
{
int out = open(outFile, O_WRONLY | O_CREAT, 0666);
if (out < 0)
{
printf("Could'nt open outFile!\n");
exit(1);
}
close(1);
dup(out);
close(out);
}
printf("K: %d ", k);
printf("PIPES: %d ", pipes);
printf("PID: %d\n", pid);
execvp(argv[loc[k]], &argv[loc[k]]);
}
printf("ONE\n");
if (bgFlag == 0)
wait(NULL);
}
printf("TWO\n");
if (strcmp(argv[0], "quit") == 0)
exit(0); //tests for exit/quit to end program
if (strcmp(argv[0], "exit") == 0)
exit(0);
printf("THREE\n");
for (k = 0; k < 20; k++) //reset all of argv to NULL
argv[k] = 0;
printf("FOUR\n");

wait(&status);
}
}

最佳答案

处理段错误的最简单方法是分析保存到内核生成的 coredump 中的堆栈跟踪。大多数发行版都禁用了核心转储保存,因此如何启用它

ulimit -c unlimited

Proc 文件系统有几个文件来配置 corudump 生产。最重要的是core pattern /proc/sys/kernel/core_pattern,指定 coredump 名称和可选的后处理命令。

当进程收到段错误信号时,内核会将其状态保存在 coredump 文件中,稍后可以使用 dgb 检查该文件。 :

gdb -c core ./binary_which_produced_the_core
bt

这将打印出发生段错误时程序正在执行的操作的回溯。

例如,对于您的程序

Program terminated with signal 11, Segmentation fault.
#0 main () at main.c:58
58 *p++ = n;
(gdb) bt
#0 main () at main.c:58
(gdb)

注意,我必须添加缺少的 header ,因此行号为 +2。

请使用 -Wall -Wextra 编译器选项来报告所有警告并始终修复它们。例如,您的 l_tube 数组未初始化。

关于c - 自定义 C Shell 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13095978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com