How to start debugging? - c

I have learned C and I would like to start to improve open source software. I would like to hack away one irritating bug in GTK+. To see the bug I need to use Gedit.
How can I download the sources of GTK+ and gedit and compile both of them so that I can see where the bug is? And I have never used any debugger in Linux so is there somewhere a tutorial for that?

You can get information about downloading and compiling of gedit here:
http://projects.gnome.org/gedit/developers.html
My Debugger of choice is GDB:
http://www.gnu.org/software/gdb/
GDB is a console application that may be hard to handle for a beginner. Try an IDE like Eclipse that provides a GUI to GDB.

Almost all Linux debuggers are front-ends for or adaptations of the gdb debugger. You should therefore learn how to use this first, preferably by starting on
small programs of your own construction, not giant FOSS codebases. The gdb manual,
available from here is actually a pretty good tutorial.

The information on building and installing GTK+ should be here:
http://www.gtk.org/development.html
The sources should be here:
http://www.gtk.org/download-linux.html
You can check out gdb:
http://www.gnu.org/software/gdb/ That's a pretty standard linux debugger. I would spend time with it on something simple first, or get an IDE that uses it. Learning gdb can be worth the time though.

gdb is a dinosaur which should long since have been made extinct. Debugging is one area where (gasp!) Windows beats Unix. Having got that off my chest, I advise you to start with the Data Display Debugger (DDD) graphical front end to gdb. Yes, the GUI is quaint, but you will be far more productive, quicker, than if you start with gdb.
Also don't overlook valgrind for finding and diagnosing memory errors. The KDE project actually mandates use of valgrind; I'm not sure about Gnome.

It is possible that you won't see the bug if you download the latest gedit and gtk+ sources. It might be fixed in the latest sources or the latest sources might be different enough to not trigger your bug.
What Linux distribution are you running?
First of all I'd suggest consulting your distribution's bug database to see if someone has filed a similar bug. If you don't find anything, I'd suggest using your distribution's tools to obtain the source that corresponds to the binary that you have installed on your sytem (e.g. apt-get source libgtk-2.0 on Debian or Ubuntu).
Also, your distribution might also make a debug package available (e.g. libgtk2.0-0-dbg on Debian) which will let you run a debugger on the binary that you already have without requiring the source. While this is no substitute for having the sources, it can be useful for running valgrind or making sense out of a core file.

Related

Valgrind and GDB substitutes for Mac?

Newbie coder. Mainly work with use a text editor (sublime/atom) with the command line.Usually code in C but will be moving on to Java and Databases soon.
I want to start use Valgrind and GDB (the former i understand is good practice for detecting memory issues). However i understand that these are not available as standard tools on Mac's and alternatives such as LLDB exist.
Could someone suggest the alternative (if that is possible) e.g. LLDB for GDB; Explaining why this is appropriate?
Supplementary literature would be appreciated (I find online tutorials for GDB are widely available...but cant say the same for LLDB (from a dumbed down perspective of a beginner such as myself)).
valgrind is a great tool, but it's good to have a variety of such tools on hand, so...
Xcode ships with a tool called ASAN (address sanitizer) which does many of the same memory analysis techniques that valgrind does. That's easiest to use if you build your project with Xcode. Then you just edit the Run scheme (Product->Scheme->Edit Scheme, then choose Run) choose the Diagnostics tab and turn on ASAN. If you run in Xcode, when an error is triggered, it will show you the relevant stacks (the original alloc plus the two free's for a double free, etc...)
If you are building outside Xcode, here's a link to the ASAN documentation:
http://clang.llvm.org/docs/AddressSanitizer.html

How do I setup a C environment on Windows (and maybe Linux compatible, too)

Short version of question: How do I get started with C programming? Note that I am not asking for a tutorial on learning C language (I can learn that easy enough). I need to setup the environment (I hope I'm asking this question clearly). Here's what I mean:
For my math thesis, I need to write a program in C on Gentoo Linux, using a library called CVODE/SUNDIALS. There is nobody (it seems) in my department who can help me set this up - my professor has left the computer work 100% to me because I have some programming background and he's a math geek. But my experience is with scripting languages (think VBA) and not full, powerful programming languages where you have to link the compiler and libraries, etc. like C.
There is no development environment on the Linux cluster - or at least not that's friendly, and has a debugger - that I've found. So, what I need to figure out how to setup a C programming environment with CVODE library on my PC (Win 7 x64, at little to no cost.
I have found plenty of tutorials on programming in C. I looked up Eclipse, which I have a little bit of experience with, as a development environment, but it's instructions say you need to install a compiler, too.
What I would like is someone to tell me, in simple language that I can understand (which might be the most difficult part of this question) the big picture of what I need and what to do (and maybe even links to where I can find what I need) to set up a C environment with CVODE. If the information is Windows/Gentoo Linux cross platform, even better.
Thank you.
P.S. I did search the site and saw lots of "How do I setup" quesitons, but no C one. Because I know someone will yell at me for that. Also, I don't want to have a convo about whether to use C#, C++, Java, etc. That just complicates the issue - and I need to get this done.
Edit: I have learned a little more since this question and now realize that I left out a key part of the question. The CVODE library and Linux cluster at school use MPI - parallel programming - which is not available on your average, run-of-the-mill PC. So all development must be done directly on the cluster.
Linux: Simple way is to install gcc or g++.
You can write your code in your plain text editor (nano, vim, gedit, kwrite, etc)
Save your file in .c or .cpp extention and type in terminal
gcc filename.c
or
g++ filename.cpp
You said that you want to write c code on Gentoo Linux, as i understand you're not familiar with Linux? The best choice in this case is to:
Install virtualbox in your windows machine (https://www.virtualbox.org/), it's a free software that let you emulate in your desktop another systems like Linux...
Install Gentoo linux on virtualbox, there are a lot of tutorials on the net, for example this video: http://www.youtube.com/watch?v=DUf_1wAPeyA
When you install Gentoo Linux on virtualbox you have all you need to develop C (gcc compiler, gdb debugger...)
Now you can download your library, and decompress it
In general all (Good) Linux libraries come with a 'README' file that contain all instructions for installing the library.
I think you need to do this:
./configure --prefix=/DIRECTORY_YOU_WANT_TO_INSTALL_THE_LIBRARY
make
make install
You can now play with C and you new library, like this:
suppose you create a new file test_lib_ CVODE.c you can compile it like this:
gcc -Wall test_lib_ CVODE.c -o test_lib_ CVODE -lcvode
I assume that the installed library is named libcvode.so
If you have any questions, you can always get help here.
Regards.
I think you should use Code::Block in Linux, it is very similar to Window's Code::Block and it is very easy to debug and other things.
These were all useful answers. I tried pursuing each of them at least a little bit. However, the only reasonable solution seems to be to use emacs on a terminal window. This is because I'm using MPI - yes, I know I didn't mention that in the OP - which can only be done on a cluster.
I am new to this environment and was not aware of the MPI or the affect it would have on my attempt to develop.
I believe I can do better than this if I can figure out X/Windows using Cygwin. But I am a long way from that.
Thanks all for your effort and sorry I can't really award a best answer (I guess).

can anyone suggest a good program for debugging a C program?

I need to debug a C program that includes posix threads, socket programming (udp client, server). I use ubuntu 12.04 and as IDE/SDK, Qt Creator 2.4.1 and Netbeans IDE 7.1.2. I know they use gdb for debugging.
When I start to debug my program, the program stops running after 5 min or so and neither Qt Creator or Netbeans output any error or warning, although I use debugging feature and my program suppose to listen for a UDP port.
I use printf for all line of my code, and I can see that my program works as it suppose to and listen the UDP port and waits. I can not figure the problem out why it stops without any reason and since IDEs that I am using do not show any debug error, warning, I can not think any reason.
I wonder if anyone can suggest me a debug program that monitors all/some variables and threads during the run time. Thank you.
An old, but reliable tool is ddd, which is basically the gdb GUI wrapper. Although, I usually do debugging directly with Emacs, ddd is the tool that you'll be able to run on almost all *nix platforms.
gdb isn't too comfortable but always available.
To do runtime analysis of different types, especially checking memory access, Valgrind (see here for docs) might be the tool of choice.
Update: I'm referring to *IX systems. For Windows gbd also works in the cygwin enviroment. Nativly there is VC Express, which is free and includes IDE and debugger.
I'm not quite sure what debugger is used for your kind of application. The only debugger I know about on Linux is gdb. It along with printf statements is all I need.
gdb is simple, though not "too comfortable" as #alk said, but seems to be ubiquitous.
There is also Eclipse, and that's quite a nice development and debugging platform, too.

OpenMP not in NetBeans IDE 7.0

The title is fairly self explanatory... isn't anywhere to be found in the version I have (the latest), and all searches on the web have turned up little to nothing in the way of real solutions. There is only a pdf file which explains that omp flags must be marked in the C compiler I use, but I can't even get that far because I can't find the library anywhere!
Is there just a way to pull the library from somewhere and load it in? Or do I need an entirely new gcc file for my IDE to pull from, and if so, where do I get it?
EDIT: Still no luck searching. I'm also willing to work with a different IDE similar to NetBeans... preferably one that uses the Cygwin compiler set as well. If anyone has any suggestions on what I could use that would support OMP, I would be more than willing to entertain them.
Oracle Solaris Studio on Solaris (SPARC and x86) and Linux (x86) provides an IDE with C/C++/Fortran compilers with OpenMP 3.0 enabled, and a debugger and performance analyzer that understand OpenMP.
http://www.oracle.com/technetwork/server-storage/solarisstudio/index.html
And, it's a free download and free to use.

mac osx development environment

i have a unix c programming assignment in which i do some advanced C network programming.this is my first step into advance programming. so i was wondering what is the best combination of tools for this on a mac. using an IDE like Eclipse is how i'd normally do it but i have to make my own makefiles and stuff. so i would like to learn how it can be done effectively using may be emacs or vim + other tools. it will be quite a big project so i am worried about project management and debugging issues mostly as well as the productivity factor. in essence i want to learn how programmers do it in the professional environment without the bloated IDE part. i am using Snow Leopard.i would also delve into C++ and python in the future so may be something that will be useful for those as well.
I know you're asking for how to do it with makefiles/VI/etc. but on the Mac, Xcode is really the way to go, especially for large projects. It's a very effective wrapper that will call gcc and gdb and the linker for you. Especially when moving to a new platform, not having to worry with many of the pesky details will be a big leap in productivity. It's IDE debugger is quite awesome.
Of course you can also use makefiles etc. Many projects (just to take OpenSSL as an example) come with makefiles and you can compile them on the Mac from the commandline just like under the *ix operating systems, i.e. calling ./configure and then make. But setting up stuff like that (e.g. compiler options for universal binaries and such) is tedious while the IDE it's just a few options. Also, if you google for specific questions, you will find far more answers on how to do it with Xcode.
If you want to get started with Xcode, it's either on your Mac operating system CD (it just does not pre-install automatically) or you can download it from Apple. When you run it, just open a Mac OS X Project of type "Application - Commandline Tool" and you'll have a project with a main.c set up in a minute. You can then just run it or run it in the debugger like that and adding more source files to it is rather easy.
Xcode can be quite a beast for setting up an already large project (we ported a large project with DLLs and depending exes (overall 250000 lines of code) to the Mac and just getting that all set up wasn't what you call a piece of cake) but if you start from scratch you'll easily grow into it.
Bottom line is that Xcode certainly is equipped to deal with large projects and I can not imagine a more productive way of doing it (I have used hand written makefiles and such in the past so I know both worlds).
Xcode is your friend. It's free and is a very nice IDE. When you launch XCode, just start a new Console application (it'll be ANSI C).
Enjoy.
If learning the rudiments of unix editors, shell programming, make, etc., are part of the assignment, then you just need to dive in and learn what you need to learn. Some good books will help. Obviously you need K&R. I always liked the O'Reilly books for Unix stuff, usually because they are the thinnest. I hate thick computer books because they never get read. You should also learn how to use the man pages.
Vim vs. Emacs is a religious choice. If you ask any Unix guy what is the best, he will invariably tell you the one he learned first, because chances are he never learned the other. In my case, I've been using Vim so long that my escape key is worn out and the commands are hard-wired into my brain. Obviously, I think it's way better than emacs (which I never learned!) If you are lucky enough to have a Mac as a work station, install mac vim. It's great.
Make is complicated enough so that you will never really master it. Just learn enough to compile and link your program. You can always learn more if you need it.
Version control is an interesting question... I use RCS for small stuff. Like vi, it is on every Unix machine. For really big projects, I use subversion, but like editors, most people use whatever they learned first. Git people will say its the only one to use, etc.
Command line debuggers are a pain, which is a main selling point for Xcode. I've used gdb, but I don't remember it as a pleasant experience. Its been so long since I used it, I can't even remember how to start it up. There must be better debuggers by now. Try google.
Bottom line, all the things you mentioned are big topics. You need to take realistic bites of each and not get tangled in the weeds. It can take years to master them all.
Finally, I'd stay as far away from C++ as possible! Objective C is much better. Personal prejudice!

Resources