I am facing problem While using watch in gdb . I am trying to keep a
watch on variable m in my code . But for some reason i am getting the following message
no symbol m in current context. I have kept a break point at line 7 so that scope of m is known .
steps performed by me :-
1>g++ -g a.cpp
2>gdb a.out
3>(gdb)break 7
4>(gdb)watch m
Below is my program :-
# include<iostream>
# include<stdio.h>
using namespace std;
int main()
{
int m=10;
char *abc = (char *)"ritesh";
cout << abc << endl ;
m=11;
m=13;
abc=NULL;
cout << *abc <<endl;
return 0;
}
I have also seen How can I use "watch" GDB? But it did not help me much . Can someone explain this problem i am facing.Below are information related to my GNU
ritesh#ubuntu:~$ gdb a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/ritesh/a.out...done.
When you load your program into a debugger it is not being run yet. However, you try to watch a symbol, which will start to "live" in a function -- main() function -- and will "disappear" when you return from the function.
For example, in this code
void func() {
int b = 1;
++b;
cout << b << endl;
}
int main() {
int a = 1;
func();
cout << a << endl;
}
you can't set a watch of the value of a before start executing a program, and a watch on the value of b until the execution enters func().
Related
I received the following code from a colleague, broken down to the bare minimum, using Boost::Signal2 and lambda expressions. It compiles with g++ 6.x and g++ 5.4.1 (the latter with argument -std=c++11).
It should print i: 5 (should be 5)
Using a gcc 6.4.1 (or 6.1.1) cross compiler for arm32 (arm-cortexa15-linux-gnueabihf-g++) and running on such a system, the output is i: 0 (should be 5)
Other architectures (x86_64) and compilers (gcc 5.4.1) work as expected.
When I change to code to use a signal instead of a slot, everything is ok.
My questions are:
Is this code really reliably supposed to output i: 5 (should be 5), or is this code buggy and worked only accidentally?
Or is there a bug in the ARM32 gcc6 compiler? (gcc 5 works)
Code:
#include <exception>
#include <iostream>
#include <boost/signals2.hpp>
class LogBuffer : public std::streambuf
{
public:
LogBuffer()
{
}
char m_buf[242 - 20];
};
namespace boost
{
void assertion_failed(char const * p_expr,
char const *,
char const *, long)
{
std::cerr << "FAILED: " << p_expr << std::endl;
}
void assertion_failed_msg(char const *,
char const * msg,
char const *,
char const *, long)
{
std::cerr << "FAILED: " << msg << std::endl;
}
} // namespace boost
void myfunction(void)
{
{
LogBuffer b;
std::cout << "LogBuffer size: " << sizeof(LogBuffer) << std::endl;
}
int i=5;
std::cout << i << std::endl;
auto lambda = [i] { std::cerr << "i: " << i << " (should be 5)" << std::endl; };
boost::signals2::signal<void()>::slot_type slot{lambda};
slot();
}
int main(int argc, char *argv[])
{
myfunction();
}
Compile and run delivers the following output:
arm-cortexa15-linux-gnueabihf-g++ (GCC) 6.4.1 20170811
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Linux fctj-4a 4.4.109-g68c6f3c-fsm4_axm #1 SMP PREEMPT Fri Feb 2 05:37:09 UTC 2018 armv7l GNU/Linux
LogBuffer size: 256
5
i: 0 (should be 5)
That looks like a bug.
Can you reduce the reproducer? Say,
what happens if you disable optimizations
what happens if you remove the LogBuffer?
What happens if you remove the slot and use it as a signal
What happens if you keep the signals stuff and just invoke the lambda directly?
What happens if you invoke the lambda before creating the slot
What happens if you don't even create the slot, and call the lambda directly?
What happens if you further also remove the signals2 header.
What happens if you terminate in the assert handlers (perhaps you are getting asserts at a time when std::cout is not yet initialized//available)
If you reduce it to the simplest possible core and still have the failure, you will at least know whether to file a bug at Boost or GCC
This is what I already did:
-O0, -O1: bug does not show up i: 5 (should be 5)
-O2: bug shows up 0 (should be 5)
remove Logbuffer: 5 (should be 5)
using a signal Signal: 0 should be 5
calling lambda directly (5 should be 5)
using i after calling slot (e.g printing): 5 (should be 5)
using i[100]: first element becomes zero, others unaffected
using x86_64 compiler : 5 should be 5
Others will follow. I am not sure how I can easily remove the signals2 header
Reiner
Please see how gdb fails to print the global variable dictionary in the code below:
m#m-X555LJ ~ $ cat 148.cc
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <utility>
#include <algorithm>
#include <sstream>
using namespace std;
using letters = array<int, 26>;
letters emptylet = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
vector<pair<string, letters>> dictionary;
void backtrace(string &orig, vector<string> &phrase, vector<string> &acc, letters &curr, letters &goal, decltype(dictionary)::iterator it) {
if(it == dictionary.end())
return;
for(int i = 0; i < it->second.size(); i++)
curr[i] += it->second[i];
acc.push_back(it->first);
bool all_same = true;
for(int i = 0; i < curr.size(); i++)
if(curr[i] > goal[i])
goto clean;
else if(curr[i] != goal[i])
all_same = false;
if(all_same) {
if(acc != phrase) {
cout << orig << " =";
for(string word: acc)
cout << ' ' << word;
cout << '\n';
}
} else
backtrace(orig, phrase, acc, curr, goal, it+1);
clean:
acc.pop_back();
for(int i = 0; i < it->second.size(); i++)
curr[i] -= it->second[i];
backtrace(orig, phrase, acc, curr, goal, it+1);
}
int main()
{
while((cin>>ws).peek()!='#') {
string word;
cin >> word;
letters let = emptylet;
for(char c: word)
let[c-'A']++;
dictionary.push_back(make_pair(word, let));
}
while((cin>>ws).peek()!='#') {
string s;
getline(cin, s);
string orig = s;
stringstream ss(s+'\n');
vector<string> phrase;
while(cin>>s){
phrase.push_back(s);
}
sort(phrase.begin(), phrase.end());
letters let = emptylet;
for(string wrd: phrase)
for(char c: wrd)
let[c-'A']++;
vector<string> empt;
backtrace(orig, phrase, empt, emptylet, let, dictionary.begin());
}
}
m#m-X555LJ ~ $ g++ -g -std=c++11 -o 148 148.cc
m#m-X555LJ ~ $ cat 148.in
ABC
AND
DEF
DXZ
K
KX
LJSRT
LT
PT
PTYYWQ
Y
YWJSRQ
ZD
ZZXY
#
ZZXY ABC DEF
SXZYTWQP KLJ YRTD
ZZXY YWJSRQ PTYYWQ ZZXY
#
m#m-X555LJ ~ $ gdb ./148
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./148...done.
(gdb) break 52
Breakpoint 1 at 0x401d0f: file 148.cc, line 52.
(gdb) r < 148.in
Starting program: /home/m/148 < 148.in
Breakpoint 1, main () at 148.cc:52
52 while((cin>>ws).peek()!='#') {
(gdb) p dictionary
No symbol "dictionary" in current context.
(gdb) p di
dictionary[abi:cxx11] dir_data dirfd dirstream.h div divmod_1.c
difftime dirent dirfd.c disallow_malloc_check div.c divrem.c
difftime.c dirent.h dirname distinguish_extX div_t
digits_dots.c dirent64 dirname.c distinguish_extX.isra.0 divide
(gdb) p dictionary[abi:cxx11]
No symbol "dictionary" in current context.
(gdb) quit
A debugging session is active.
Inferior 1 [process 3815] will be killed.
Quit anyway? (y or n) y
m#m-X555LJ ~ $ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
m#m-X555LJ ~ $
I do not understand this. Why is this the case? Is there something obvious about gdb I do not know?
To my best understanding, p dictionary should print the contents of the std::vector global variable called dictionary.
And this is not the first time something like that happens to me. I remember gdb not seeing functions, variables, etc. This time I decided to record the case.
You can see the problem when you use p ditabtab (as you did) -- the symbol produced by gcc is called "dictionary[abi:cxx11]", which you can print by entering p 'dicttab -- note the quotes around the name to get gdb to not try to interpret the [..] as an expression.
int main()
{
typedef unsigned char a4[4];
a4 p1;
a4& p2 = p1;
p2[1]=1;
cout<<sizeof(p2);
return p2[1];
}
Compile, start gdb and put breakpoint on return. If you type p sizeof(p2), gdb will print 8 instead of 4 which will be printed if you start the program. If you write in gdb p sizeof(*p2), the output is 4 (the size of array). I think this is because gdb treats p2 as pointer(reference is implemented behind the scene as pointer).
Tested with compilers GCC 4.8.2 and Clang 4.3 on GDB 7.7 linux arch., ubuntu 13.10,
Is this correct or a bug in gdb?
Here's a modified version of your program. I've changed the array size from 4 to 17 to ensure that its size is distinguishable from anything else. I've also changed the type and variable names to make the code easier to follow, and added #include <iostream> so it actually compiles. I've also removed some unnecessary stuff.
#include <iostream>
int main()
{
typedef unsigned char char17[17];
char17 arr17;
char17& arr17_ref = arr17;
std::cout << "sizeof(arr17) = "
<< sizeof arr17
<< ", sizeof(arr17_ref) = "
<< sizeof(arr17_ref)
<< "\n";
return 0;
}
When I compile and run it on my system, the output is 17.
When I run it under gdb, I get 8 (the size of a pointer on my system):
$ gdb ./c
GNU gdb (GDB) 7.5-ubuntu
[snip]
Reading symbols from /home/kst/c...done.
(gdb) b 12
Breakpoint 1 at 0x40097e: file c.cpp, line 12.
(gdb) r
Starting program: /home/kst/c
sizeof(arr17) = 17, sizeof(arr17_ref) = 17
Breakpoint 1, main () at c.cpp:12
12 return 0;
(gdb) p sizeof(arr17)
$1 = 17
(gdb) p sizeof(arr17_ref)
$2 = 8
(gdb) c
Continuing.
[Inferior 1 (process 23420) exited normally]
(gdb) q
$
Yes, this is a bug in gdb. gdb is supposed to evaluate expressions as they'd be evaluated in a running program; in this case, it fails to do so.
(I'm using gcc 4.7.2 and gdb 7.5 on Linux Mint 14.)
UPDATE :
The OP submitted a bug report: https://sourceware.org/bugzilla/show_bug.cgi?id=16675
and it's been fixed. The patch was approved and committed 2014-04-14. I still see the bug in gdb 7.7.1, but it's fixed in 7.11.1.
I am new to program world. I am learning C with Dev-cpp 5.6.1
I had a problem with my Debugger (GNU gdb (GDB) 7.6.1). When I debug any program, the debugger warned me
Single stepping until exit from function main,
which has no line number information.
and
__mingw_CRTStartup ()
at ../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:260
260 ../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c: No such file or directory.
The problem happened before. I solved it by reinstalling Dev-Cpp (also reset old configure). But after a little time the problem came back again.
Example code:
#include <stdio.h>
int main(void)
{
int a, b;
printf("Please give me number 1: ");
scanf("%d", &a);
printf("Please give me number 2: ");
scanf("%d", &b);
printf("Sum = %d", a + b);
}
The debugger warned me:
C:\Users\Nam\Dropbox\code>gdb sum.exe
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:\Users\Nam\Dropbox\code\sum.exe...done.
(gdb) b main
Breakpoint 1 at 0x4016b3
(gdb) n
The program is not being run.
(gdb) r
Starting program: C:\Users\Nam\Dropbox\code/sum.exe
[New Thread 7148.0x1b6c]
Breakpoint 1, 0x004016b3 in main ()
(gdb) n
Single stepping until exit from function main,
which has no line number information.
Please give me number 1: 3
Please give me number 2: 4
Sum = 7__mingw_CRTStartup ()
at ../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:260
260 ../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c: No such file or di
rectory.
(gdb)
I couldn't know how to fix it.
Anyone helps me please :(. Thanks in advance
I couldn't know how to fix it.
I don't think it is necessary to fix it. You got this message since you had already returned from main() and now you are not in your code, it is mingw code that calls your main(). I did the same test as you and this is backtrace after finishing main:
10 printf("Sum = %d", a + b);
(gdb)
Sum = 311 }
(gdb) bt
#0 main () at t.c:11
(gdb) n
__mingw_CRTStartup () at ../mingw/crt1.c:250
250 ../mingw/crt1.c: No such file or directory.
(gdb) bt
#0 __mingw_CRTStartup () at ../mingw/crt1.c:250
#1 0x00401284 in mainCRTStartup () at ../mingw/crt1.c:264
(gdb) n
252 in ../mingw/crt1.c
(gdb) n
[Inferior 1 (process 1448) exited normally]
(gdb)
Again - you don't have to debug mingw startup code. Just give gdb command "continue" so that it can finish executing your process.
I have found source of this __mingw_CRTStartup here http://gitorious.org/mingw/mingw-runtime/source/be97f73714b4e267e5903fc9bdeb0f23fcc3ac8f:crt1.c#L200. You can take a look at what steps mingw library does after returning from main:
static void __attribute__((noreturn))
__mingw_CRTStartup (void)
{
int nRet;
/* skipped some lines ... */
nRet = main (_argc, _argv, environ);
/*
* Perform exit processing for the C library. This means
* flushing output and calling 'atexit' registered functions.
*/
_cexit ();
ExitProcess (nRet);
}
Some useful links:
1) https://stackoverflow.com/a/4988376/184968
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm making a simulation for severals queues.
I'm supposed to input number of queues in the beginning and then simulate all thoose.
Output for each queue each "round" is meantime, total number of served and size of each queue.
My program is crashing and dosen't respond.
It writes out the first queue och then crashes...
Help!
I think my calculations is wrong to but i don't know since it's all crashing.
Here it is:
#include <iostream>
#include <cstdlib>
#include <list>
#include <ctime>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
#include<dos.h>
#include<windows.h>
using namespace std;
class Customer
{
public:
int servicet;
int served;
Customer()
{
servicet= rand()%150+30;
}
int getServicetime()
{
return servicet;
}
int getServed()
{
return served;
}
int decreaseServeTime()
{
servicet --;
}
};
int totServed=0;
int queues=0;
int inLine=0;
int totTime=0;
int smallestQueue=0;
int temp=0;
int ran=0;
double mean=0;
int served=0;
int serviceTime=0;
int help=0;
int sim=0;
int n=0;
using namespace std;
int main()
{
cout<<"Number of Cashiers?: "<<endl;
cin >> queues;
cout <<"How long simulation?: "<<endl;
cin >> sim;
list<Customer> *cashiers[queues];
list<Customer> *cust;
for(int i=0; i<=queues; i++)
{
cust = new list<Customer>;
cashiers[i] = cust;
}
srand(time(0));
while(n<sim)
{
Sleep(2000);
ran= rand()%4;
smallestQueue = cashiers[0] ->size();
for(int j=0; j<ran; j++)
{
for(int k=0; k<queues; k++)
{
temp = cashiers[k]->size();
if(temp<=smallestQueue)
{
smallestQueue = temp;
help=k;
}
}
Customer C;
cashiers[help]->push_back(C);
inLine++;
}
for(int i=0; i<queues; i++)
{
if(serviceTime>0)
{
serviceTime = cashiers[i]->front().getServicetime();
cashiers[i]->front().decreaseServeTime();
}
else if(serviceTime==0)
{
cashiers[i]->pop_front();
served++;
}
}
totTime++;
int cash=1;
for(int i=0; i<queues; i++)
{
if(inLine!=0)
{
cout <<"Kassa: "<<cash<<endl;
inLine = cashiers[i]->size();
mean = (totTime/inLine);
totServed +=served;
cash++;
}
cout <<inLine<<" "<<mean<<" "<<totServed<<endl;
}
n++;
}
system("pause");
}
I recommend that you use a program such as Application Verifier to find the issue that is causing the crash:
http://www.microsoft.com/en-us/download/details.aspx?id=20028
It's important that you learn how to debug your software and understand what is going on. Please run your code in a debugger (Visual Studio, Eclipse) and take a look at where it stops. If you used Application Verifier, then it will likely stop where the issue originates. Take a look at the variables and see whether they make sense. Look at whether you're accessing memory locations you shouldn't.
To use Application Verifier with Visual Studio, install it then find the appVerifier.exe in the System32 folder in C:\Windows. Then open the file and point it to your executable. Enable what you think are the proper checks. Then run it in visual Studio.
A good place to start is with a debugger (e.g. gdb). First we compile with debugging enabled (g++ -ggdb) and try running in the debugger,
$ g++ hi.cpp -ggdb
$ gdb ./a.out
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ben/a.out...done.
(gdb) run
Starting program: /home/ben/a.out
Number of Cashiers?:
5
How long simulation?:
5
Kassa: 1
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401827 in std::_List_const_iterator<Customer>::operator++ (
this=0x7fffffffdd10) at /usr/include/c++/4.7/bits/stl_list.h:236
236 _M_node = _M_node->_M_next;
(gdb) backtrace
#0 0x0000000000401827 in std::_List_const_iterator<Customer>::operator++ (
this=0x7fffffffdd10) at /usr/include/c++/4.7/bits/stl_list.h:236
#1 0x0000000000401665 in std::__distance<std::_List_const_iterator<Customer> >
(__first=..., __last=...)
at /usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:82
#2 0x0000000000401492 in std::distance<std::_List_const_iterator<Customer> > (
__first=..., __last=...)
at /usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:118
#3 0x000000000040135b in std::list<Customer, std::allocator<Customer> >::size
(this=0x604010) at /usr/include/c++/4.7/bits/stl_list.h:855
#4 0x0000000000401122 in main () at hi.cpp:125
Here we see that the program crashed with a segmentation fault in a
function of the std::list. After programming for a while, you will
gain an intuition that this is probably due to your program trampling
on some memory it shouldn't be. Having identified roughly the nature
of the problem, we will now switch to valgrind, a tool for tracking
down specifically this sort of issue.
$ valgrind ./a.out
==13751== Memcheck, a memory error detector
==13751== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==13751== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==13751== Command: ./a.out
==13751==
Number of Cashiers?:
5
How long simulation?:
5
Kassa: 1
==13751== Invalid read of size 8
==13751== at 0x401422: std::list<Customer, std::allocator<Customer> >::begin() const (stl_list.h:749)
==13751== by 0x40134F: std::list<Customer, std::allocator<Customer> >::size() const (stl_list.h:855)
==13751== by 0x401121: main (hi.cpp:125)
==13751== Address 0x5a06040 is 0 bytes inside a block of size 16 free'd
==13751== at 0x4C2A44B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13751== by 0x4018E7: __gnu_cxx::new_allocator<std::_List_node<Customer> >::deallocate(std::_List_node<Customer>*, unsigned long) (new_allocator.h:100)
==13751== by 0x4017D9: std::_List_base<Customer, std::allocator<Customer> >::_M_put_node(std::_List_node<Customer>*) (stl_list.h:339)
==13751== by 0x4015C0: std::list<Customer, std::allocator<Customer> >::_M_erase(std::_List_iterator<Customer>) (stl_list.h:1549)
==13751== by 0x4013E9: std::list<Customer, std::allocator<Customer> >::pop_front() (stl_list.h:983)
==13751== by 0x40108B: main (hi.cpp:113)
==13751==
==13751==
==13751== Process terminating with default action of signal 8 (SIGFPE)
==13751== Integer divide by zero at address 0x402CCCE98
==13751== at 0x40113C: main (hi.cpp:126)
Here we see valgrind telling us that your program attempted a read
operation on unallocated memory. In particular, this seems to be
happening as a result of a pop_front operation. Looking at the
source, you indeed attempt to pop from cashiers[i] without first
checking it's size.
We can add an appropriate check,
...
else if(serviceTime==0)
{
if (!cashiers[i]->empty()) {
cashiers[i]->pop_front();
served++;
}
}
...
The actual cause of the crash, however, is a division by zero in computing the
mean, as noted at the end of valgrind's output. This is due to the fact that the case where no Customers are inLine is not handled when computing mean.