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?
I add -g3 -ggdb to all my commands as well as include all the same headers as you and have had no problems ever
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)
Sorry dudes no luck with -ggdb3 or -gstabs+ ,
gdb still insists my source file is fstream.
[center]:'([/center]
By the way I am using GDB 5.2.1
Did you do a total-recompile?
Why don't you post a gdb transcript? You know you can change the source file, right?
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.
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)
1 | C:\CtwoPlus\progcode\allegro>gdb shadedshapes.exe |
2 | GNU gdb 5.2.1 |
3 | Copyright 2002 Free Software Foundation, Inc. |
4 | GDB is free software, covered by the GNU General Public License, and you are |
5 | welcome to change it and/or distribute copies of it under certain conditions. |
6 | Type "show copying" to see the conditions. |
7 | There is absolutely no warranty for GDB. Type "show warranty" for details. |
8 | This GDB was configured as "i686-pc-mingw32"... |
9 | (gdb) list |
10 | 1 // File based streams -*- C++ -*- |
11 | 2 |
12 | 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 |
13 | 4 // Free Software Foundation, Inc. |
14 | 5 // |
15 | 6 // This file is part of the GNU ISO C++ Library. This library is free |
16 | 7 // software; you can redistribute it and/or modify it under the |
17 | 8 // terms of the GNU General Public License as published by the |
18 | 9 // Free Software Foundation; either version 2, or (at your option) |
19 | 10 // any later version. |
20 | (gdb) help |
21 | List of classes of commands: |
22 | |
23 | aliases -- Aliases of other commands |
24 | breakpoints -- Making program stop at certain points |
25 | data -- Examining data |
26 | files -- Specifying and examining files |
27 | internals -- Maintenance commands |
28 | obscure -- Obscure features |
29 | running -- Running the program |
30 | stack -- Examining the stack |
31 | status -- Status inquiries |
32 | support -- Support facilities |
33 | tracepoints -- Tracing of program execution without stopping the program |
34 | user-defined -- User-defined commands |
35 | |
36 | Type "help" followed by a class name for a list of commands in that class. |
37 | Type "help" followed by command name for full documentation. |
38 | Command name abbreviations are allowed if unambiguous. |
39 | (gdb) help files |
40 | Specifying and examining files. |
41 | |
42 | List of commands: |
43 | |
44 | add-shared-symbol-files -- Load the symbols from shared objects in the dynamic linker's link map |
45 | add-symbol-file -- Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR> |
46 | cd -- Set working directory to DIR for debugger and program being debugged |
47 | core-file -- Use FILE as core dump for examining memory and registers |
48 | directory -- Add directory DIR to beginning of search path for source files |
49 | dll-symbols -- Load dll library symbols from FILE |
50 | exec-file -- Use FILE as program for getting contents of pure memory |
51 | file -- Use FILE as program to be debugged |
52 | forward-search -- Search for regular expression (see regex(3)) from last line listed |
53 | list -- List specified function or line |
54 | load -- Dynamically load FILE into the running program |
55 | path -- Add directory DIR(s) to beginning of search path for object files |
56 | pwd -- Print working directory |
57 | reverse-search -- Search backward for regular expression (see regex(3)) from last line listed |
58 | search -- Search for regular expression (see regex(3)) from last line listed |
59 | ---Type <return> to continue, or q <return> to quit--- |
60 | section -- Change the base address of section SECTION of the exec file to ADDR |
61 | set extension-language -- Set mapping between filename extension and source language |
62 | set gnutarget -- Set the current BFD target |
63 | show gnutarget -- Show the current BFD target |
64 | symbol-file -- Load symbol table from executable file FILE |
65 | |
66 | Type "help" followed by command name for full documentation. |
67 | Command name abbreviations are allowed if unambiguous. |
68 | (gdb) file |
69 | No executable file now. |
70 | Discard symbol table from `C:\CtwoPlus\progcode\allegro/shadedshapes.exe'? (y or n) y |
71 | No symbol file now. |
72 | (gdb) directory c:\\ctwoplus\\progcode\\allegro |
73 | Source directories searched: c:\\ctwoplus\\progcode\\allegro;$cdir;$cwd |
74 | (gdb) path c:\\ctwoplus\\progcode\\allegro |
75 | Executable and object file path: c:\\ctwoplus\\progcode\\allegro |
76 | (gdb) file c:\\ctwoplus\\progcode\\allegro\\shadedshapes.exe |
77 | Reading symbols from c:\ctwoplus\progcode\allegro\shadedshapes.exe...done. |
78 | (gdb) list |
79 | 1 // File based streams -*- C++ -*- |
80 | 2 |
81 | 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 |
82 | 4 // Free Software Foundation, Inc. |
83 | 5 // |
84 | 6 // This file is part of the GNU ISO C++ Library. This library is free |
85 | 7 // software; you can redistribute it and/or modify it under the |
86 | 8 // terms of the GNU General Public License as published by the |
87 | 9 // Free Software Foundation; either version 2, or (at your option) |
88 | 10 // any later version. |
89 | (gdb) |
BAF - I touched up my include files , thanks for the tip.
Any suggestions?
Try break main. Or break main.c:102. Or help break...
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.
Well, gdb isn't a source browser. I don't think there's any problem
I would like so see the full output of one of these sessions where its giving the wrong info.
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?
Well, I can see a good argument for having it show the main function as that is the next line about the be executed
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 - 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 | |
2 | C:\CtwoPlus\progcode\allegro>gdb .\shadedshapes.exe |
3 | GNU gdb 5.2.1 |
4 | Copyright 2002 Free Software Foundation, Inc. |
5 | GDB is free software, covered by the GNU General Public License, and you are |
6 | welcome to change it and/or distribute copies of it under certain conditions. |
7 | Type "show copying" to see the conditions. |
8 | There is absolutely no warranty for GDB. Type "show warranty" for details. |
9 | This GDB was configured as "i686-pc-mingw32"... |
10 | (gdb) list 1,30 |
11 | 1 // File based streams -*- C++ -*- |
12 | 2 |
13 | 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 |
14 | 4 // Free Software Foundation, Inc. |
15 | 5 // |
16 | 6 // This file is part of the GNU ISO C++ Library. This library is free |
17 | 7 // software; you can redistribute it and/or modify it under the |
18 | 8 // terms of the GNU General Public License as published by the |
19 | 9 // Free Software Foundation; either version 2, or (at your option) |
20 | 10 // any later version. |
21 | 11 |
22 | 12 // This library is distributed in the hope that it will be useful, |
23 | 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 | 15 // GNU General Public License for more details. |
26 | 16 |
27 | 17 // You should have received a copy of the GNU General Public License along |
28 | 18 // with this library; see the file COPYING. If not, write to the Free |
29 | 19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
30 | 20 // USA. |
31 | 21 |
32 | ---Type <return> to continue, or q <return> to quit--- |
33 | 22 // As a special exception, you may use this file as part of a free software |
34 | 23 // library without restriction. Specifically, if other files instantiate |
35 | 24 // templates or use macros or inline functions from this file, or you compile |
36 | 25 // this file and link it with other files to produce an executable, this |
37 | 26 // file does not by itself cause the resulting executable to be covered by |
38 | 27 // the GNU General Public License. This exception does not however |
39 | 28 // invalidate any other reasons why the executable file might be covered by |
40 | 29 // the GNU General Public License. |
41 | 30 |
42 | (gdb) |
43 | // ############ Look here ###################################################### |
44 | (gdb) break 1 |
45 | No line 1 in file "C:/MinGW/include/c++/3.2.3/bits/fstream.tcc". |
46 | (gdb) break shadedshapes.cpp:266 |
47 | Breakpoint 1 at 0x401978: file C:/CtwoPlus/progcode/allegro/ShadedShapes.cpp, line 266. |
48 | (gdb) run |
49 | Starting program: C:\CtwoPlus\progcode\allegro/.\shadedshapes.exe |
50 | |
51 | Breakpoint 1, _mangled_main() () |
52 | at C:/CtwoPlus/progcode/allegro/ShadedShapes.cpp:266 |
53 | warning: Source file is more recent than executable. |
54 | |
55 | 266 int main() { |
56 | Current language: auto; currently c++ |
57 | (gdb) list |
58 | 261 }; |
59 | 262 |
60 | 263 |
61 | 264 |
62 | 265 |
63 | 266 int main() { |
64 | 267 |
65 | 268 int x = 0; |
66 | 269 char outputstring[200]; |
67 | 270 char outputstring2[200]; |
68 | (gdb) quit |
69 | The program is running. Exit anyway? (y or n) y |
70 | |
71 | C:\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.
What? That looks fine to me. You do understand how #include works, right?
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
// ############ 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.
.tcc? Isn't that a precompiled header? Maybe that has something to do with the oddities?
I haven't the slightest clue , so I'm going to try updating gdb (6.6 is out now)
and look over the online manual.
GDB: The GNU Project Debugger
GDB Documentation
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...
fstream.tcc is a bunch of templates which I don't understand yet anyway.
But I won't be deleting anything. Don't want to reinstall Mingw.
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.
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.