Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » GnuDebugger picks wrong source file

This thread is locked; no one can reply to it. rss feed Print
GnuDebugger picks wrong source file
Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Has anyone else ever had any problems with running their progams in GnuDebugger and then gdb picks the wrong source file?

It's really frustrating because I can't set any breakpoints and it's only useful if my program quits suddenly and then I can use a backtrace (bt full) to find out where it was. And why does that work anyway if it is picking the wrong source file?

I've been developing the same program for a while now and gdb has been picking the correct source file for a while now until a few weeks ago it started picking wrong source files!

I think it was about the same time I added #include<fstream> and #include<iostream>
but I'm not sure. The last major change I made was to add saving settings to text and data files and reading them back in.

For a while gdb would pick allegro's inline - wait a minute, I remember it started happening as soon as I added my settings class. That's when gdb started picking allegro's inline file as source. I was using the settings from memory and then I added file save functions to my code. Then gdb started picking fstream as it's source file.

Here are my compiler settings.
Compile
-Wall -mwindows -ggdb
Link
-mwindows -lalld -ggdb

I get the same results if I use -g instead of -ggdb.

I've tried setting the default directory to search for source files (in gdb)
tried just about every command I could find in gdb's help and nothing works.
Here is the list of includes I am using.

#include <allegro.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <fstream>

//using std::cout;
using std::endl;
using std::ostream;
using std::fstream;
using std::ofstream;
using std::ios;

Does anyone know how to fix this?

ImLeftFooted
Member #3,935
October 2003
avatar

I add -g3 -ggdb to all my commands as well as include all the same headers as you and have had no problems ever

Thomas Fjellstrom
Member #476
June 2000
avatar

Dustin, that -ggdb will override the -g3. -g args do not "stack". Just like -O.

Personally, I use -ggdb3, or -gstabs+ for c++ (though I'm not sure thats necessary anymore)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ImLeftFooted
Member #3,935
October 2003
avatar

Did you do a total-recompile?

CGamesPlay
Member #2,559
July 2002
avatar

Why don't you post a gdb transcript? You know you can change the source file, right?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

BAF
Member #2,981
December 2002
avatar

You probably might want to use cmath instead of math.h, cstring instead of string.h, and cstdio instead of stdio.h, if you want everything to be nice and tidy in the std namespace.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Dustin - deleted object and executable files and recompiled, no change.

CGamesPlay - Here's a copy of what happens in gdb. I've looked for how to change the source file but I don't know how to do that in gdb. All I've been able to find are commands that change the search path for source files and that doesn't help even after unloading the executable and changing the source path search directory by using
cmd->file
cmd->directory c:\\ctwoplus\\progcode\\allegro
cmd->path c:\\ctwoplus\\progcode\\allegro
cmd->file c:\\ctwoplus\\progcode\\allegro\\shadedshapes.exe
(Shown in transcript below)

1C:\CtwoPlus\progcode\allegro>gdb shadedshapes.exe
2GNU gdb 5.2.1
3Copyright 2002 Free Software Foundation, Inc.
4GDB is free software, covered by the GNU General Public License, and you are
5welcome to change it and/or distribute copies of it under certain conditions.
6Type "show copying" to see the conditions.
7There is absolutely no warranty for GDB. Type "show warranty" for details.
8This GDB was configured as "i686-pc-mingw32"...
9(gdb) list
101 // File based streams -*- C++ -*-
112
123 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
134 // Free Software Foundation, Inc.
145 //
156 // This file is part of the GNU ISO C++ Library. This library is free
167 // software; you can redistribute it and/or modify it under the
178 // terms of the GNU General Public License as published by the
189 // Free Software Foundation; either version 2, or (at your option)
1910 // any later version.
20(gdb) help
21List of classes of commands:
22 
23aliases -- Aliases of other commands
24breakpoints -- Making program stop at certain points
25data -- Examining data
26files -- Specifying and examining files
27internals -- Maintenance commands
28obscure -- Obscure features
29running -- Running the program
30stack -- Examining the stack
31status -- Status inquiries
32support -- Support facilities
33tracepoints -- Tracing of program execution without stopping the program
34user-defined -- User-defined commands
35 
36Type "help" followed by a class name for a list of commands in that class.
37Type "help" followed by command name for full documentation.
38Command name abbreviations are allowed if unambiguous.
39(gdb) help files
40Specifying and examining files.
41 
42List of commands:
43 
44add-shared-symbol-files -- Load the symbols from shared objects in the dynamic linker's link map
45add-symbol-file -- Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR>
46cd -- Set working directory to DIR for debugger and program being debugged
47core-file -- Use FILE as core dump for examining memory and registers
48directory -- Add directory DIR to beginning of search path for source files
49dll-symbols -- Load dll library symbols from FILE
50exec-file -- Use FILE as program for getting contents of pure memory
51file -- Use FILE as program to be debugged
52forward-search -- Search for regular expression (see regex(3)) from last line listed
53list -- List specified function or line
54load -- Dynamically load FILE into the running program
55path -- Add directory DIR(s) to beginning of search path for object files
56pwd -- Print working directory
57reverse-search -- Search backward for regular expression (see regex(3)) from last line listed
58search -- Search for regular expression (see regex(3)) from last line listed
59---Type <return> to continue, or q <return> to quit---
60section -- Change the base address of section SECTION of the exec file to ADDR
61set extension-language -- Set mapping between filename extension and source language
62set gnutarget -- Set the current BFD target
63show gnutarget -- Show the current BFD target
64symbol-file -- Load symbol table from executable file FILE
65
66Type "help" followed by command name for full documentation.
67Command name abbreviations are allowed if unambiguous.
68(gdb) file
69No executable file now.
70Discard symbol table from `C:\CtwoPlus\progcode\allegro/shadedshapes.exe'? (y or n) y
71No symbol file now.
72(gdb) directory c:\\ctwoplus\\progcode\\allegro
73Source directories searched: c:\\ctwoplus\\progcode\\allegro;$cdir;$cwd
74(gdb) path c:\\ctwoplus\\progcode\\allegro
75Executable and object file path: c:\\ctwoplus\\progcode\\allegro
76(gdb) file c:\\ctwoplus\\progcode\\allegro\\shadedshapes.exe
77Reading symbols from c:\ctwoplus\progcode\allegro\shadedshapes.exe...done.
78(gdb) list
791 // File based streams -*- C++ -*-
802
813 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
824 // Free Software Foundation, Inc.
835 //
846 // This file is part of the GNU ISO C++ Library. This library is free
857 // software; you can redistribute it and/or modify it under the
868 // terms of the GNU General Public License as published by the
879 // Free Software Foundation; either version 2, or (at your option)
8810 // any later version.
89(gdb)

BAF - I touched up my include files , thanks for the tip.

Any suggestions?

CGamesPlay
Member #2,559
July 2002
avatar

Try break main. Or break main.c:102. Or help break...

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

CGamesPlay -
I tried
(gdb)->break myfile.cpp:line_number
and it worked to set a breakpoint. Still outputs wrong file when using the list command though.

At least now I can set breakpoints again. Thanks.

Edit 1
-Correction , when gdb is stopped at the now sucessfully set breakpoint when I use the list command it lists my source file, but still initially only lists fstream.

CGamesPlay
Member #2,559
July 2002
avatar

Well, gdb isn't a source browser. I don't think there's any problem ::)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Thomas Fjellstrom
Member #476
June 2000
avatar

I would like so see the full output of one of these sessions where its giving the wrong info.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CGamesPlay
Member #2,559
July 2002
avatar

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Thomas Fjellstrom
Member #476
June 2000
avatar

Har. So I missed it.

But yeah, I don't think thats the right way to be using gdb...

cd /path/to/program
gdb ./program
gdb> run <arguments>
... (wait for crash)
gdb> bt
... (backtrace here)

I'd like to see that, instead of that odd mess that I saw above.

Or set proper breakpoints, and watches, and then "run". None of that other stuff should be necessary.

how can you expect it to give a valid "list" if its not even started running?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CGamesPlay
Member #2,559
July 2002
avatar

Well, I can see a good argument for having it show the main function as that is the next line about the be executed :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Well, I can see a good argument for having it show the main function as that is the next line about the be executed :)

Could be, but the actual first line is in crt0.o, some init code that eventually calls main().

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thomas - I included the output from the help commands within gdb in case I had missed something that would help me set the source file that was being read from.
My problem was that I couldn't set proper breakpoints because gdb was picking fstream for the source file. (Which I can do now with {gdb->break myfile.cpp:line_number})

This excerpt should show you better what happens when I use gdb to debug my program. I put in a line with pound signs to highlight what happens.

1 
2C:\CtwoPlus\progcode\allegro>gdb .\shadedshapes.exe
3GNU gdb 5.2.1
4Copyright 2002 Free Software Foundation, Inc.
5GDB is free software, covered by the GNU General Public License, and you are
6welcome to change it and/or distribute copies of it under certain conditions.
7Type "show copying" to see the conditions.
8There is absolutely no warranty for GDB. Type "show warranty" for details.
9This GDB was configured as "i686-pc-mingw32"...
10(gdb) list 1,30
111 // File based streams -*- C++ -*-
122
133 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
144 // Free Software Foundation, Inc.
155 //
166 // This file is part of the GNU ISO C++ Library. This library is free
177 // software; you can redistribute it and/or modify it under the
188 // terms of the GNU General Public License as published by the
199 // Free Software Foundation; either version 2, or (at your option)
2010 // any later version.
2111
2212 // This library is distributed in the hope that it will be useful,
2313 // but WITHOUT ANY WARRANTY; without even the implied warranty of
2414 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2515 // GNU General Public License for more details.
2616
2717 // You should have received a copy of the GNU General Public License along
2818 // with this library; see the file COPYING. If not, write to the Free
2919 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3020 // USA.
3121
32---Type <return> to continue, or q <return> to quit---
3322 // As a special exception, you may use this file as part of a free software
3423 // library without restriction. Specifically, if other files instantiate
3524 // templates or use macros or inline functions from this file, or you compile
3625 // this file and link it with other files to produce an executable, this
3726 // file does not by itself cause the resulting executable to be covered by
3827 // the GNU General Public License. This exception does not however
3928 // invalidate any other reasons why the executable file might be covered by
4029 // the GNU General Public License.
4130
42(gdb)
43// ############ Look here ######################################################
44(gdb) break 1
45No line 1 in file "C:/MinGW/include/c++/3.2.3/bits/fstream.tcc".
46(gdb) break shadedshapes.cpp:266
47Breakpoint 1 at 0x401978: file C:/CtwoPlus/progcode/allegro/ShadedShapes.cpp, line 266.
48(gdb) run
49Starting program: C:\CtwoPlus\progcode\allegro/.\shadedshapes.exe
50 
51Breakpoint 1, _mangled_main() ()
52 at C:/CtwoPlus/progcode/allegro/ShadedShapes.cpp:266
53warning: Source file is more recent than executable.
54 
55266 int main() {
56Current language: auto; currently c++
57(gdb) list
58261 };
59262
60263
61264
62265
63266 int main() {
64267
65268 int x = 0;
66269 char outputstring[200];
67270 char outputstring2[200];
68(gdb) quit
69The program is running. Exit anyway? (y or n) y
70 
71C:\CtwoPlus\progcode\allegro>

P.S. The progam has never had to be running for me to use list to view source code.
The problem is that it picks the wrong file.

Edit 1 - If gdb is listing the wrong file , that's where the breakpoints get set (in the wrong file). CGamesPlay gave me a workaround by using
gdb->break myfile.cpp:line_number
Then when my program reaches that breakpoint , it starts listing source from my file properly again. This is what I want to understand.

BAF
Member #2,981
December 2002
avatar

What? That looks fine to me. You do understand how #include works, right?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

BAF - Not really , no. At least not in the way the compiler puts everything together.

And I've used plenty of #includes before and until recently they've never made gdb read in the wrong source file.

Like I said , it started happening as soon as I added my settings class (basically just data storage and retrieval and an << operator function so I can put it into a text file nicely. That's when gdb started selecting some allegro inline file as my main source file. Then I added #include<fstream> and #include<iostream> and that's when gdb started picking fstream.tcc as my source file.

Look at my last post - in the transcript window at these three lines

Edgar Reynaldo said:

// ############ Look here ######################################################
(gdb) break 1
No line 1 in file "C:/MinGW/include/c++/3.2.3/bits/fstream.tcc".

Until CGamesPlay showed me how to set a breakpoint by using the file name
I couldn't set any breakpoints to debug my program.
I've never had to explicitly mention my program by name or path when setting breakpoints until recently when gdb started playing roulette with source files.

Thomas Fjellstrom
Member #476
June 2000
avatar

.tcc? Isn't that a precompiled header? Maybe that has something to do with the oddities?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ImLeftFooted
Member #3,935
October 2003
avatar

If .tcc is a precompiled header I think its safe to just delete it. Try renaming / deleting it and see if that fixes it.

Is there a settings.tcc as well?

On second thought, I think .pch is the precompiled header extension... hm...

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

But I won't be deleting anything. Don't want to reinstall Mingw.

The trick is to rename it which is effectively the same yet can be easily undone.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I knew this would cause trouble but I didn't know how. Did it for fun anyway.
renamed fstream.tcc to fstream.tcc.bak
and the compiler replied :
C:/MinGW/include/c++/3.2.3/fstream:576:29: bits/fstream.tcc: No such file or directory

fstream contains these lines

#ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
# define export
#endif
#ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
# include <bits/fstream.tcc>
#endif

#endif

And as such the program would never compile without it.
Well , I had my fun.

Dustin - no , there's no settings.tcc in my compiler directory.

Go to: