I'm having this simple c++ code:
int main() {
int x = 1;
int y = 2;
int z = x + y;
return 0;
}
Then I do g++ -g main.cpp -o main, then gdb main into gdb mode. Then I set break point by b 2, then r, then type n, immedately I get
Program received signal SIGSEGV, Segmentation fault.
0x000055555555513a in main () at main.cpp:4
4 int z = x + y;
I am very confused as this code is too simple and having nothing wrong.
Could somebody help? Thank you so much!
When I input backtrace, I got
#0 0x000055555555513a in main () at main.cpp:4
I don't know in which way it is incorrect.
If I set break point b 4, then r, then the program safely comes to line 4, but same thing happens if I use n. Very confused.
I'm running it in Debian virtual box but I don't think it's related.
Related
I am debugging a huge program using GDB and there is a SegFault in my program.
Instead of re-running the program, is it possible to switch to a previous stack frame and continue execution from there?
On Unix and Linux systems, at least, you can use gdb's handle command to tell gdb to stop the program when a signal is received (with the stop keyword) and not to pass the signal to the program (with the nopass keyword). When the program stops, you can use the return command to return a value from the current frame, then continue the program.
$ gdb -q segvtest
Reading symbols from segvtest...done.
(gdb) list 1,99999
1 #include <stdio.h>
2
3 int a()
4 {
5 int *p = 0;
6 return *p;
7 }
8
9 int main()
10 {
11 int i = a();
12 printf("a() returned %d\n", i);
13 }
(gdb) handle SIGSEGV stop nopass
Signal Stop Print Pass to program Description
SIGSEGV Yes Yes No Segmentation fault
(gdb) run
Starting program: /home/mp/segvtest
Program received signal SIGSEGV, Segmentation fault.
0x00000000080006c0 in a () at segvtest.c:6
6 return *p;
(gdb) return 12345
Make a return now? (y or n) y
#0 0x00000000080006d6 in main () at segvtest.c:11
11 int i = a();
(gdb) c
Continuing.
a() returned 12345
[Inferior 1 (process 74) exited normally]
(gdb)
is it possible to switch to a previous stack frame and continue
execution from there?
Yes, you can do it with reverse debugging.
When you get segfault, run reverse-finish to go out of the current frame in reverse direction. You will stop in the previous frame where you are about to call the function that caused a segfault.
I'm a beginner in c++ and I was trying to solve https://projecteuler.net/problem=9 . I wrote a code for it and it shows the error - Program received signal SIGSEGV, Segmentation fault.
In strcmp () (C:\Windows\syswow64\msvcrt.dll)
while debugging.
If I straightaway run the program, a dialog box appears that says "windows is checking for a solution."
I've tried not using the string function and instead of writing pytha(a,b,c)=="true" , I just wrote axa+bxb=c*c (I wrote * instead of x but here it is not showing * between the two a's so I am replacing it with x) and the code works perfectly fine. But the thing is why does it not work with the string function?
I do not see anything wrong with the code.
I've found plenty of similar questions-
1. https://www.codeproject.com/Questions/93770/what-is-this-means-Program-received-signal-SIGSEGV
According to this one, my program is referring to a memory location which it does not have access to. But I do not see anything that is restricting this code to access something.
Program received signal SIGSEGV, Segmentation fault error
3.Debug---Program received signal SIGSEGV, Segmentation fault
program received signal SIGSEGV, segmentation fault
"Program received signal SIGSEGV, Segmentation fault."
Program received signal SIGSEGV, Segmentation fault
Program received signal SIGSEGV, segmentation fault, Linked list program
None of them answer my query as I am not able to relate to the codes mentioned in them to my code.
The link numbered 5 mentions that probably the error is because of the large number of computations involved. Even I had that doubt for my code, but it works fine when I don't use the function "pytha". Also, I do not see the large number of steps involved related in any way to an error related to memory access.
Also, even if large number of steps are involved is the reason, the program should compile when given enough time. But it doesn't. It straightaway shows the error that "Windows is looking for a solution."
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string pytha(int a, int b, int c) {
if(a * a + b * b == c * c) return "true";
}
int main() {
for(int a = 1; a < 1000; a++) {
for(int b = 1; b < 1000; b++) {
for(int c = 1; c < 1000; c++) {
if(a + b + c == 1000) {
if(pytha(a, b, c) == "true")
cout << "a= " << a << " b= " << b << " c= " << c;
}
}
}
}
}
Please note that this code is a very inefficient one. The point is not to solve the question but to know why is the program not compiling.
pytha doesn't return a value on its all control flow paths.
Fix:
string pytha(int a, int b, int c)
{
if (a*a+b*b==c*c)
return "true";
return "";
}
Always compile your code with warnings enabled. For gcc and clang the compiler command line options are -Wall -Wextra -Werror.
You probably want to use bool type instead of string.
Lets say we have a structure with some variables.
Is it possible to values of those variables at a particular point of execution..?
One way might be to print each of them individually.
But my point is, is there a way to check values of all the variables in that structure at a particular point of time, without having to use printf or cout to print each variable value..?
Just wondering if this is possible atleast in gdb..!!
it is possible in gdb, no problem:
For example:
x.C
#include <iostream>
struct A {
int x;
int y;
};
int main(int argc,char **argv) {
A a;
a.x=10;
a.y=11;
std::cout << "Hello world" << std::endl;
}
compiling:
g++ -g -o x x.C
running on gdb
gdb x
(gdb) break main
Breakpoint 1 at 0x40096c: file x.C, line 10.
(gdb) run
Starting program: /home/jsantand/x
Breakpoint 1, main (argc=1, argv=0x7fffffffde98) at x.C:10
10 a.x=10;
(gdb) next
11 a.y=11;
(gdb) next
12 std::cout << "Hello world" << std::endl;
(gdb) print a
$1 = {x = 10, y = 11}
(gdb) quit
Doing that on your code, traces, etc... it will be be more difficult as C++ lacks reflection.
You could do it by hand or if you're adventurous, create something to generate automatically operator<< for your classes an structs/classes so that they provide a string representation. You need some basic C++ parser at least.
I have a complicated class for which I have written a clean printing method, more-or-less explicitly for the purposes of debugging. However, I can't seem to figure out the syntax to actually use it to print when I'm using gdb. Basically I want to be able to type something like "myObject->print()" and have it run my print method but instead I get the following error:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000000000000a1
0x00007fff814c0684 in std::ostream::sentry::sentry () The program
being debugged was signaled while in a function called from GDB. GDB
remains in the frame where the signal was received. To change this
behavior use "set unwindonsignal on" Evaluation of the expression
containing the function (wfSamplePath::print_traj(std::ostream&)) will
be abandoned.
where "wfSamplePath" is my class and "print_traj" is my print method (with std::cout as the default argument). So clearly something is wrong with how I think I can do this. I'm using gdb from within xcode 3. "myObject" is definitely in scope, as I can access some of its other methods.
The expression evaluator in GDB is quite limited, particularly with C++ expressions, so try to keep it simple. Particularly, do not use default arguments. Using cout is also probably a bad idea. So are inline functions.
I've got good results with a simple member function that returns a string. For example this code works as expected:
#include <sstream>
struct S
{
int x, y, z;
std::string debug();
};
std::string S::debug()
{
std::ostringstream os;
os << x << ", " << y << ", " << z;
return os.str();
}
int main()
{
S s;
s.x = 1;
s.y = 2;
s.z = 3;
return 0;
}
Then, compile and debug with:
$ g++ -O0 -g test.cpp
$ gdb ./a.out
....
$start
....
19 s.x = 1;
(gdb) n
20 s.y = 2;
(gdb) n
21 s.z = 3;
(gdb) n
22 return 0;
(gdb) p s.debug()
$1 = "1, 2, 3"
This code compiles and runs though gives a Microsoft compiler error that I cant fix
warning C4700: uninitialized local variable '' used.
This is in the last line of the code, I think
#include <iostream>
using namespace std;
const int DIM0 = 2, DIM1 = 3, DIM2 = 4, DIM3 = 5;
void TestDeclar();
int main(){
TestDeclar();
cout << "Done!\n";
return 0;
}
void TestDeclar(){
//24 - array of 5 floats
float xa[DIM3], xb[DIM3], xc[DIM3], xd[DIM3], xe[DIM3], xf[DIM3];
float xg[DIM3], xh[DIM3], xi[DIM3], xj[DIM3], xk[DIM3], xl[DIM3];
float xm[DIM3], xn[DIM3], xo[DIM3], xp[DIM3], xq[DIM3], xr[DIM3];
float xs[DIM3], xt[DIM3], xu[DIM3], xv[DIM3], xw[DIM3], xx[DIM3];
//6 - array of 4 pointers to floats
float *ya[DIM2] = {xa, xb, xc, xd}, *yb[DIM2] = {xe, xf, xg, xh};
float *yc[DIM2] = {xi, xj, xk, xl}, *yd[DIM2] = {xm, xn, xo, xp};
float *ye[DIM2] = {xq, xr, xs, xt}, *yf[DIM2] = {xu, xv, xw, xx};
//2 - array of 3 pointers to pointers of floats
float **za[DIM1] = {ya, yb, yc};
float **zb[DIM1] = {yd, ye, yf};
//array of 2 pointers to pointers to pointers of floats
float ***ptr[DIM0] = {za, zb};
cout << &***ptr[DIM0] << '\n';
}
You're accessing past the end of the ptr4D. DIM0 is 2, one greater than the last index of 1!
Change the last few lines to:
//array of 2 pointers to pointers to pointers of floats
float ***ptr4D[DIM0] = {za, zb};
cout << &***ptr4D[0] << '\n';
Not sure if I can help you but I tried to find out what's wrong trying to run it on my linux machine. I've compiled it on a ubuntu machine to compare and it went ok, even tellign the compiler to turn on all option warnings (passing the -Wall option). When running, I got this:
# Compiled it with -Wall to enable all warning flags and -g3 to produce extra debug information
~$ g++ -Wall stackoverflow.cpp -g3
./a.out
Segmentation fault (core dumped)
Then I've tried to debug it with GDB (GNU debugger) and got this:
(gdb) r
Starting program: /home/ubuntu/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400986 in TestDeclar () at stackoverflow.cpp:34
34 cout << &***ptr4D[DIM0] << '\n';
(gdb) s
So it appears that the problem is at the cout line. Checking your code again, DIM0's value is 2, so you're trying to access a memory address beyond the ptr4D. As user1721424 mentioned, just replace the DIM0 with 0 and it's done!
#After fixing it:
~$ ./a.out
0x7fff74cd3830
Done!
Hope it helps!