I am writing a C program that loads two tables as matrices to perform operations on using the open-source GSL library. When compiled, the program prompts for the two files needed and enters an infinite loop. I am trying to use gdb to debug the issue; however, gdb cannot even run into the main file as I receive a SIGSEV error. Backtracing doesn't work since the main function doesn't get loaded. Below are the three files I'm working with (matrixoperations.c, datafile.c, and datafile.h). The relevant parts of each file are copied, and the repository is located here: https://github.com/DogIsGreat/C_Science_Lab
------ matrixoperations.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);
......}
-------- datafile.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);
}
------- datafile.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
I am currently using a Mac and debugging through a Linux container that runs gdb and Valgrind. Both Valgrind and gdb encounter a sigsegv failure before reaching the main function, preventing further progress. However, when running the compiled program with only the options -Wall and -g, it works until attempting to load the matrix. I expect to be able to debug into the main file and identify the issues with the load_data function, although that is not the focus of this question. I need to be able to debug effectively.
----- Valgrind Output -----
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
This might be a rabbit hole issue. It seems that in order to debug on a mac you need administrator privileges no matter what the program you use to debug is. There is a developer group an admin can add you too.