Bug fix for debugging lines inside include files

General FreeBASIC programming questions.
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Bug fix for debugging lines inside include files

Post by SARG »

Hi all,

When debugging a source code with include files only lines inside procedures are correctly associated with their file....
I searched in the compiler to find a way to change this behaviour. I did it with very few modifications (#20 lines added or changed).
Nota 1 If every include file name is already stored somewhere it would possible to reduce a bit the memory consumption.
Nota 2 the fix works only for the gas version although the information is now also available in ir-hlc and ir-hllvm but not really used.


As I'm not comfortable with GitHub, I hope someone, St_W or coderjeff or dkl, could integrate these mods, it would be nice.

Code: Select all

==============================================================================================================================
== AST.BI ====================================================================================================================
==============================================================================================================================
Type AST_LINE_DATA ''creating a new type containing line number and included file name SARG
   linenum  As Long
   filename As ZString Ptr 
End Type

type AST_NODE_DBG
	ex				as AST_LINE_DATA ''replacing integer by the new type SARG
	op				as Integer
end Type									   		
												   		
.........
''adding filename SARG
declare function astNewDBG _
	( _
		byval op as integer, _
		byval ex as integer = 0, _
		ByVal filename As ZString Ptr =0 _
	) as ASTNODE Ptr

........
==============================================================================================================================
== AST-NODE-MISC.BAS =========================================================================================================
==============================================================================================================================
function astNewDBG _
	( _
		byval op as integer, _
		byval ex as Integer, _
		ByVal incfile As ZString Ptr _   
	) as ASTNODE ptr

	dim as ASTNODE ptr n = any

	if( env.clopt.debuginfo = FALSE ) then
		return NULL
	end if

	n = astNewNode( AST_NODECLASS_DBG, FB_DATATYPE_INVALID )
	
	n->dbg.op = op
	
	n->dbg.ex.linenum = ex ''filling linenum and filename SARG
	n->dbg.ex.filename = ZstrAllocate( len( *incfile ) )''creating space to store filename SARG
	*n->dbg.ex.filename = *incfile ''filling filename SARG

	function = n
end function

function astLoadDBG( byval n as ASTNODE ptr ) as IRVREG ptr
	if( ast.doemit ) Then
		irEmitDBG( n->dbg.op, astGetProc( )->sym,Cast(Integer,@n->dbg.ex)) ''casting as integer SARG
	end if

	function = NULL
end Function

=============================================================================================================================
== PARSE-TOP-LEVEL.BAS =======================================================================================================
==============================================================================================================================
Sub cProgram()
	dim as integer startlevel = pp.level

	'' For each line...
	do
		'' line begin
		astAdd( astNewDBG( AST_OP_DBG_LINEINI, lexLineNum( ),env.inf.incfile ) ) ''adding env.inf.incfile SARG
		'' Label?
		cLabel( )
		..........
		
		
		
==============================================================================================================================
== EDBG_STAB.BAS =============================================================================================================
==============================================================================================================================
Sub edbgLineBegin _
	( _
		byval proc as FBSYMBOL ptr, _
		byval lnum As integer, _
		byval pos_ as integer _
	)
   Dim As AST_LINE_DATA Ptr ldataptr=Cast(AST_LINE_DATA Ptr, lnum) ''casting SARG
	if( env.clopt.debuginfo = FALSE ) then
    	exit sub
	end If

	if( *ldataptr->filename <> *ctx.incfile) Then edbgInclude(ldataptr->filename) ''check new line ? SARG
   zstrfree(ctx.incfile) ''no more needed so release memory SARG

    if( ctx.lnum > 0 ) then
    	ctx.pos = pos_ - ctx.pos
    	if( ctx.pos > 0 ) then
    		edbgEmitLine( proc, ctx.lnum, ctx.label )
    		ctx.isnewline = TRUE
    	end if
    end if

    ctx.pos = pos_
    ctx.lnum = ldataptr->linenum
    if( ctx.isnewline ) then
    	ctx.label = symbAddLabel( NULL )
    	hLABEL( symbGetMangledName( ctx.label ) )
    	ctx.isnewline = FALSE
    end if

end Sub


==============================================================================================================================
== ir-hlc.bas ===================================================================================================================
==============================================================================================================================
#include once "ast.bi"  ''to get AST_LINE_DATA define SARG 
.........
private sub _emitDBG _
	( _
		byval op as integer, _
		byval proc as FBSYMBOL ptr, _
		byval ex as integer _
	)
        Dim As AST_LINE_DATA Ptr ldataptr=Cast(AST_LINE_DATA Ptr, ex) ''casting SARG
	if( op = AST_OP_DBG_LINEINI ) Then
		ctx.linenum = ldataptr->linenum ''retrieve line number SARG
		zstrfree(ldataptr->filename) ''no need so release memory SARG
	end if

end sub	

==============================================================================================================================
== ir-hllvm.bas ==============================================================================================================
==============================================================================================================================
#include once "ast.bi" ''to get AST_LINE_DATA define SARG 
..........
private sub _emitDBG _
	( _
		byval op as integer, _
		byval proc as FBSYMBOL ptr, _
		byval ex as integer _
	)
        Dim As AST_LINE_DATA Ptr ldataptr=Cast(AST_LINE_DATA Ptr, ex) ''casting SARG
	if( op = AST_OP_DBG_LINEINI ) Then
  		ctx.linenum = ldataptr->linenum ''retrieve line number SARG
		zstrfree(ldataptr->filename) ''no need so release memory SARG
		hWriteLine( "#line " & ctx.linenum & " """ & hReplace( env.inf.name, "\", $"\\" ) & """" )
	end if

end sub	

------------------------------------
-------- For testing
------------------------------------

Code: Select all

==============================================================================================================================
== testone.bi ================================================================================================================
==============================================================================================================================	
print "begin one"
#Include Once "testtwo.bi"
Dim As Integer testone=10
sub testinone
	print "inside testinone"
end sub
==============================================================================================================================
== testtwo.bi ================================================================================================================
==============================================================================================================================	
print "begin two"
sub testintwo
	print "inside testintwo"
end sub

Dim As Integer testtwo=52
==============================================================================================================================
== testtwo.bi ================================================================================================================
==============================================================================================================================			
print "begin main"
#Include Once "testone.bi"
sub test
 print "inside test"
end sub
print "inside main"

-------------------------------------
--- resulting asm file
-------------------------------------
Not easy to follow as the lines coming from include files are not included in the asm code........Just a test to remove if needed.
--------------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

	.file "tests.bas"
	.stabs "D:\\compiler\\",100,0,0,.Lt_0002
	.stabs "tests.bas",100,0,0,.Lt_0002

.section .text
.Lt_0002:
	.stabs "integer:t1=-1",128,0,0,0
	.stabs "void:t7=-11",128,0,0,0
	.stabs "byte:t2=-6",128,0,0,0
	.stabs "ubyte:t3=-5",128,0,0,0
	.stabs "char:t4=-2",128,0,0,0
	.stabs "short:t5=-3",128,0,0,0
	.stabs "ushort:t6=-7",128,0,0,0
	.stabs "uinteger:t8=-8",128,0,0,0
	.stabs "longint:t9=-31",128,0,0,0
	.stabs "ulongint:t10=-32",128,0,0,0
	.stabs "single:t11=-12",128,0,0,0
	.stabs "double:t12=-13",128,0,0,0
	.stabs "string:t13=s12data:15,0,32;len:1,32,32;size:1,64,32;;",128,0,0,0
	.stabs "fixstr:t14=-2",128,0,0,0
	.stabs "pchar:t15=*4;",128,0,0,0
	.stabs "boolean:t16=@s8;-16",128,0,0,0

	.stabs "tests.bas",130,0,0,0
	.intel_syntax noprefix
	.stabs "D:\\COMPILER\\TESTTWO.BI",130,0,0,0
	.stabs "D:\\COMPILER\\TESTTWO.BI",132,0,0,.Lt_000B
.Lt_000B:
	.stabs "TESTINTWO:F7",36,0,2,_TESTINTWO@0
.balign 16

.globl _TESTINTWO@0
_TESTINTWO@0:
	push ebp
	mov ebp, esp
	.stabn 68,0,2,_TESTINTWO@0-_TESTINTWO@0
.Lt_0008:
.Lt_000C:
	push 1
	push 16
	push offset _Lt_000A
	call _fb_StrAllocTempDescZEx@8
	push eax
	push 0
	call _fb_PrintString@12
.stabn 68,0,3,.Lt_000C-_TESTINTWO@0
.Lt_000D:
.Lt_0009:
	mov esp, ebp
	pop ebp
	ret
	.stabn 68,0,4,.Lt_0009-_TESTINTWO@0
	.stabn 192,0,0,.Lt_0008-_TESTINTWO@0
	.stabn 224,0,0,.Lt_0009-_TESTINTWO@0
.Lt_000E:
	.stabs "",36,0,0,.Lt_000E-_TESTINTWO@0
	.stabs "",162,0,0,0
	.stabs "D:\\COMPILER\\TESTONE.BI",130,0,0,0
	.stabs "D:\\COMPILER\\TESTONE.BI",132,0,0,.Lt_0012
.Lt_0012:
	.stabs "TESTINONE:F7",36,0,5,_TESTINONE@0
.balign 16

.globl _TESTINONE@0
_TESTINONE@0:
	push ebp
	mov ebp, esp
	.stabn 68,0,5,_TESTINONE@0-_TESTINONE@0
.Lt_000F:
.Lt_0013:
	push 1
	push 16
	push offset _Lt_0011
	call _fb_StrAllocTempDescZEx@8
	push eax
	push 0
	call _fb_PrintString@12
.stabn 68,0,6,.Lt_0013-_TESTINONE@0
.Lt_0014:
.Lt_0010:
	mov esp, ebp
	pop ebp
	ret
	.stabn 68,0,7,.Lt_0010-_TESTINONE@0
	.stabn 192,0,0,.Lt_000F-_TESTINONE@0
	.stabn 224,0,0,.Lt_0010-_TESTINONE@0
.Lt_0015:
	.stabs "",36,0,0,.Lt_0015-_TESTINONE@0
	.stabs "",162,0,0,0
	.stabs "TESTS.BAS",130,0,0,0
	.stabs "TESTS.BAS",132,0,0,.Lt_0019
.Lt_0019:
	.stabs "TEST:F7",36,0,4,_TEST@0
.balign 16

.globl _TEST@0
_TEST@0:
	push ebp
	mov ebp, esp
	.stabn 68,0,4,_TEST@0-_TEST@0
.Lt_0016:
.Lt_001A:
## print "inside test"
	push 1
	push 11
	push offset _Lt_0018
	call _fb_StrAllocTempDescZEx@8
	push eax
	push 0
	call _fb_PrintString@12
.stabn 68,0,5,.Lt_001A-_TEST@0
.Lt_001B:
.Lt_0017:
	mov esp, ebp
	pop ebp
	ret
	.stabn 68,0,6,.Lt_0017-_TEST@0
	.stabn 192,0,0,.Lt_0016-_TEST@0
	.stabn 224,0,0,.Lt_0017-_TEST@0
.Lt_001C:
	.stabs "",36,0,0,.Lt_001C-_TEST@0
	.stabs "main",42,0,1,_main
	.stabd 68,0,1
	.stabs "main:F1",36,0,1,_main
	.stabs "__FB_ARGC__:p1",160,0,0,8
	.stabs "__FB_ARGV__:p17=*18=*4",160,0,0,12
.balign 16

.globl _main
_main:
	push ebp
	mov ebp, esp
	and esp, 0xFFFFFFF0
	sub esp, 20
	.stabn 68,0,1,_main-_main
	mov dword ptr [ebp-4], 0
	call ___main
	push 0
	push dword ptr [ebp+12]
	push dword ptr [ebp+8]
	call _fb_Init@12
.Lt_0003:
.Lt_001E:
##
##print "begin main"
	push 1
	push 10
	push offset _Lt_0005
	call _fb_StrAllocTempDescZEx@8
	push eax
	push 0
	call _fb_PrintString@12
	.stabs "",162,0,0,0
	.stabs "D:\\COMPILER\\TESTONE.BI",130,0,0,0
	.stabs "D:\\COMPILER\\TESTONE.BI",132,0,0,.Lt_001F
.Lt_001F:
.stabn 68,0,2,.Lt_001E-_main
.Lt_0020:
	push 1
	push 9
	push offset _Lt_0006
	call _fb_StrAllocTempDescZEx@8
	push eax
	push 0
	call _fb_PrintString@12
	.stabs "",162,0,0,0
	.stabs "D:\\COMPILER\\TESTTWO.BI",130,0,0,0
	.stabs "D:\\COMPILER\\TESTTWO.BI",132,0,0,.Lt_0021
.Lt_0021:
.stabn 68,0,2,.Lt_0020-_main
.Lt_0022:
	push 1
	push 9
	push offset _Lt_0007
	call _fb_StrAllocTempDescZEx@8
	push eax
	push 0
	call _fb_PrintString@12
.stabn 68,0,1,.Lt_0022-_main
.Lt_0023:
	mov dword ptr [ebp-8], 52
.stabn 68,0,6,.Lt_0023-_main
	.stabs "",162,0,0,0
	.stabs "D:\\COMPILER\\TESTONE.BI",130,0,0,0
	.stabs "D:\\COMPILER\\TESTONE.BI",132,0,0,.Lt_0024
.Lt_0024:
.Lt_0025:
	mov dword ptr [ebp-12], 10
.stabn 68,0,4,.Lt_0025-_main
.Lt_0026:
###Include Once "testone.bi"
	.stabs "",162,0,0,0
	.stabs "TESTS.BAS",130,0,0,0
	.stabs "TESTS.BAS",132,0,0,.Lt_0027
.Lt_0027:
##
##end sub
##print "inside main"
	push 1
	push 11
	push offset _Lt_001D
	call _fb_StrAllocTempDescZEx@8
	push eax
	push 0
	call _fb_PrintString@12
.stabn 68,0,7,.Lt_0026-_main
.Lt_0004:
	push 0
	call _fb_End@4
	mov eax, dword ptr [ebp-4]
	mov esp, ebp
	pop ebp
	ret
	.stabn 68,0,7,.Lt_0004-_main
	.stabs "TESTTWO:1",128,0,0,-8
	.stabs "TESTONE:1",128,0,0,-12
	.stabn 192,0,0,.Lt_0003-_main
	.stabn 224,0,0,.Lt_0004-_main
.Lt_0028:
	.stabs "",36,0,0,.Lt_0028-_main
	.stabs "",162,0,0,0
	.stabs "tests.bas",132,0,0,.Lt_0029
.Lt_0029:

.section .data
.balign 4
_Lt_0005:	.ascii	"begin main\0"
.balign 4
_Lt_0006:	.ascii	"begin one\0"
.balign 4
_Lt_0007:	.ascii	"begin two\0"
.balign 4
_Lt_000A:	.ascii	"inside testintwo\0"
.balign 4
_Lt_0011:	.ascii	"inside testinone\0"
.balign 4
_Lt_0018:	.ascii	"inside test\0"
.balign 4
_Lt_001D:	.ascii	"inside main\0"

.section .text
	.stabs "",100,0,0,.Lt_002A
.Lt_002A:
			   		
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Bug fix for debugging lines inside include files

Post by St_W »

Here's a GIT patch (based on the current master) for anybody wanting to try this out (without applying all the changes manually):

Code: Select all

From da8cc87e7379c08ea319164fb94590b884f0c977 Mon Sep 17 00:00:00 2001
From: Laurent Gras <sarg@aliceadsl.fr>
Date: Wed, 11 Jul 2018 00:15:35 +0200
Subject: [PATCH] fix debugging data for lines inside include files

---
 src/compiler/ast-node-misc.bas   |  9 ++++++---
 src/compiler/ast.bi              | 10 ++++++++--
 src/compiler/edbg_stab.bas       |  7 ++++++-
 src/compiler/ir-hlc.bas          |  6 +++++-
 src/compiler/ir-llvm.bas         |  6 +++++-
 src/compiler/parser-toplevel.bas |  2 +-
 6 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/src/compiler/ast-node-misc.bas b/src/compiler/ast-node-misc.bas
index f08ba96d1..da12a3135 100644
--- a/src/compiler/ast-node-misc.bas
+++ b/src/compiler/ast-node-misc.bas
@@ -152,7 +152,8 @@ end function
 function astNewDBG _
 	( _
 		byval op as integer, _
-		byval ex as integer _
+		byval ex as integer, _
+		byval incfile as zstring ptr _
 	) as ASTNODE ptr
 
 	dim as ASTNODE ptr n = any
@@ -164,14 +165,16 @@ function astNewDBG _
 	n = astNewNode( AST_NODECLASS_DBG, FB_DATATYPE_INVALID )
 
 	n->dbg.op = op
-	n->dbg.ex = ex
+	n->dbg.ex.linenum = ex
+	n->dbg.ex.filename = ZstrAllocate( len( *incfile ) )
+	*n->dbg.ex.filename = *incfile
 
 	function = n
 end function
 
 function astLoadDBG( byval n as ASTNODE ptr ) as IRVREG ptr
 	if( ast.doemit ) then
-		irEmitDBG( n->dbg.op, astGetProc( )->sym, n->dbg.ex )
+		irEmitDBG( n->dbg.op, astGetProc( )->sym, cast( integer, @n->dbg.ex ))
 	end if
 
 	function = NULL
diff --git a/src/compiler/ast.bi b/src/compiler/ast.bi
index 0e4b1a569..f50e247b0 100644
--- a/src/compiler/ast.bi
+++ b/src/compiler/ast.bi
@@ -171,8 +171,13 @@ type AST_NODE_JMPTB
 	span				as ulongint
 end type
 
+Type AST_LINE_DATA
+	linenum			as long
+	filename		as zstring ptr
+End Type
+
 type AST_NODE_DBG
-	ex				as integer
+	ex				as AST_LINE_DATA
 	op				as integer
 end type
 
@@ -791,7 +796,8 @@ declare function astNewASM( byval asmtokhead as ASTASMTOK ptr ) as ASTNODE ptr
 declare function astNewDBG _
 	( _
 		byval op as integer, _
-		byval ex as integer = 0 _
+		byval ex as integer = 0, _
+		byval filename as zstring ptr = 0 _
 	) as ASTNODE ptr
 
 declare function astNewMEM _
diff --git a/src/compiler/edbg_stab.bas b/src/compiler/edbg_stab.bas
index a76c7494f..c03e30d8e 100644
--- a/src/compiler/edbg_stab.bas
+++ b/src/compiler/edbg_stab.bas
@@ -269,10 +269,15 @@ sub edbgLineBegin _
 		byval pos_ as integer _
 	)
 
+	dim as AST_LINE_DATA ptr ldataptr = cast( AST_LINE_DATA ptr, lnum )
+
 	if( env.clopt.debuginfo = FALSE ) then
     	exit sub
     end if
 
+	if( *ldataptr->filename <> *ctx.incfile) Then edbgInclude( ldataptr->filename )
+	zstrfree( ctx.incfile )
+
     if( ctx.lnum > 0 ) then
     	ctx.pos = pos_ - ctx.pos
     	if( ctx.pos > 0 ) then
@@ -282,7 +287,7 @@ sub edbgLineBegin _
     end if
 
     ctx.pos = pos_
-    ctx.lnum = lnum
+	ctx.lnum = ldataptr->linenum
     if( ctx.isnewline ) then
     	ctx.label = symbAddLabel( NULL )
     	hLABEL( symbGetMangledName( ctx.label ) )
diff --git a/src/compiler/ir-hlc.bas b/src/compiler/ir-hlc.bas
index f4600d413..0ae61046c 100644
--- a/src/compiler/ir-hlc.bas
+++ b/src/compiler/ir-hlc.bas
@@ -103,6 +103,7 @@
 #include once "rtl.bi"
 #include once "flist.bi"
 #include once "lex.bi"
+#include once "ast.bi"
 #include once "ir-private.bi"
 
 '' The stack of nested sections allows us to go back and emit text to
@@ -3161,8 +3162,11 @@ private sub _emitDBG _
 		byval ex as integer _
 	)
 
+	dim as AST_LINE_DATA ptr ldataptr = cast( AST_LINE_DATA ptr, ex )
+
 	if( op = AST_OP_DBG_LINEINI ) then
-		ctx.linenum = ex
+		ctx.linenum = ldataptr->linenum
+		zstrfree( ldataptr->filename )
 	end if
 
 end sub
diff --git a/src/compiler/ir-llvm.bas b/src/compiler/ir-llvm.bas
index 925866ca6..eb86a4686 100644
--- a/src/compiler/ir-llvm.bas
+++ b/src/compiler/ir-llvm.bas
@@ -90,6 +90,7 @@
 #include once "rtl.bi"
 #include once "flist.bi"
 #include once "lex.bi"
+#include once "ast.bi"
 #include once "ir-private.bi"
 
 enum
@@ -2083,9 +2084,12 @@ private sub _emitDBG _
 		byval ex as integer _
 	)
 
+	dim as AST_LINE_DATA ptr ldataptr = cast( AST_LINE_DATA ptr, ex )
+
 	if( op = AST_OP_DBG_LINEINI ) then
 		hWriteLine( "#line " & ex & " """ & hReplace( env.inf.name, "\", $"\\" ) & """" )
-		ctx.linenum = ex
+		ctx.linenum = ldataptr->linenum
+		zstrfree( ldataptr->filename )
 	end if
 
 end sub
diff --git a/src/compiler/parser-toplevel.bas b/src/compiler/parser-toplevel.bas
index 10a00d8e8..860dc443c 100644
--- a/src/compiler/parser-toplevel.bas
+++ b/src/compiler/parser-toplevel.bas
@@ -110,7 +110,7 @@ sub cProgram()
 	'' For each line...
 	do
 		'' line begin
-		astAdd( astNewDBG( AST_OP_DBG_LINEINI, lexLineNum( ) ) )
+		astAdd( astNewDBG( AST_OP_DBG_LINEINI, lexLineNum( ), env.inf.incfile ) )
 
 		'' Label?
 		cLabel( )
-- 
2.17.1
I haven't investigated your changes in more detail yet, but from looking at the code while applying the changes i think there might be some adaptations required before this can be merged. But I can't do that anyway so let's wait for a comment by dkl/coderJeff/countingpine :-)
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bug fix for debugging lines inside include files

Post by SARG »

@St_W
Thank you.
St_W wrote:I haven't investigated your changes in more detail yet, but from looking at the code while applying the changes i think there might be some adaptations required before this can be merged.
Just curious, what adaptations do you think about ?
Initially to have a more readable code I also changed 'lnum' by 'ex' but that's only cosmetic and it involves more files modified.
St_W wrote:so let's wait for a comment by dkl/coderJeff/countingpine :-)
Hope they do :-)
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Bug fix for debugging lines inside include files

Post by coderJeff »

SARG, cool. It's a good start. St_W, thanks for the patch; I would not have looked at it otherwise.

If there is no community member that wants to take this on, I suggest tracking it with a bug report or feature request, refering to this topic. I'm working on something else, for now.

Here are a few suggestions:

Extending AST_NODE_DBG:
- add the new fields to AST_NODE_DBG and use the fields directly instead of AST_LINE_DATA to avoid the uneeded and potentially confusing cast. AST_NODE_BLOCK is largest AST_NODE_* structure at the moment so won't be increasing memory usage here by adding fields to AST_NODE_DBG
- update the *emitDBG calls to take a filename instead of using AST data structure directly in IR modules. That's just a guideline; I think the intent is to separate IR method calls from AST data structure, however a few IR methods do use the AST data directly.
- if "ex" is only ever used for line number, then change the name, everywhere. Even if only cosmetic, it does make the code easier to follow.

Storing the file names:
- env.filenamehash should have all the filenames found, so I think should be possible to just store the zstring ptr.
- ctx.inf.infname should be a pointer in to env.filenamehash's data. Shouldn't have to keep a copy. Need to confirm, see sym->proc.ext->dbg.incfile

For testing:
- include files nested inside include files
- #include files inside a sub procedure
- #macro's from one include file, used in another included file
- __FILE__ and __LINE__ overrides
- gcc emitter, can use #line num "filename", I think
- llvm emitter, don't know
- we don't have a way to test this kind of change in the test-suite, so need to be somewhat thorough with own tests

Next steps:
- submitting a pull request to github would give the proposed changes the most attention. It's convenient to track the progress and code comments through a github pull request.
- I would guess about 6 to 10 hours to make changes, test on various platforms/emitters, and then submit pull request, assuming no complications. I tend to underestimate time required for programming work though...
- In my opinion, this is a not too complex, entry level kind of change, that someone new to developing fbc could take on.
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bug fix for debugging lines inside include files

Post by SARG »

@coderJeff
Many thanks for your comments, suggestions and advices.
My first code was more close from what you adviced but with a lot of modified lines I was afraid that nobody wants to do something.... So I have used the more simpliest way.
I'll try to follow what you suggest. Some points no problem and for the others I'll learn as the compiler is really a "treasure hunt" :-)
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Bug fix for debugging lines inside include files

Post by St_W »

btw, to create a similar diff to the one I created previously you just need to do these simple steps:
1. install git: https://git-scm.com/
2. clone the FB repository; run "git clone https://github.com/freebasic/fbc.git"
3. <do your changes in the cloned repo>
4. create the diff with "git diff". you can save it to a file with "git diff > myFile"

(alternatively, and IMHO even more convenient for the user, you can first commit your changes (git add -A; git commit -m "message") and then use "git format-patch")
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bug fix for debugging lines inside include files

Post by SARG »

Hi all,

I did it :-)
Some comments :
- Without fix for issue #851 it was not possible.
- About macro the line numbers/file names are the one where the macro is used
- the right name files are emitted for gcc and llvm backends.
- I did tests (see below the patch the 4 files used) but I don't know if there is a way to avoid manual checks.

And now what is the continuation (access to fbc github,...) ?

Code: Select all

From 7cd394df8ee65681221ba7a4b55a34dc182e77d6 Mon Sep 17 00:00:00 2001
From: Laurent <debug@aliceadsl.fr>
Date: Fri, 20 Jul 2018 17:56:10 +0200
Subject: [PATCH] Fix for debugging lines in include files but not in
 procedures

---
 src/compiler/ast-node-misc.bas    |  6 ++++--
 src/compiler/ast.bi               | 10 ++++++----
 src/compiler/edbg_stab.bas        | 11 +++++++----
 src/compiler/emit.bas             | 12 ++++++++----
 src/compiler/emit.bi              |  7 +++++--
 src/compiler/emit_x86.bas         |  5 +++--
 src/compiler/emitdbg.bi           |  3 ++-
 src/compiler/ir-hlc.bas           | 15 +++++++++------
 src/compiler/ir-llvm.bas          | 22 ++++++++++++++--------
 src/compiler/ir-tac.bas           | 21 +++++++++++++--------
 src/compiler/ir.bi                |  6 ++++--
 src/compiler/parser-inlineasm.bas |  2 +-
 src/compiler/parser-toplevel.bas  |  2 +-
 13 files changed, 77 insertions(+), 45 deletions(-)

diff --git a/src/compiler/ast-node-misc.bas b/src/compiler/ast-node-misc.bas
index f08ba96d1..77fa9a258 100644
--- a/src/compiler/ast-node-misc.bas
+++ b/src/compiler/ast-node-misc.bas
@@ -152,7 +152,8 @@ end function
 function astNewDBG _
 	( _
 		byval op as integer, _
-		byval ex as integer _
+		byval ex As Integer, _
+		byval filename As ZString Ptr _
 	) as ASTNODE ptr
 
 	dim as ASTNODE ptr n = any
@@ -165,13 +166,14 @@ function astNewDBG _
 
 	n->dbg.op = op
 	n->dbg.ex = ex
+	n->dbg.filename = filename
 
 	function = n
 end function
 
 function astLoadDBG( byval n as ASTNODE ptr ) as IRVREG ptr
 	if( ast.doemit ) then
-		irEmitDBG( n->dbg.op, astGetProc( )->sym, n->dbg.ex )
+		irEmitDBG( n->dbg.op, astGetProc( )->sym, n->dbg.ex, n->dbg.filename )
 	end if
 
 	function = NULL
diff --git a/src/compiler/ast.bi b/src/compiler/ast.bi
index 0e4b1a569..8c769ab65 100644
--- a/src/compiler/ast.bi
+++ b/src/compiler/ast.bi
@@ -172,8 +172,9 @@ type AST_NODE_JMPTB
 end type
 
 type AST_NODE_DBG
-	ex				as integer
-	op				as integer
+	ex          as integer
+	filename    as ZString Ptr
+	op          as integer
 end type
 
 type AST_NODE_MEM
@@ -790,8 +791,9 @@ declare function astNewASM( byval asmtokhead as ASTASMTOK ptr ) as ASTNODE ptr
 
 declare function astNewDBG _
 	( _
-		byval op as integer, _
-		byval ex as integer = 0 _
+		byval op       As integer, _
+		byval ex       as integer = 0, _
+		byval filename As ZString Ptr = 0 _
 	) as ASTNODE ptr
 
 declare function astNewMEM _
diff --git a/src/compiler/edbg_stab.bas b/src/compiler/edbg_stab.bas
index a76c7494f..8af253760 100644
--- a/src/compiler/edbg_stab.bas
+++ b/src/compiler/edbg_stab.bas
@@ -266,12 +266,13 @@ sub edbgLineBegin _
 	( _
 		byval proc as FBSYMBOL ptr, _
 		byval lnum as integer, _
-		byval pos_ as integer _
+		byval pos_ as Integer, _
+		ByVal filename As ZString Ptr _
 	)
 
 	if( env.clopt.debuginfo = FALSE ) then
     	exit sub
-    end if
+	end If
 
     if( ctx.lnum > 0 ) then
     	ctx.pos = pos_ - ctx.pos
@@ -281,6 +282,8 @@ sub edbgLineBegin _
     	end if
     end if
 
+    edbgInclude(filename)
+    
     ctx.pos = pos_
     ctx.lnum = lnum
     if( ctx.isnewline ) then
@@ -530,7 +533,7 @@ sub edbgEmitProcHeader _
 
 	''
 	ctx.isnewline = TRUE
-	ctx.lnum	  = 0
+	ctx.lnum	     = 0
 	ctx.pos	  	  = 0
 	ctx.label	  = NULL
 
@@ -645,7 +648,7 @@ sub edbgEmitProcFooter _
 
 	''
 	ctx.isnewline = TRUE
-	ctx.lnum	  = 0
+	ctx.lnum      = 0
 	ctx.pos	  	  = 0
 	ctx.label	  = NULL
 
diff --git a/src/compiler/emit.bas b/src/compiler/emit.bas
index 9369a0fc5..97e42cc2d 100644
--- a/src/compiler/emit.bas
+++ b/src/compiler/emit.bas
@@ -204,7 +204,8 @@ sub emitFlush( )
 		case EMIT_NODECLASS_DBG
 			cast( EMIT_DBGCB, emit.opFnTb[n->dbg.op] )( n->dbg.sym, _
 												   		n->dbg.lnum, _
-												   		n->dbg.pos )
+												   		n->dbg.pos, _
+												   		n->dbg.filename )
 
 		end select
 
@@ -456,7 +457,8 @@ private function hNewDBG _
 		byval op as integer, _
 		byval sym as FBSYMBOL ptr, _
 		byval lnum as integer = 0, _
-		byval pos_ as integer = 0 _
+		byval pos_ as integer = 0, _
+		ByVal filename As ZString Ptr =0  _
 	) as EMIT_NODE ptr static
 
 	dim as EMIT_NODE ptr n
@@ -466,6 +468,7 @@ private function hNewDBG _
 	n->dbg.op = op
 	n->dbg.sym = sym
 	n->dbg.lnum = lnum
+	n->dbg.filename = filename
 	n->dbg.pos = pos_
 
 	function = n
@@ -1632,10 +1635,11 @@ end function
 function emitDBGLineBegin _
 	( _
 		byval proc as FBSYMBOL ptr, _
-		byval lnum as integer _
+		byval lnum as Integer, _
+		ByVal filename As ZString Ptr _
 	) as EMIT_NODE ptr
 
-	function = hNewDBG( EMIT_OP_LINEINI, proc, lnum, emit.pos )
+	function = hNewDBG( EMIT_OP_LINEINI, proc, lnum, emit.pos, filename )
 
 end function
 
diff --git a/src/compiler/emit.bi b/src/compiler/emit.bi
index a0601273b..a166a5828 100644
--- a/src/compiler/emit.bi
+++ b/src/compiler/emit.bi
@@ -199,6 +199,7 @@ type EMIT_DBGNODE
 	op			as integer
 	sym			as FBSYMBOL ptr
 	lnum		as integer
+	filename As ZString Ptr
 	pos			as integer
 end type
 
@@ -261,7 +262,8 @@ type EMIT_MEMCB as sub( byval dvreg as IRVREG ptr, _
 
 type EMIT_DBGCB as sub( byval sym as FBSYMBOL ptr, _
 						byval lnum as integer, _
-						byval pos as integer )
+						byval pos as Integer, _
+						ByVal filename As ZString Ptr =0 )
 
 '' if changed, update the _vtbl symbols at emit_*.bas::*_ctor
 type EMIT_VTBL
@@ -790,7 +792,8 @@ declare function emitSTKCLEAR _
 declare function emitDBGLineBegin _
 	( _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval ex as Integer, _
+		ByVal filename As ZString Ptr _
 	) as EMIT_NODE ptr
 
 declare function emitDBGLineEnd _
diff --git a/src/compiler/emit_x86.bas b/src/compiler/emit_x86.bas
index 13541770e..2c2262f59 100644
--- a/src/compiler/emit_x86.bas
+++ b/src/compiler/emit_x86.bas
@@ -6049,10 +6049,11 @@ private sub _emitLINEINI _
 	( _
 		byval proc as FBSYMBOL ptr, _
 		byval lnum as integer, _
-		byval pos_ as integer _
+		byval pos_ as Integer, _
+		ByVal filename As ZString Ptr _
 	)
 
-	edbgLineBegin( proc, lnum, pos_ )
+	edbgLineBegin( proc, lnum, pos_, filename )
 
 end sub
 
diff --git a/src/compiler/emitdbg.bi b/src/compiler/emitdbg.bi
index 7542d3bcb..1006e0f92 100644
--- a/src/compiler/emitdbg.bi
+++ b/src/compiler/emitdbg.bi
@@ -9,7 +9,8 @@ declare sub edbgLineBegin _
 	( _
 		byval proc as FBSYMBOL ptr, _
 		byval lnum as integer, _
-		byval pos as integer _
+		byval pos as Integer, _
+		ByVal filename as ZString Ptr _
 	)
 
 declare sub edbgLineEnd _
diff --git a/src/compiler/ir-hlc.bas b/src/compiler/ir-hlc.bas
index f4600d413..6c8d211c8 100644
--- a/src/compiler/ir-hlc.bas
+++ b/src/compiler/ir-hlc.bas
@@ -192,7 +192,7 @@ type IRHLCCTX
 	section				as integer '' Current section to write to
 	sectiongosublevel		as integer
 
-	linenum				as integer
+	lnum				as integer
 	escapedinputfilename		as string
 	usedbuiltins			as uinteger  '' BUILTIN_*
 
@@ -223,7 +223,8 @@ declare sub _emitDBG _
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval lnum as Integer, _
+		ByVal filename As ZString Ptr = 0 _
 	)
 
 declare sub exprFreeNode( byval n as EXPRNODE ptr )
@@ -385,7 +386,7 @@ private sub hWriteLine( byref s as string, byval noline as integer = FALSE )
 	static as string ln
 
 	if( env.clopt.debuginfo and (noline = FALSE) ) then
-		ln = "#line " + str( ctx.linenum )
+		ln = "#line " + str( ctx.lnum )
 		ln += " """ + ctx.escapedinputfilename + """"
 		sectionWriteLine( ln )
 	end if
@@ -1249,7 +1250,7 @@ private function _emitBegin( ) as integer
 
 	ctx.section = -1
 	ctx.sectiongosublevel = 0
-	ctx.linenum = 0
+	ctx.lnum = 0
 	ctx.usedbuiltins = 0
 	ctx.globalvarpass = 0
 	hUpdateCurrentFileName( env.inf.name )
@@ -3158,11 +3159,13 @@ private sub _emitDBG _
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval lnum as Integer, _
+		ByVal filename As ZString Ptr _
 	)
 
 	if( op = AST_OP_DBG_LINEINI ) then
-		ctx.linenum = ex
+		ctx.lnum = lnum
+		If filename<>0 Then hUpdateCurrentFileName(filename)
 	end if
 
 end sub
diff --git a/src/compiler/ir-llvm.bas b/src/compiler/ir-llvm.bas
index 925866ca6..a125f8af8 100644
--- a/src/compiler/ir-llvm.bas
+++ b/src/compiler/ir-llvm.bas
@@ -155,7 +155,7 @@ const MAXVARINISCOPES = 128
 
 type IRLLVMCONTEXT
 	indent				as integer  '' current indentation used by hWriteLine()
-	linenum				as integer
+	lnum				as integer
 
 	varini				as string
 	variniscopelevel		as integer
@@ -185,7 +185,8 @@ declare sub _emitDBG _
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval lnum as Integer, _
+		ByVal filename As ZString Ptr = 0 _
 	)
 declare function hVregToStr( byval vreg as IRVREG ptr ) as string
 declare sub hEmitConvert( byval v1 as IRVREG ptr, byval v2 as IRVREG ptr )
@@ -885,7 +886,7 @@ private function _emitBegin( ) as integer
 	ctx.head_txt = ""
 	ctx.body_txt = ""
 	ctx.foot_txt = ""
-	ctx.linenum = 0
+	ctx.lnum = 0
 	ctx.section = SECTION_HEAD
 
 	for i as integer = 0 to BUILTIN__COUNT-1
@@ -2080,13 +2081,18 @@ private sub _emitDBG _
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval lnum as Integer, _
+		ByVal filename As ZString Ptr _
 	)
 
-	if( op = AST_OP_DBG_LINEINI ) then
-		hWriteLine( "#line " & ex & " """ & hReplace( env.inf.name, "\", $"\\" ) & """" )
-		ctx.linenum = ex
-	end if
+	if( op = AST_OP_DBG_LINEINI ) Then
+	   If filename<>0 Then 
+		   hWriteLine( "#line " & lnum & " """ & hReplace( filename, "\", $"\\" ) & """" )
+	   Else
+	      hWriteLine( "#line " & lnum & " """ & hReplace( env.inf.name, "\", $"\\" ) & """" )
+	   End If
+		ctx.lnum = lnum
+	end If
 
 end sub
 
diff --git a/src/compiler/ir-tac.bas b/src/compiler/ir-tac.bas
index e623d934c..36c55c717 100644
--- a/src/compiler/ir-tac.bas
+++ b/src/compiler/ir-tac.bas
@@ -106,7 +106,8 @@ declare sub hFlushDBG _
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval ex as Integer, _
+		ByVal filename As ZString Ptr _ 
 	)
 
 declare sub hFlushLIT( byval op as integer, byval text as zstring ptr )
@@ -294,7 +295,8 @@ private sub _emit _
 		byval v2 as IRVREG ptr, _
 		byval vr as IRVREG ptr, _
 		byval ex1 as FBSYMBOL ptr = NULL, _
-		byval ex2 as integer = 0 _
+		byval ex2 as integer = 0, _
+		byval ex3 as ZString Ptr = 0 _
 	) static
 
     dim as IRTAC ptr t
@@ -317,7 +319,8 @@ private sub _emit _
 
     t->ex1 = ex1
     t->ex2 = ex2
-
+    t->ex3 = ex3
+    
     ctx.taccnt += 1
 
 end sub
@@ -679,10 +682,11 @@ private sub _emitDBG _
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval ex       As Integer, _
+		ByVal filename As ZString Ptr _
 	)
 
-	_emit( op, NULL, NULL, NULL, proc, ex )
+	_emit( op, NULL, NULL, NULL, proc, ex, filename )
 
 end sub
 
@@ -1361,7 +1365,7 @@ private sub _flush static
 			hFlushMEM( op, v1, v2, t->ex2, t->ex1 )
 
 		case AST_NODECLASS_DBG
-			hFlushDBG( op, t->ex1, t->ex2 )
+			hFlushDBG( op, t->ex1, t->ex2, t->ex3 )
 
 		case AST_NODECLASS_LIT
 			hFlushLIT( op, cast( any ptr, t->ex1 ) )
@@ -2397,12 +2401,13 @@ private sub hFlushDBG _
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval ex as Integer, _
+		ByVal filename As ZString Ptr _
 	)
 
 	select case as const op
 	case AST_OP_DBG_LINEINI
-		emitDBGLineBegin( proc, ex )
+		emitDBGLineBegin( proc, ex, filename )
 
 	case AST_OP_DBG_LINEEND
 		emitDBGLineEnd( proc, ex )
diff --git a/src/compiler/ir.bi b/src/compiler/ir.bi
index 6feb6507b..757ad0c3c 100644
--- a/src/compiler/ir.bi
+++ b/src/compiler/ir.bi
@@ -67,6 +67,7 @@ type IRTAC
 
 	ex1			as FBSYMBOL ptr					'' extra field, used by call/jmp
 	ex2			as integer						'' /
+	ex3         As ZString Ptr 
 end type
 
 type IRVREG
@@ -319,7 +320,8 @@ type IR_VTBL
 	( _
 		byval op as integer, _
 		byval proc as FBSYMBOL ptr, _
-		byval ex as integer _
+		byval ex       as Integer, _
+		ByVal filename As ZString Ptr = 0 _
 	)
 
 	emitVarIniBegin as sub( byval sym as FBSYMBOL ptr )
@@ -598,7 +600,7 @@ declare function vregDump( byval v as IRVREG ptr ) as string
 
 #define irEmitSCOPEEND(s) ir.vtbl.emitScopeEnd( s )
 
-#define irEmitDBG(op, proc, ex) ir.vtbl.emitDBG( op, proc, ex )
+#define irEmitDBG(op, proc, ex, filename) ir.vtbl.emitDBG( op, proc, ex, filename )
 
 #define irEmitDECL( sym ) ir.vtbl.emitDECL( sym )
 
diff --git a/src/compiler/parser-inlineasm.bas b/src/compiler/parser-inlineasm.bas
index 6a87c0339..e11f08ff5 100644
--- a/src/compiler/parser-inlineasm.bas
+++ b/src/compiler/parser-inlineasm.bas
@@ -319,7 +319,7 @@ function cAsmBlock as integer
 	'' (AsmCode Comment? NewLine)+
 	do
 		if( issingleline = FALSE ) then
-			astAdd( astNewDBG( AST_OP_DBG_LINEINI, lexLineNum( ) ) )
+			astAdd( astNewDBG( AST_OP_DBG_LINEINI, lexLineNum( ),env.inf.incfile ))
 		end if
 
 		cAsmCode( )
diff --git a/src/compiler/parser-toplevel.bas b/src/compiler/parser-toplevel.bas
index 10a00d8e8..0bdbddecd 100644
--- a/src/compiler/parser-toplevel.bas
+++ b/src/compiler/parser-toplevel.bas
@@ -110,7 +110,7 @@ sub cProgram()
 	'' For each line...
 	do
 		'' line begin
-		astAdd( astNewDBG( AST_OP_DBG_LINEINI, lexLineNum( ) ) )
+		astAdd( astNewDBG( AST_OP_DBG_LINEINI, lexLineNum( ),env.inf.incfile ) )
 
 		'' Label?
 		cLabel( )
-- 
2.18.0.windows.1

Code: Select all

''test0.bas
print "begin main", __FILE__,__LINE__
#Include Once "test1.bi"
Print "free line in test0",__FILE__,__LINE__
sub test
 print "sub in test0",__FILE__,__LINE__
end sub
print "inside main", __FILE__,__LINE__

===========================================
''test1.bi
print "begin test1", __FILE__,__LINE__
#Include Once "test2.bi"
Print "free line in test1", __FILE__,__LINE__
Dim As Integer testone=10
sub testinone
	Print "sub in testone",__FILE__,__LINE__
	newprint( "test macro")
end sub

==============================================
''test2.bi
Print "begin test2", __FILE__,__LINE__
sub testintwo
	print "sub testintwo"
	#Include  "test3.bi"
	Print "inside testintwo", __FILE__,__LINE__
end sub
Asm
   nop
   nop
End Asm
Print "free line in test2",__FILE__,__LINE__
Dim As Integer testtwo=52
#Macro newprint(a)
   Print "-----------------------"
   Print a
   print "macro line in testtwo",__FILE__,__LINE__
   Print "-----------------------"
#EndMacro

===================================================
''test3.bi
Print "begin test3",__FILE__,__LINE__

Print "free line in test3",__FILE__,__LINE__
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Bug fix for debugging lines inside include files

Post by coderJeff »

If you think you might want to contribute other patches in future, I think it would be worthwhile to go the github route. It can seem to be complicated at first, but after you work through it once, it's easy. Assuming that you have a cloned copy of freebasic/fbc.

Share your changes on github:
1) create a github account
2) fork the freebasic/fbc project to your github account
3) add your github repo as a git remote in your local repo
4) create a new branch and commit your changes (patch) to that new branch
5) push the newly created branch to your fork in your github repo

Let FreeBASIC project know you have changes you want to merge:
6) on github, create a pull request from your fork of fbc to freebasic/fbc.

---
Looking through the test-suite, I don't think we have any tests currently for __LINE__ and __FILE__. If the tests are checking how __LINE__ and __FILE__ work, then I think that could be added to the test-suite. A test in tests/pp could be added to the unit-tests.

If the tests are specific to debug output, like how the information looks in a debugger, or fbc error messages, then that can probably only be manual.
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Bug fix for debugging lines inside include files

Post by coderJeff »

I started a pull request for this patch at https://github.com/freebasic/fbc/pull/96

This update seems to work well. Because fbc collects all the module level statements in to an implicit main, I was specifically looking to see what GDB reports when generating a backtrace. The root level call will always be "main", but because fbc's main function can be spread across multiple files, the source file and line number will change depending on where the call stack originated from. I think this is correct.

For testing, I used a main file and an include file to test backtraces in GDB. It appears to give good results. Every time I break on the sub check_backtrace(), I would examine GDB's backtrace for comparison to execution.

dbg0.bas

Code: Select all

declare sub dbg1_proc()

#define LOGMSG( msg ) print "  | " & msg

#macro SHOWLOCEX( fil, lin, func )
	LOGMSG( fil & "(" & lin & "): " & func )
#endmacro

#define SHOWLOC() SHOWLOCEX( __FILE__, __LINE__, __FUNCTION__ )

#macro HEADER( msg )
	print string(78, "=" )
	print "  " & msg
#endmacro

sub check_backtrace()
	print string(78, "-" )
end sub

HEADER( "PROGRAM START" )
SHOWLOC()
LOGMSG( "dbg0.bas    : BEGIN" )

sub dbg0_proc()
	SHOWLOC()
	LOGMSG( "dbg0.bas    : in   dbg0.bas:dbg0_proc()" )
	check_backtrace()
end sub

HEADER( "INSIDE MAIN STATEMENT" )
SHOWLOC()
LOGMSG( "dbg0.bas    : exec dbg0.bas:main" )
check_backtrace()

HEADER( "INSIDE MAIN CALL MAIN MODULE PROC" )
SHOWLOC()
LOGMSG( "dbg0.bas    : call dbg0.bas:dbg0_proc()" )
dbg0_proc()

HEADER( "INSIDE MAIN CALL MAIN INCLUDED PROC" )
SHOWLOC()
LOGMSG( "dbg0.bas    : call dbg1.bi :dbg1_proc()" )
dbg1_proc()

HEADER( "INCLUDING FILE" )
SHOWLOC()
#include once "dbg1.bi"

HEADER( "INSIDE MAIN STATEMENT" )
SHOWLOC()
LOGMSG( "dbg0.bas    : exec dbg0.bas:main" )
check_backtrace()

HEADER( "INSIDE MAIN CALL MAIN MODULE PROC" )
SHOWLOC()
LOGMSG( "dbg0.bas    : call dbg0.bas:dbg0_proc()" )
dbg0_proc()

HEADER( "INSIDE MAIN CALL INCLUDED PROC" )
SHOWLOC()
LOGMSG( "dbg0.bas    : call dbg1.bi :dbg1_proc()" )
dbg1_proc()

HEADER( "PROGRAM END" )
SHOWLOC()
LOGMSG( "dbg0.bas    : END" )
dbg1.bi

Code: Select all

sub dbg1_proc()
	SHOWLOC()
	LOGMSG( "dbg1.bi     : in   dbg1.bi :dbg1_proc()" )
	check_backtrace()
end sub

SHOWLOC()
LOGMSG( "dbg1.bi     : in   dbg1.bi :main" )
check_backtrace()

SHOWLOC()
LOGMSG( "dbg1.bi     : call dbg0.bas:dbg0_proc()" )
dbg0_proc()

SHOWLOC()
LOGMSG( "dbg1.bi     : call dbg1.bi :dbg1_proc()" )
dbg1_proc()
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bug fix for debugging lines inside include files

Post by SARG »

I'm sorry but the (mine) fix is not completly ok for stabs :
- there are too many "end of include" tag (those for main file)
- also need some changes about file names as win and dos are set in uppercase but not everywhere....
- with some stabs tags the linker removes data if it has already handled the file, need to use the simpler way (only one type of tag).
I have written an other version well tested this times. The changes are only in edbg_stabs.bas, the rest is ok.
Tomorrow I'll send the new fix.

Working on an other project I just did the 3 first steps (your previous post) for Github ;-( Hoping going further.
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Bug fix for debugging lines inside include files

Post by coderJeff »

oops! I already merged in the changes. I should have waited for your comments, sorry. I don't think it necessary to revert the changes, though. Sounds good, whenever you have next update ready.
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bug fix for debugging lines inside include files

Post by SARG »

Hi coderjeff,

I forgot to thank you for the integration in the repo.... :-)

As promised the complement for the fix.
4 bas files for testing in one of my previous posts.
NB : To be tested under linux.

Code: Select all

From 5793dd0fd6fc1c7f5d0fa93ef030c98710b7c392 Mon Sep 17 00:00:00 2001
From: Laurent <debug@aliceadsl.fr>
Date: Mon, 10 Sep 2018 15:54:22 +0200
Subject: Complement to Fix for debugging lines in include files
 but not in procedures

---
 src/compiler/edbg_stab.bas | 55 ++++++++++++++------------------------
 1 file changed, 20 insertions(+), 35 deletions(-)

diff --git a/src/compiler/edbg_stab.bas b/src/compiler/edbg_stab.bas
index 8af253760..5d60567d8 100644
--- a/src/compiler/edbg_stab.bas
+++ b/src/compiler/edbg_stab.bas
@@ -214,7 +214,11 @@ sub edbgEmitHeader( byval filename as zstring ptr )
 	ctx.typecnt 	= 1
 	ctx.label 		= NULL
 	ctx.incfile 	= NULL
-	ctx.filename	= *filename
+	#If defined( __FB_WIN32__ ) or defined( __FB_DOS__ )
+		ctx.filename	= UCase(*filename)
+   #Else
+   	ctx.filename	= *filename
+   #EndIf
 
 	'' emit source file name
 	lname = *symbUniqueLabel( )
@@ -240,7 +244,6 @@ sub edbgEmitHeader( byval filename as zstring ptr )
 
 	emitWriteStr( "" )
 
-	hEmitSTABS( STAB_TYPE_BINCL, filename, 0, 0 )
 end sub
 
 '':::::
@@ -1018,40 +1021,22 @@ end sub
 sub edbgInclude( byval incfile as zstring ptr )
 	dim as string lname
 
-	'' incfile is the new include file for which we should open a block.
-	'' incfile can be NULL to indicate that no next include file is coming,
-	'' in which case we just want to return to the toplevel .bas file name,
-	'' if we previously opened an include file block.
+	'' incfile is the new include file or main file name
 
-	'' Already in the correct block?
-	'' (same include file, or NULL for toplevel)
-	if( incfile = ctx.incfile ) then
-		exit sub
-	end if
-
-	'' Currently in an include file block?
-	if( ctx.incfile ) then
-		'' Close it
-		hEmitSTABS( STAB_TYPE_EINCL, "", 0, 0 )
-
-		'' "Return" to the main filename, if no new include file block
-		'' will be opened
-		if( incfile = NULL ) then
-			emitSECTION( IR_SECTION_CODE, 0 )
-			lname = *symbUniqueLabel( )
-			hEmitSTABS( STAB_TYPE_SOL, ctx.filename, 0, 0, lname )
-			hLABEL( lname )
-		end if
-	end if
+   '' coming from _close incfile is null so no real need to change
+	If( incfile = NULL )Then
+	   Exit Sub
+	EndIf
 
-	ctx.incfile = incfile
+	'' Already handling the correct name
+	if( incfile = ctx.incfile ) Then
+		exit sub
+	end If
+			
+	emitSECTION( IR_SECTION_CODE, 0 )
+	lname = *symbUniqueLabel( )
+	hEmitSTABS( STAB_TYPE_SOL, incfile, 0, 0, lname )
+	hLABEL( lname )
 
-	'' Open new include file block if needed
-	if( incfile ) then
-		hEmitSTABS( STAB_TYPE_BINCL, incfile, 0, 0 )
-		emitSECTION( IR_SECTION_CODE, 0 )
-		lname = *symbUniqueLabel( )
-		hEmitSTABS( STAB_TYPE_SOL, incfile, 0, 0, lname )
-		hLABEL( lname )
-	end if
+   ctx.incfile = incfile
 end sub
-- 
2.18.0.windows.1

coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Bug fix for debugging lines inside include files

Post by coderJeff »

Thanks SARG. I have been investigating this last patch.

You probably already know, because I see you had code in fbdebugger (now commented) to handle stabs BINCL, EINCL, EXCL tags. BINCL & EINCL purpose is to remove duplicate debug information from multiple modules (object files) at link time, replacing the duplicates with EXCL, to reduce the size of the debugging information. This use of BINCL/EINCL is probably only effective if the entire header debug type information is emitted the same way every time. But, because fbc only emits the type information that is used, the actual type information is often different for every object file, so we are already minimizing the object file size by only emitting the type information needed. Because of these reasons, we can probably remove use of BINCL & EINCL and only use SOL tag, and file sizes will probably still be about the same.

I'm doing some tests with window.bi and debugging information. windows.bi can easily add 50K+, even when only a few types are used. If there is a possibility for a significant file size savings, then maybe should look at fixing our use of BINCL/EINCL instead of removal.
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bug fix for debugging lines inside include files

Post by SARG »

Hi coderjeff,

The issue with bincl and eincl is that even debug data is not duplicated the linker removed it just because it's based on the file name (include files) not on the data itself.

Test with the test files I have provided or look at the small example below. As some lines from include files are interlaced with those of main file or other inc files the linker excludes them.
I suppose that is not a good way to code but it's possible to do so need to be taken in account IMO.


inc1 file :
line1 of code outside any procedure
#include inc2 file
sub
...
end sub
line2 of code outside any procedure

--> Inc1 file would appears several times : inc1 (sub), other procedures, inc1 (line1), inc2 (lines from inc2), inc1 (line2).
And in this case no need to remove information about line1, line2....
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Bug fix for debugging lines inside include files

Post by coderJeff »

Thanks SARG. I've created a pull request https://github.com/freebasic/fbc/pull/99 with your latest patch.

I have a feeling this is not the last we will see with this area of the compiler, but I think it gets us closer to how fbc works with include files and debug information.
Post Reply