Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

deanrayj

macrumors newbie
Original poster
Apr 25, 2008
5
0
I'm pretty new to programming. I got my hands on a beginner's book for C++ with a series of basic programs to type up, and I've been using XCode 1.5 on my OS 10.3.9 Powerbook to play around with it. I got through to the last chapter, which is on inheritance and polymorphism, and I get to the last sample program, a simple Blackjack program. I type it up and compile, and the program crashes early on a segfault.

Rather than comb the program for the error, I decide that this would be a good time to figure out how to use XCode's debugger. The help pages I've been finding about XCode seem to suggest that I should be able to debug the program from the source code and see how things are linking together, so I could find the exact line where things are messing up.

However, when I open the debugger, I hit a couple of problems. First of all, the debugger doesn't load the source code. If I pause the program in operation, I get some machine code to pop up, but I can't get the debug to actually run off the source. I can even go to the source code in the list of "targets" on the sidebar of the main window and drag it into the debug window, but it'll then basically ignore that and go back to dumping me off on the source.

Second, the segfault is occurring right after I get prompted to input a "how many players?" variable. When I run the program from the debugger, I can't seem to be able to give an input for that variable. For example, if I type "1" and hit enter, the cursor will just jump to the next line and leave white space. If I pause the program, the line "Undefined command: "1". Try "help"," pops up.

I've been looking around the net for some hints on making things run, but I'm not getting anything but the basic help pages. Could I get a hand with the troubleshooting?

http://ohnoabear.com/dreamshade/debug0.jpg
This is the console window on running the program.

http://ohnoabear.com/dreamshade/debug1.jpg
This is what happens when I "build and debug" the program. The program itself does not show up in the debug window. If I enter a number for the prompt in the console window, I come back with nothing.

http://ohnoabear.com/dreamshade/debug2.jpg
If I pause the program, this occurs.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Debug->Standard I/O Log
You can do the interactive console I/O there. The debugger log will let you manually interact with gdb, the debugger.

This is not at all obvious.

-Lee
 

mysticwhiskey

macrumors newbie
Mar 31, 2008
25
0
http://ohnoabear.com/dreamshade/debug2.jpg
If I pause the program, this occurs.

The reason it's not showing you the source code is because it's currently executing code for which you don't have the source (ie in the C++ stream read function). The upper-left pane of the Debugger window shows a call stack - the # column indicates the nesting level of the call stack. If you click on #7 (main) this should show you your line of code, which is at the bottom of the stack.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Did the crash leave a core file? If not, set:

ulimit -c unlimited

in the shell and run the program again

Find the core file (usually called 'core') then in the terminal:

gdb mybuggyprogram core

once everything is loaded by gdb, type 'where' or 'bt' at the (gdb) prompt. This will give you the stack trace which tells you the sequence of events leading up to the crash.
 

deanrayj

macrumors newbie
Original poster
Apr 25, 2008
5
0
The reason it's not showing you the source code is because it's currently executing code for which you don't have the source (ie in the C++ stream read function). The upper-left pane of the Debugger window shows a call stack - the # column indicates the nesting level of the call stack. If you click on #7 (main) this should show you your line of code, which is at the bottom of the stack.

Is there any way to go by the source code? I've seen some pictures on the XCode site and such that seem to indicate that I can, but it doesn't seem to come in automatically. Clicking on the "main" line points me to a line of the machine code. Is there some way to figure out where to go from the machine code?

I appreciate the responses, I'll probably have a little more time Sunday to look at this again.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I was going to leave this point be this time around, but yeroen brought it up.

A project of this scope doesn't need an IDE. Compile and debug at the command line. There's just one source file (i question that as well, i'd separate each class into its own file, but i digress), compiling should not be a herculean effort with g++.

Code:
g++ -ostudy10_7 main.cpp
./study10_7

You might need -lstdc++ for compiling, i honestly don't know.

It will crash, so you should do what yeroen said first. You can then invoke gdb on the core file. Alternately, you could run:
Code:
gdb ./study10_7
(gdb) run
<enter your input, program dies>
(gdb) bt

The backtrace should show you where it crashed. You can use "up" to move up the stackframe and inspect what's in variables with print, etc. To see where things are going awry.

You can do all of this in XCode as well (not the ulimit part, but the gdb stuff), but it's a lot to get bogged down in for a relatively small project.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Is there any way to go by the source code? I've seen some pictures on the XCode site and such that seem to indicate that I can, but it doesn't seem to come in automatically. Clicking on the "main" line points me to a line of the machine code. Is there some way to figure out where to go from the machine code?

I appreciate the responses, I'll probably have a little more time Sunday to look at this again.

If you look at my first post above, it should let you get past the read by entering the necessary input to your program. Your program will then die, and at that point the debugger will actually be helpful. You should be able to see where things died, etc.

-Lee
 

deanrayj

macrumors newbie
Original poster
Apr 25, 2008
5
0
I was going to leave this point be this time around, but yeroen brought it up.

A project of this scope doesn't need an IDE. Compile and debug at the command line. There's just one source file (i question that as well, i'd separate each class into its own file, but i digress), compiling should not be a herculean effort with g++.

In all fairness, the book told me to go find myself a nice free IDE to use. :D

But anyway, I went back and tried five other things trying to make things work. I tried launching the debugger from the executable rather than from the source, which gave me the option to try to "fix" the problem in the source. This brought up a prompt that read something like, "Source not found. Maybe you didn't add debugging symbols?" It turns out that if you want the source to load, then you have to add at least one breakpoint into the source rather than just hoping to go step-by-step with your lines to begin with. Thanks for all the input again.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.