为什么gdb显示/stdlib/strtol_l.c:没有这样的文件或目录?我是否缺少要安装的东西?

发布于 2025-01-12 20:19:46 字数 8539 浏览 0 评论 0原文

我尝试使用 -g 进行编译,然后运行 ​​gdb 来查找导致分段错误的行,但错误消息让我感到困惑。

Program received signal SIGSEGV, Segmentation fault.
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0, loc=0x7ffff7fb04a0 <_nl_global_locale>)
    at ../stdlib/strtol_l.c:292
292     ../stdlib/strtol_l.c: No such file or directory.

我尝试重新安装 gdb 以使其再次工作,但失败了。它仍然显示相同的错误消息。后来我自己发现了这个问题,并在下面的代码中标记出来。我只是好奇为什么当我尝试调试某些字符串函数时有时会发生这样的事情?例如 strdupstrtokstrtol 等。我是否缺少要安装的东西?我希望我能彻底解决这个问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

char buff[255];
#define NUM_BUCKETS 32

typedef struct Customer {
    char* email;
    char* name;
    int shoesize;
    char* food;
    struct Customer* next;
} Customer ;

unsigned long hash(char *str) {
    unsigned long hash = 0;
    int c;

    while (*str != '\0') {
        c = *str;
        hash = ((hash << 5) + hash) + (unsigned char)c;
        str++;
    }
    return hash;
}

Customer *add_friend_to_list(char *email, char *name, int shoesize, char *food, Customer *bucket) {
    Customer* customer;

    customer = malloc(sizeof(Customer));
    customer->name = strdup(name);
    customer->food = strdup(food);
    customer->shoesize = shoesize;
    customer->email = strdup(email);
    customer->next = bucket;

    return customer;
}

void add_consumer_to_hashtable(char *name, char *food, char *email, int shoesize, Customer **buckets, size_t num_buckets) {
    size_t which_bucket = hash(name) % num_buckets;
    buckets[which_bucket] = add_friend_to_list(email, name, shoesize, food, buckets[which_bucket]);
}

int main() {
    Customer* buckets[NUM_BUCKETS] = {NULL};
    int ittime = 0;
    FILE *fp = NULL;
    fp = fopen("customers.tsv", "r");
    while (true) {
        fgets(buff, 255, fp);
        if (feof(fp)) {
            break;
        }
        ittime++;
    }
    
    fclose(fp);

    fp = NULL;
    char *email = (char *)malloc(5 * sizeof(char));
    char *name = (char *)malloc(5 * sizeof(char));
    int shoesize;
    char *food = (char *)malloc(5 * sizeof(char));
    const char s[2] = "\t";
 
    fp = fopen("customers.tsv", "r");
    for (int i = 0; i < ittime + 1; i++) {        //This line cause the Segmentation Fault
        fgets(buff, 255, fp);
        char *token;

        token = strtok(buff, s);
        email = token;
        token = strtok(NULL, s);
        name = token;
        token = strtok(NULL, s);
        shoesize = atoi(token);
        token = strtok(NULL, s);
        food = token;
        add_consumer_to_hashtable(name, food, email, shoesize, buckets, NUM_BUCKETS);
    }

    fclose(fp);

    while (true) {
        char *cmd = (char *)malloc(5 * sizeof(char));

        printf("command: ");
        scanf("%s", cmd);
        if (strcmp(cmd, "add") == 0) {
            char *email1 = (char *)malloc(5 * sizeof(char));
            char *name1 = (char *)malloc(5 * sizeof(char));
            int shoesize1;
            char *food1 = (char *)malloc(5 * sizeof(char));

            printf("email address? ");
            scanf("%s", email1);
            printf("name? ");
            scanf(" %[^\n]", name1);
            printf("shoe size? ");
            scanf("%d", &shoesize1);
            printf("favorite food? ");
            scanf("%s", food1);
            add_consumer_to_hashtable(name1, food1, email1, shoesize1, buckets, NUM_BUCKETS);
            free(name1);
            free(food1);
            free(email1);
        } else if (strcmp(cmd, "lookup") == 0) {
            char *Email = (char *)malloc(5 * sizeof(char));
            printf("email address? ");
            scanf("%s", Email);
            bool exist = false;
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL)) {
                    if (cus->shoesize == EOF) {
                        break;
                    }
                    if (strcmp(cus->email, Email) == 0) {
                        printf("email: %s\n", cus->email);
                        printf("name: %s\n", cus->name);
                        printf("shoesize: %d\n", cus->shoesize);
                        printf("food: %s\n", cus->food);
                        exist = true;
                        break;
                    }
                    if (cus->next != NULL) {
                        cus = cus->next;
                    } else {
                        break;
                    }
                }
            }
            if (exist == false) {
                printf("user not found!\n");
            }
        } else if (strcmp(cmd, "delete") == 0) {
            char *Email = (char *)malloc(5 * sizeof(char));
            printf("email address? ");
            scanf("%s", Email);
            bool exist = false;
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL)) {
                    if (cus->shoesize == EOF) {
                        break;
                    }
                    if (strcmp(cus->email, Email) == 0) {
                        free(cus->email);
                        free(cus->food);
                        free(cus->name);
                        free(cus);
                        cus->shoesize = EOF;
                        cus = NULL;
                        exist = true;
                        break;
                    }
                    if (cus->next != NULL) {
                        cus = cus->next;
                    } else {
                        break;
                    }
                }
            }
            if (exist == false) {
                printf("user not found!\n");
            }
        } else if (strcmp(cmd, "list") == 0) {
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL) && ((cus->shoesize) != EOF)) {
                    printf("email: %s\n", cus->email);
                    printf("name: %s\n", cus->name);
                    printf("shoesize: %d\n", cus->shoesize);
                    printf("food: %s\n", cus->food);
                    if (cus->next != NULL) {
                        cus = cus->next;
                        printf("\n");
                    } else {
                        break;
                    }
                }
            }
        } else if (strcmp(cmd, "quit") == 0) {
            break;
        } else if (strcmp(cmd, "save") == 0) {
            fp = fopen("customers.tsv", "w");
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL) && ((cus->shoesize) != EOF)) {
                    fprintf(fp, "%s\t%s\t%d\t%s", cus->email, cus->name, cus->shoesize, cus->food);
                    if (cus->next != NULL) {
                        cus = cus->next;
                        fprintf(fp, "\n");
                    } else {
                        break;
                    }
                }
            }
            fclose(fp);
        } else {
            printf("unknown command\n");
        }
    }
    for (int i = 0; i < 32; i++) {
        Customer *tmp;
        Customer *cus = buckets[i];
        if (cus == NULL) {
            continue;
        }
        if (cus->next != NULL) {
            tmp = cus;
            cus = cus->next;
        } else {
            break;
        }
        while ((tmp != NULL)) {
            if (tmp->shoesize != EOF) {
                free(tmp->email);
                free(tmp->food);
                free(tmp->name);
                free(tmp);
            }
            cus->shoesize = EOF;
            cus = NULL;
        }
        if (tmp != NULL) {
            free(tmp);
        }
        if (cus != NULL) {
            free(cus);
        }
    }

    return 0;
}

I tried to compile with -g and then run gdb to find the line that caused the segmentation fault, but the error message confused me.

Program received signal SIGSEGV, Segmentation fault.
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0, loc=0x7ffff7fb04a0 <_nl_global_locale>)
    at ../stdlib/strtol_l.c:292
292     ../stdlib/strtol_l.c: No such file or directory.

I tried reinstalling gdb to get it working again, but I failed. It still shows the same error message. I later found the problem myself and marked it in the code below. I'm just curious why something like this sometimes happens when I try to debug some string functions? Like strdup, strtok, strtol, etc.. Am I missing something to install? I hope I can solve this problem completely.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

char buff[255];
#define NUM_BUCKETS 32

typedef struct Customer {
    char* email;
    char* name;
    int shoesize;
    char* food;
    struct Customer* next;
} Customer ;

unsigned long hash(char *str) {
    unsigned long hash = 0;
    int c;

    while (*str != '\0') {
        c = *str;
        hash = ((hash << 5) + hash) + (unsigned char)c;
        str++;
    }
    return hash;
}

Customer *add_friend_to_list(char *email, char *name, int shoesize, char *food, Customer *bucket) {
    Customer* customer;

    customer = malloc(sizeof(Customer));
    customer->name = strdup(name);
    customer->food = strdup(food);
    customer->shoesize = shoesize;
    customer->email = strdup(email);
    customer->next = bucket;

    return customer;
}

void add_consumer_to_hashtable(char *name, char *food, char *email, int shoesize, Customer **buckets, size_t num_buckets) {
    size_t which_bucket = hash(name) % num_buckets;
    buckets[which_bucket] = add_friend_to_list(email, name, shoesize, food, buckets[which_bucket]);
}

int main() {
    Customer* buckets[NUM_BUCKETS] = {NULL};
    int ittime = 0;
    FILE *fp = NULL;
    fp = fopen("customers.tsv", "r");
    while (true) {
        fgets(buff, 255, fp);
        if (feof(fp)) {
            break;
        }
        ittime++;
    }
    
    fclose(fp);

    fp = NULL;
    char *email = (char *)malloc(5 * sizeof(char));
    char *name = (char *)malloc(5 * sizeof(char));
    int shoesize;
    char *food = (char *)malloc(5 * sizeof(char));
    const char s[2] = "\t";
 
    fp = fopen("customers.tsv", "r");
    for (int i = 0; i < ittime + 1; i++) {        //This line cause the Segmentation Fault
        fgets(buff, 255, fp);
        char *token;

        token = strtok(buff, s);
        email = token;
        token = strtok(NULL, s);
        name = token;
        token = strtok(NULL, s);
        shoesize = atoi(token);
        token = strtok(NULL, s);
        food = token;
        add_consumer_to_hashtable(name, food, email, shoesize, buckets, NUM_BUCKETS);
    }

    fclose(fp);

    while (true) {
        char *cmd = (char *)malloc(5 * sizeof(char));

        printf("command: ");
        scanf("%s", cmd);
        if (strcmp(cmd, "add") == 0) {
            char *email1 = (char *)malloc(5 * sizeof(char));
            char *name1 = (char *)malloc(5 * sizeof(char));
            int shoesize1;
            char *food1 = (char *)malloc(5 * sizeof(char));

            printf("email address? ");
            scanf("%s", email1);
            printf("name? ");
            scanf(" %[^\n]", name1);
            printf("shoe size? ");
            scanf("%d", &shoesize1);
            printf("favorite food? ");
            scanf("%s", food1);
            add_consumer_to_hashtable(name1, food1, email1, shoesize1, buckets, NUM_BUCKETS);
            free(name1);
            free(food1);
            free(email1);
        } else if (strcmp(cmd, "lookup") == 0) {
            char *Email = (char *)malloc(5 * sizeof(char));
            printf("email address? ");
            scanf("%s", Email);
            bool exist = false;
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL)) {
                    if (cus->shoesize == EOF) {
                        break;
                    }
                    if (strcmp(cus->email, Email) == 0) {
                        printf("email: %s\n", cus->email);
                        printf("name: %s\n", cus->name);
                        printf("shoesize: %d\n", cus->shoesize);
                        printf("food: %s\n", cus->food);
                        exist = true;
                        break;
                    }
                    if (cus->next != NULL) {
                        cus = cus->next;
                    } else {
                        break;
                    }
                }
            }
            if (exist == false) {
                printf("user not found!\n");
            }
        } else if (strcmp(cmd, "delete") == 0) {
            char *Email = (char *)malloc(5 * sizeof(char));
            printf("email address? ");
            scanf("%s", Email);
            bool exist = false;
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL)) {
                    if (cus->shoesize == EOF) {
                        break;
                    }
                    if (strcmp(cus->email, Email) == 0) {
                        free(cus->email);
                        free(cus->food);
                        free(cus->name);
                        free(cus);
                        cus->shoesize = EOF;
                        cus = NULL;
                        exist = true;
                        break;
                    }
                    if (cus->next != NULL) {
                        cus = cus->next;
                    } else {
                        break;
                    }
                }
            }
            if (exist == false) {
                printf("user not found!\n");
            }
        } else if (strcmp(cmd, "list") == 0) {
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL) && ((cus->shoesize) != EOF)) {
                    printf("email: %s\n", cus->email);
                    printf("name: %s\n", cus->name);
                    printf("shoesize: %d\n", cus->shoesize);
                    printf("food: %s\n", cus->food);
                    if (cus->next != NULL) {
                        cus = cus->next;
                        printf("\n");
                    } else {
                        break;
                    }
                }
            }
        } else if (strcmp(cmd, "quit") == 0) {
            break;
        } else if (strcmp(cmd, "save") == 0) {
            fp = fopen("customers.tsv", "w");
            for (int i = 0; i < 32; i++) {
                Customer *cus = buckets[i];
                if (buckets[i] == NULL) {
                    continue;
                }
                while ((cus != NULL) && ((cus->shoesize) != EOF)) {
                    fprintf(fp, "%s\t%s\t%d\t%s", cus->email, cus->name, cus->shoesize, cus->food);
                    if (cus->next != NULL) {
                        cus = cus->next;
                        fprintf(fp, "\n");
                    } else {
                        break;
                    }
                }
            }
            fclose(fp);
        } else {
            printf("unknown command\n");
        }
    }
    for (int i = 0; i < 32; i++) {
        Customer *tmp;
        Customer *cus = buckets[i];
        if (cus == NULL) {
            continue;
        }
        if (cus->next != NULL) {
            tmp = cus;
            cus = cus->next;
        } else {
            break;
        }
        while ((tmp != NULL)) {
            if (tmp->shoesize != EOF) {
                free(tmp->email);
                free(tmp->food);
                free(tmp->name);
                free(tmp);
            }
            cus->shoesize = EOF;
            cus = NULL;
        }
        if (tmp != NULL) {
            free(tmp);
        }
        if (cus != NULL) {
            free(cus);
        }
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浸婚纱 2025-01-19 20:19:46

我尝试使用 -g 进行编译,然后运行 ​​gdb 来查找导致分段错误的行,但错误消息让我感到困惑。

错误消息的意思是:

  • 崩溃发生在 GLIBC strtol_l_internal() 函数内部
  • GDB 无法显示该函数的源代码,因为未安装 libc6-src (或类似)包。

现在,查看 strtol_l_internal() 的源代码不会有任何帮助 - 问题的根本原因是您使用不正确的参数调用它。

您应该阅读 man strtol 并验证您是否满足其先决条件。

看起来您调用了 strtol(NULL, NULL, ...),这不是有效的操作。您可以使用(gdb) up命令来找出错误调用的来源,并修复调用者。

I tried to compile with -g and then run gdb to find the line that caused the segmentation fault, but the error message confused me.

The error message means:

  • crash happened inside GLIBC strtol_l_internal() function
  • GDB can't show you the source of that function because libc6-src (or similar) package is not installed.

Now, looking at the source for strtol_l_internal() is not going to be helpful -- the root cause of the problem is that you called it with incorrect parameter.

You should read man strtol and verify that you satisfied its preconditions.

It looks like you called strtol(NULL, NULL, ...), which is not a valid thing to do. You could use (gdb) up command to find out where the wrong call came from, and fix the caller.

没有心的人 2025-01-19 20:19:46

我遇到了这个错误,结果是命令行参数没有被 gdb 传入。查看使用 gdb 调用程序时从命令行传入参数的规则。

I had this error and it turned out to be that the command line argument wan't being passed in by gdb. Look up the rules for passing in arguments from the command line when invoking a program with gdb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文