C 程序运行,但 GDB 在 main 之前失败。使用 GSL

问题描述 投票:0回答:1

我正在编写一个 C 程序,它将两个表作为矩阵加载,以使用开源 GSL 库执行操作。编译时,程序提示输入需要的两个文件,并进入无限循环。我正在尝试使用 gdb 来调试该问题;但是,当我收到 SIGSEV 错误时,gdb 甚至无法运行到主文件。由于主函数未加载,因此回溯不起作用。下面是我正在使用的三个文件(matrixoperations.c、datafile.c 和 datafile.h)。每个文件的相关部分都被复制,存储库位于此处: https://github.com/DogIsGreat/C_Science_Lab

------矩阵运算.c -----------

#include "datafile.h"
#include <stdio.h>
#include <string.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_errno.h>

int main(){
    
    int M = 2;
    int N = 2;
    int O = 0;
    int P = 0;
    char file1[256];
    char file2[256];
    
    printf("Please specify the name of the csv for the first matrix. \n");
    scanf("%255s", file1);
    getchar();
    printf("Please specify the name the csv file for the second matrix. \n");
    scanf("%255s", file2);
    getchar();
    printf("%s\n", file1);

    gsl_matrix *A = gsl_matrix_alloc(M,N);
    gsl_matrix *B = gsl_matrix_alloc(O,P);

    // File loading of Matrix Values.
    count_rows_cols(file1, &M, &N);
    count_rows_cols(file2, &O, &P);

    if( M == N && N == O && N == P){
        load_data(file1, A);
        load_data(file2, B);
    } else {
        printf("You must provide 2 square matrixes for this program as of now.\n");
    }

    //gsl_matrix_add(A, B);
    gsl_matrix_mul_elements(A,B);
    double max = gsl_matrix_max(A);
    gsl_matrix_transpose(B);

 ......}

-------- 数据文件.c --------

#include "datafile.h"
#include "dbg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_errno.h>


void count_rows_cols(const char *filename, int *rows, int *cols){
    FILE *file = fopen(filename, "r");
    if (!file){
        perror("Unable to open the file");
        log_err("Unable to open file: name = %s ", filename);
        exit(1);
    }

    //char buffer[1024];
    char* buffer= malloc(1024*sizeof(char));
    int row_count = 0;
    int col_count =0;

    while(fgets(buffer, 1024, file)){
        row_count++;
        if (row_count == 1){
            char *token = strtok(buffer, ",");
            while (token){
                col_count++;
                token = strtok(NULL, ",");
            }
        }
    }
    fclose(file);

    *rows = row_count;
    *cols = col_count;
    if(row_count != col_count){
        log_err("Not a square matrix: row:column =  %d:%d name = %s ", row_count, col_count, filename);
    }
    free(buffer);

}

void load_data(const char *filename, gsl_matrix *m){
    FILE *file = fopen(filename, "r");
    if(!file){
        perror("Unable to open the file");
        log_err("Unable to open file: name = %s ", filename);
        exit(1);
    }

    int row = 0; 
    //char buffer[1024];
    char* buffer= malloc(1024*sizeof(char));



    while (fgets(buffer, 1024, file)){
        int col = 0;
        char *value = strtok(buffer, ",");

        while (value){
            if (row >= 0 && row < (int)m->size1 && col >= 0 && col < (int)m->size2) {
                gsl_matrix_set(m, row, col, atof(value));
                value = strtok(NULL, ",");
                col++;
            } else {
            log_err("Attempt to access [%d, %d] out of matrix bounds.\n", row, col);
            }
        }
        row++;
    }
    fclose(file);
    free(buffer);
}

-----数据文件.h -------

#ifndef __datafile_h__
#define __datafile_h__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_matrix.h>

void count_rows_cols(const char *filename, int *rows, int *cols);

void load_data(const char *filename, gsl_matrix *m);

#endif

我目前使用 Mac 并通过运行 gdb 和 Valgrind 的 Linux 容器进行调试。 Valgrind 和 gdb 在到达 main 函数之前都会遇到 sigsegv 故障,从而阻止进一步的进展。但是,当仅使用选项 -Wall 和 -g 运行编译的程序时,它会一直工作,直到尝试加载矩阵。我希望能够调试主文件并识别 load_data 函数的问题,尽管这不是这个问题的重点。我需要能够有效地调试。

----- Valgrind 输出-----

make: Circular build/ScienceDog <- build/ScienceDog dependency dropped.
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./build/ScienceDog
==232== Memcheck, a memory error detector
==232== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==232== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==232== Command: ./build/ScienceDog
==232== 
==232== 
==232== Process terminating with default action of signal 11 (SIGSEGV)
==232==  Bad permissions for mapped region at address 0x108000
==232==    at 0x108000: ??? (in /workspace/C_Science_Lab/build/ScienceDog)
==232== 
==232== HEAP SUMMARY:
==232==     in use at exit: 0 bytes in 0 blocks
==232==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==232== 
==232== All heap blocks were freed -- no leaks are possible
==232== 
==232== For lists of detected and suppressed errors, rerun with: -s
==232== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
make: *** [Makefile:55: memcheck] Segmentation fault
c debugging gdb valgrind gsl
1个回答
0
投票

这可能是一个兔子洞问题。看来,为了在 Mac 上调试,无论您用来调试的程序是什么,您都需要管理员权限。有一个开发者小组,管理员也可以添加您。

© www.soinside.com 2019 - 2024. All rights reserved.