是的,我知道这听起来很疯狂,但这是我能够描述它的唯一方法。我正在为一个模仿终端的类编写程序,因为它将命令作为输入并执行它们。 (我将在下面添加一些代码)正如您将看到的,该程序包含命令historyArgs的历史记录,以便用户可以执行最近的命令。
用户执行Ctrl-C时会列出命令历史记录。使用命令'r'(对于最近的)和r'x'(其中x与最近历史中的命令的第一个字母匹配)访问最近的命令。当我开始实现'r'命令时,我开始得到这个段错误。然后,我恢复了所有更改并一次添加一行。我发现添加偶数原始变量声明会导致段错误(int temp = 10;)但这是它变得陌生的地方。我相信永远不会访问导致段错误的行(int temp = 10;)。我输入printf语句并在if块的开头刷新输出以查看是否已输入块,但它们不会执行。
为我们提供了设置。它接受用户输入并将其放入char * args [],即input = ls -a -C,args = {“ls”,“ - a”,“ - C”,NULL,... NULL}。我在main中标记了以某种方式导致段错误的行。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#define BUFFER_SIZE 50
static char buffer[BUFFER_SIZE];
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
char *historyArgs[10][MAX_LINE/2 + 1];
int historyCount = 0;
int indexOfLatestCommand = 0;
/* the signal handler function */
void handle_SIGINT() {
//write(STDOUT_FILENO,buffer,strlen(buffer));
if(historyCount > 0){
printf("\n%i command(s), printing most recent:\n", historyCount);
int i;
for(i = 0; i < historyCount && i < 10; i++){
printf("%i.] %s ", i+1, historyArgs[i][0]);
//print args
int j = 1;
while(historyArgs[i][j] != NULL){
printf("%s ", historyArgs[i][j]);
j++;
}
printf("\n");
}
}
else{
printf("\nNo recent commands.\n");
}
fflush(stdout);
}
/**
* setup() reads in the next command line, separating it into distinct tokens
* using whitespace as delimiters. setup() sets the args parameter as a
* null-terminated string.
*/
void setup(char inputBuffer[], char *args[],int *background)
{
int length, /* # of characters in the command line */
i, /* loop index for accessing inputBuffer array */
start, /* index where beginning of next command parameter is */
ct; /* index of where to place the next parameter into args[] */
ct = 0;
/* read what the user enters on the command line */
length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
start = -1;
if (length == 0)
//exit(0); /* ^d was entered, end of user command stream */
if (length < 0){
perror("error reading the command");
exit(-1); /* terminate with error code of -1 */
}
/* examine every character in the inputBuffer */
for (i = 0; i < length; i++) {
switch (inputBuffer[i]){
case ' ':
case '\t' : /* argument separators */
if(start != -1){
args[ct] = &inputBuffer[start]; /* set up pointer */
ct++;
}
inputBuffer[i] = '\0'; /* add a null char; make a C string */
start = -1;
break;
case '\n': /* should be the final char examined */
if (start != -1){
args[ct] = &inputBuffer[start];
ct++;
}
inputBuffer[i] = '\0';
args[ct] = NULL; /* no more arguments to this command */
break;
case '&':
*background = 1;
inputBuffer[i] = '\0';
break;
default : /* some other character */
if (start == -1)
start = i;
}
}
args[ct] = NULL; /* just in case the input line was > 80 */
}
int main(void)
{
int i;
char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
int background; /* equals 1 if a command is followed by '&' */
char* args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */
int status;
struct sigaction handler;
handler.sa_handler = handle_SIGINT;
sigaction(SIGINT, &handler, NULL);
strcpy(buffer,"Caught <ctrl><c>\n");
while (1){ /* Program terminates normally inside setup */
background = 0;
printf("COMMAND->");
fflush(0);
setup(inputBuffer, args, &background); /* get next command */
//If command wasn't empty
if(args[0] != NULL){
if(strcmp(args[0], "r") != 0){
//Copy into history if not a recent call
for(i = 0; i < MAX_LINE/2+1 && args[i] != NULL; i++){
historyArgs[historyCount%10][i] = malloc(strlen(args[i]));
strcpy(historyArgs[historyCount%10][i], args[i]);
}
indexOfLatestCommand = historyCount%10;
historyCount++;
}
//Create child process
int pid = fork();
//In child process
if(pid == 0){
if(strcmp(args[0], "r") == 0){
//If only "r" was entered, execute most recent command
if(args[1] == NULL){
printf("Entering recent?\n");
fflush(stdout);
int temp = 10; //SEGFAULTS HERE IF THIS IS INCLUDED
execvp(historyArgs[indexOfLatestCommand][0], &historyArgs[indexOfLatestCommand][0]);
}
else{
//Find in args[1][0] history, run if found
for(i = indexOfLatestCommand; i >= 0; i--){
if(historyArgs[i][0][0] == args[1][0]){
execvp(historyArgs[i][0], &historyArgs[i][0]);
break;
}
}
if(i == -1){
for(i = historyCount > HISTORY_SIZE ? HISTORY_SIZE : historyCount; i > indexOfLatestCommand; i--){
if(historyArgs[i][0][0] == args[1][0])
execvp(historyArgs[i][0], &historyArgs[i][0]);
break;
}
}
}
}
else execvp(args[0], &args[0]);
}
//In parent process
else if (pid > 0){
/*If child isn't a background process,
wait for child to terminate*/
if(background == 0)
while(wait(&status) != pid);
}
}
}
}
值得一提的是,在该位置声明变量不会导致段错误。只为新变量赋值。在该部分重新分配全局变量也不会导致段错误。
编辑:什么引发了崩溃。命令正确执行。当你运行它时,你可以键入任何命令,这应该工作。直到我执行Ctrl-C并打印出程序段错误的历史记录。
示例输入:
LS
ls -a
grep的
Ctrl-C
HEADS UP:如果你决定运行它,知道要结束任务,你可能需要使用kill命令,因为我没有实现“q”退出。
答案 0 :(得分:2)
您看到的症状(不相关的代码更改似乎会影响崩溃的性质)通常意味着您的程序更早地导致了未定义的行为。行为的性质会发生变化,因为您的程序依赖于它在某个阶段读取或写入的垃圾值。
要调试它,请尝试删除程序中所有未定义行为的来源。最明显的一个是void handle_SIGINT()
函数的内容。您可以在信号器中轻松执行的 事项是:
volatile sig_atomic_t
类型的变量或其他无锁类型,然后返回_Exit
,abort
或类似。特别是,您无法调用任何库函数,因为它们可能无法重入。
有关完整规格,请参阅当前C标准的第7.14.1节。如果您还遵循其他一些标准,例如POSIX,它可以指定信号处理程序中允许的其他一些东西。
如果您不打算退出,则必须设置一个标志,然后稍后从主“线程”中测试该标志,以查看是否出现了信号。