Debuging C code....

mike burg mb at pertec.UUCP
Tue Jan 8 17:32:43 AEST 1985


	Seeing the entries on unix-wizards and net.sources about debuging
C code, I thought I might bring up some more talk about this.

	A couple of nights ago I was debuging code (ugh) and was wonder if
there was any other debuging package other than adb and the recent debuging
package posted to net.sources that told the programer which line the program
bombed on. 

	Now, usualy you run adb on your program and your program's core to
find out what subroutine it blew up in and at what instruction. Well that's
fine if you know how the system's compiler procduces instruction for each
line of code. Or, if you don't know assembly and only know how to type
$C in adb for a traceback on the stack (or even you don't know that),
you usualy end up bombing your program/subroutine with printf()'s. Well, that's
okay, if your subroutine doesn't go into long loops were as you have to sit
there for hours until you find out what last printf() it ran before finnaly
dying. Has there been any atempts to modified the C compiler to produce
line tracking code? For example: 

main() {
	char *ptr;
	ptr = (char *) malloc(5);
	ptr[5] = 0;
}

	Will probably cause the program to abort due to the overindexing of
ptr. Now, if you told the compiler to procduce code that keep track of which
line number, how about some like this (on a PDP-11):
	.text
	.globl ~main
	.globl csv,cret
~main:	jsr	r5,csv
	sub	$4,r5	(or is it sp?)
	mov	$2,linenum	/ ptr = (char *) malloc(5)
	mov	$5,-(sp)
	jsr	pc,~malloc
	tst	(sp)+
	mov	r0,-4(sp)	/ ptr = return result of malloc()
	mov	$3,linenum	/ ptr[5] = 0
	mov	-4(sp),r0
	add	$5,r0
	clr	(r0)
	rts	pc	/ return from main

	Now in the csv and cret the var linenum could be push onto the stack
for subroutine call return. Several problems arise with this method, 1) It
uses up three words of memory for every line of the program and another word
if it was to be pushed on the stack  and, 2) This would require modifing the
the C compile, csv/cret routines and adb to understand the extra word on the
stack. But one of the advantages of this is that you could just:

% cc -j prog.c		(using -j to tell the compiler to procduce line tracking			code)
% a.out
Segmentation Voliation -- core dumped
% adb a.out core
$r
pc	010000
sp	017760
r5	017755
...
..
r0	017754
linenum	3
~main+xxx:	clr	(r0)

and new it crashed around line 3. 


	Has anyone try this or any thing simular to this? Any other ideas??
(has this been talk about on here before??)

		Mike Burg
		decvax!trwrb!pertec!mb

PS, Forgive me if I did anything wrong..I'm sorta new to the net.   



More information about the Comp.unix.wizards mailing list