On debugging...

Zorak

Distinguished
Mar 20, 2006
505
0
18,990
I guess this is really more of a programming question than a Linux question, but since Linux is pretty much the programmer's OS (for all its great tools) here goes...

Back in the day when I was programming in c/c++ in school, we used a windows IDE called CodeWarrior. Hopefully some of you are familiar with it? The thing that I really loved about that IDE is that it came with an awesome graphical debugger that allowed you to set breakpoints, run one statement at a time, run blocks of code, view the contents of all the program variables, etc. etc. . I am wondering if there is something analogous to this under a Linux environment (CodeWarrior is antiquated and freaking expensive!)? I am aware of several different debuggers in linux, but I am not sure which have GUI and which ones are easier to use or more powerful or what.

Also, if someone could point me towards some good resources on how to debug w/o all the graphical frills, I would like that. As it stands, I don't know the first thing about gdb, but I would kinda like to learn to write all my programs in vi, compile them on the command line w/ gcc and then debug w/ gdb. I guess by the time I could do that, I could consider myself a level two hardcore Linux badass! :wink:

Thanks for any help that you may or may not give me :D

-Zorak

P.S. In order that my earlier comment not sound too cocky, I am certain there are like 80 levels to being a hardcore Linux badass, and I only reached level one after learning how to install programs by compiling from source rather than via package managers. 8)
 

linux_0

Splendid
:-D

I guess eclipse is one IDE and kdevelop another

and then there's about a million more....

http://freshmeat.net/browse/65/


Check to see which ones are apt-getable or yumable on your distro and give them a try!
 

bmouring

Distinguished
May 6, 2006
1,215
0
19,360
Those are fine suggestions from linux_0. I only chime in to add my thoughts/experiences on the subject.

I personally use Eclipse with the CDT (or C/C++ Developer Toolkit). This allows the autogeneration of Makefiles (or the use of manually-created/pre-existing ones) for easy distribution as a source package as well as hooking into gdb in any guise it may have (for example, avr-gdb/avarice for Atmel microcontroller debugging over JTAG or arm-gdm for debugging over a serial or ethernet connection, can you tell I like embedded work?), as well as allowing you to choose different compilers (again, allowing for easy cross-platform dev work).

In regards to debugging, you can easily set and clear breakpoints simply by double-clicking the area next to a line that indicates whether there's a breakpoint there or not. A blue dot appears, indicating that you've set a breakpoint.

You can then run the program under the chosen debugger and it gives you access to the contents of variables either by a window that has the current function's variable list or simply by overing the cursor over the variable in question. You can modify the value of a variable in-debug to correct for an error or test for other conditions.

I haven't used KDevelop much, but I have heard great things about this IDE as well, so try a few out and see what works for you, as linux_0 suggested.
 

Zorak

Distinguished
Mar 20, 2006
505
0
18,990
thank you all for your help and suggestions :D. Someday I hope to learn to be a real guru and only use printf() to debug :wink:

-Zorak
 

bmouring

Distinguished
May 6, 2006
1,215
0
19,360
...
Someday I hope to learn to be a real guru and only use printf() to debug
...

Ha, it's funny, that's how I started programming and debugging, I then learned how to use debugging tools, and in order to do kernel dev I use the kernel equivalent of printf (namely printk). Basically, find a tool that works for the job and use it.
 

Zorak

Distinguished
Mar 20, 2006
505
0
18,990
I know that in java the equivalent of printf() -- that is before they actually added printf() in 1.5 -- is System.out.println() and for debugging purposes you have Debug.print(). Although I have never used it before, I know that when you type the latter statement, it wont print that statement unless you explicitly turn on "debug mode". Is there somethign analogous to this in c/c++?

-Zorak
 

Zorak

Distinguished
Mar 20, 2006
505
0
18,990
I am not certain i understand what you mean by that last comment. How is it that learning VisualBasic.net makes you poor?

Also, I have never actually used visualbasic in any of its iterations. The last time i did programming in basic was several years ago on our old 486 in windows 3.1 on QBASIC. :wink:

-Zorak

P.S. I am very excited because I just figured out how to get VI to do syntax highlighting for source code just like gedit has been doing. With this, I am one step closer to leaving gedit for vi.
 

linux_0

Splendid
You can with mono however native languages tend to work a bit better and are more appropriate for most tasks.

*sh, *csh, perl, php, python, tk/tcl can handle most scripting needs

If you need to bring in the heavy guns native C and C++ are also available.

You can also use java if you want / need to along with Pascal, Fortran, and a bunch of languages I'm forgetting.

:D

I'm a poor man :(

I'm learning VB .NET.... Find it suprisingly good to use. Shame I cannot use it for linux :cry:
 

bmouring

Distinguished
May 6, 2006
1,215
0
19,360
The version of such a system in C/C++ is as follows

[code:1:a624c97b17]
...
#ifdef debug
printf("Some debug message\n");
#endif
...
[/code:1:a624c97b17]

Compile with -Ddebug, and all of the debug statements will show up; don't and they disappear. Not as fancy as java, but works quite well.
 

knightrous

Distinguished
Jan 27, 2006
271
0
18,780
I've tryed learning a bit of python, but it just seems confusing to me. the commands just don't hold any meaning to me. I found VB .NET extremely simple to learn, commands and code are easy to remember and it's easy to debug.

I just downloaded the Visual Studio 2005 Express of the M$ site and have had a lot of fun with it. Including using some script and some thinking to dial a number on my mobile phone via AT Commands sent through serial bluetooth to my phone :)

Each time I look at Python or C/C++, I just get lost.... Maybe I'm just not ready for it....

@Zorak: I said I was poor because I use VB .NET and a lot of people seem to demonize it because it's M$. I would like to use python, since it runs native in linux, but I just get confused and struggle. VB .NET I had my first program running in a day, which was a barcode scanning program, that recorded the barcode, quantity and then wrote it to a CSV file.... I had trouble getting python to work as a calculator using 'case' functions. :cry:

Maybe someday my brain will work with Python...
 

linux_0

Splendid
I would recommend perl / php and shell for you then.

They are much friendlier than python :-D


[code:1:33e66ce522]

use strict;
use Win32::NetAdmin;
use Net::SMTP;

my $dc = "\\\\x";
my $from = 'x@x.com';
my @recip_list = ('x@x.com', 'y@y.com');
my $smtp_host = "x.x.x.x";
my @users = ();
my $VERSION = "1.2";

my $smtp = Net::SMTP->new($smtp_host, Timeout => 30) || die "Can't connect to $smtp_host.\n";

$smtp->mail($from) || die "cannot mail from.\n\n";

$smtp->recipient(@recip_list);
$smtp->data();

$smtp->datasend("To:\tPerl Recipients\n");
$smtp->datasend("Subject:\tPerl output of Users in the domain\n\n");

Win32::NetAdmin::GetUsers($dc, undef, \@users) || die "GetUsers failed.\n";

foreach (@users) {
$smtp->datasend( "$_\n\n");
}

$smtp->dataend;
$smtp->quit;

=head1 NAME

adminmail - This script grabs a list of all the users in a domain and emails the results to an email address.

=head1 DESCRIPTION

This program is useful for quickly getting a list of all the users on your domain. I wrote this program when our Notes
administrator "Maddog" needed a list of users on the domain.

=head1 README

This script grabs a list of all the users in a domain and emails the results to an email address.

=head1 PREREQUISITES

This script has a few requirements. You will need the Win32::NetAdmin and Net::SMTP modules.
You will also need to change the values for dc, from, recip_list, and smtp_host.

=head1 COREQUISITES

None

=pod OSNAMES

MSWin32

=pod SCRIPT CATEGORIES

Win32/Utilities

=cut

[/code:1:33e66ce522]



[code:1:33e66ce522]
<?php
$timeout = 3;
$old = ini_set('default_socket_timeout', $timeout);
$file = fopen('http://example.com', 'r');
ini_set('default_socket_timeout', $old);
stream_set_timeout($file, $timeout);
stream_set_blocking($file, 0);
//the rest is standard
?>
[/code:1:33e66ce522]




[code:1:33e66ce522]
#!/bin/bash

echo "hello world";

ls -al |sort -k 5 -r -n
[/code:1:33e66ce522]

:-D
 

Zorak

Distinguished
Mar 20, 2006
505
0
18,990
oh ok. Well, i have never used visual anything by M$. Not so much out of principle but because when I was learning programming, we always just used different IDEs than Visual studio. As for c/c++, it really isn't that hard to pick up. The hard part might be to make yourself think in terms of Object Oriented Programming, but that will be OK after you have some practice. Also, if you know java, it makes c/c++ much easier to pick up.


-Zorak
 

Zorak

Distinguished
Mar 20, 2006
505
0
18,990
Linux_0, allow me to submit the following:

Wow, that perl looks horribly complicated and unlike my beloved C/C++/Java :p

-Zorak
 

knightrous

Distinguished
Jan 27, 2006
271
0
18,780
[code:1:bf623e27ff]
Private Sub TB_Quantity_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_Quantity.GotFocus
Dim PMFile As String = (Application.StartupPath + "\data\pmf.csv") ' Product Master File for barcode comparison
Dim objReader As New System.IO.StreamReader(PMFile) ' Where to read PMF from
Dim PMArray(4) As String ' Array for line splitting
Dim DataLine As String ' Line of Product Master File
Dim ICheck As Boolean ' Input barcode check
Dim MCheck As Boolean ' Match barcode check

' Check Input for Barcode
If TB_Barcode.Text.Trim = "" Then
ICheck = False
TB_Barcode.Focus()
MsgBox("No Barcode Entered!", MsgBoxStyle.Exclamation, "Input Error!")
Else
ICheck = True
PB_FileRead.Visible = True
' Split Read Line and put in Array
Do
DataLine = objReader.ReadLine()
If DataLine = Nothing Then
'Dirty Trick :p
Else
PB_FileRead.Value = PB_FileRead.Value + 1
PMArray = DataLine.Split(",")
PMArray(0) = Replace(PMArray(0), """", "")
PMArray(1) = Replace(PMArray(1), """", "")
PMArray(2) = Replace(PMArray(2), """", "")
PMArray(3) = Replace(PMArray(3), """", "")
PMArray(4) = Replace(PMArray(4), """", "")
End If
Loop Until PMArray(3) = TB_Barcode.Text Or PMArray(4) = TB_Barcode.Text Or DataLine = Nothing
objReader.Close()
End If

If PMArray(3) = TB_Barcode.Text Or PMArray(4) = TB_Barcode.Text Then
If ICheck = True Then
MCheck = True
Else
' Dirty code test...
End If
End If

If MCheck = True Then

TB_Pcode.Text = PMArray(0)
TB_VBarcode.Text = TB_Barcode.Text
TB_PDescription.Text = PMArray(2)
' Make the following visible
TB_VBarcode.Visible = True
' TB_Pcode.Visible = True
TB_PDescription.Visible = True
LBL_Verify.Visible = True
PB_FileRead.Value = 0
BTN_Next.Visible = True
If CB_Speed.Checked Then ' Speed Scan Check
BTN_Next.PerformClick()
End If
Else
' Error message
MCheck = False
TB_Barcode.Clear()
TB_Barcode.Focus()
PB_FileRead.Value = 0
PB_FileRead.Visible = False
MsgBox("No Match Found", MsgBoxStyle.Exclamation, "Error!")
End If
End Sub
[/code:1:bf623e27ff]

One small section of my program I wrote for work :) I find this relatively easy to work with... What I can't do with good programming, I'll bodge with superior bodging skills and work arounds :lol:
 

linux_0

Splendid
Some clever code from down under along with other examples :-D


[code:1:b62c5480a2]
#!/usr/bin/perl

use Image::Magick;

use warnings;
use strict;

my $dir = '/Users/peter/images';
my $pdffile = '/Users/peter/newsletter.pdf';

chdir( $dir ) or die "Cant chdir to $dir $!";
while( <*.gif> ) {
push @files, $_;
}

my $pdf = Image::Magick->new();

foreach $file ( sort @files ) {
my $img = Image::Magick->new();
$rc = $img->Read( $file );
warn $rc if $rc;
push @$pdf, $img;
}

$pdf->[0]->Coalesce();
$pdf->Write( $pdffile );

[/code:1:b62c5480a2]