How can I perform line by line crash debugging to deal with "has stopped working" error in c?

-1

I have a c project compiled with GCC.When I run my test.exe program ends with "test.exe has stopped working" but compiled successfully.How can I debug my program and how can find where is my mistake?

Technique 1: I tried to printf("Successful 1..."),printf("Successful 2...") on the beginning of every single line of code but this method is frustrating.Are there tools(gdb...) to debug my code line by line for me?

printf("Successful 1...")
//code
printf("Successful 2...")
//code

How can I debug my program line by line with GDB? Can I go to the error line directly with GDB?

Edit 1: I use gdb and type run command and get following output which is not helpful(segmentation fault but where?):

Starting program: C:\Users\q\..././bin/test.exe
[New Thread 3292.0x22b8]
[New Thread 3292.0x1fb8]
Program received signal SIGSEGV, Segmentation fault.
0x7696e3e3 in ungetwc () from C:\WINDOWS\SysWOW64\msvcrt.dll

Edit 2: How to debug using gdb? doesn't contain helpful answer to my question.Not possible duplicate of my answer

Edit 3:(bt method output) I have program that contain 6 source and 6 header files.And every header and source files contain three and more than function pointers that perform some action.I tried debug with gdb bt and get following output:

#1  0x00000001 in ?? ()
#2  0x00000073 in ?? ()
#3  0x00000032 in ?? ()
#4  0x00000000 in ?? ()

My makefile:

    all: compile run
    compile :
        gcc -I ./include/ -o ./lib/... .o -c ./src/... .c
        gcc -I ./include/ -o ./lib/... .o -c ./src/... .c
        gcc -I ./include/ -o ./lib/... .o -c ./src/... .c
        gcc -I ./include/ -o ./lib/... .o -c ./src/... .c
        gcc -I ./include/ -o ./lib/... .o -c ./src/... .c
        gcc -I ./include/ -o ./bin/test ./lib/... .o ./lib/... .o ./lib/... .o ./lib/... .o ./lib/... .o  ./src/test.c

    run:
        gdb ./bin/test.exe
c
debugging
crash
gdb
asked on Stack Overflow Apr 20, 2018 by 3code • edited Apr 20, 2018 by 3code

2 Answers

1

You are getting segmentation fault and that is why your application is not running. Segmentation fault is a run time problem, even if your program compiles successfully.

In gdb after you get this:

Program received signal SIGSEGV, Segmentation fault.

type bt. The gdb's bt command will show you the back trace of your running program. It tells you which flow your program took and also the exact line which caused the segmentation fault and made your application crash.

The output of bt will be more 'readable' if you have built your application in debug mode. If you are using using gcc, you need to add -g flag to build in debug mode

answered on Stack Overflow Apr 20, 2018 by Syed
1

Sometimes, if your bug is particularly bad, it overwrites the stack and demolishes all the clues that gdb (and particularly the bt command) would need in order to show you where you were.

In that case you can try something like this:

  1. Start gdb, set a breakpoint on main, run your program, wait for the breakpoint to be hit.
  2. Single-step through your program with the n command, which steps over, not down into, functions. (That is, each called function runs all at once; you're not single-stepping recursively down into each function.) Sooner or later one of your single-steps will step over a function which crashes. Now you've narrowed it down a bit.
  3. Set a breakpoint on that function.
  4. Rerun the program.
  5. When you hit the breakpoint inside the troublesome function, again begin single-stepping. Sooner or later one of your single-steps will enter a sub-function which crashes. Now you've narrowed it down a bit more.
  6. Continue in this fashion until you've located the actual line causing the crash.
answered on Stack Overflow Apr 20, 2018 by Steve Summit • edited Apr 20, 2018 by Steve Summit

User contributions licensed under CC BY-SA 3.0