Print debugging statement: Difference between revisions

(→‎{{header|zkl}}: added code)
Line 198:
from file: debug.p6, sub: beta, line: 28
from file: debug.p6, line: 36</pre>
 
=={{header|Phix}}==
The ? statement is used as a quick shorthand to dump variable contents and expression results.<br>
For provenance, the following was added to builtins/reflections.e (for now/not an autoinclude, 0.8.1+), and throw()
was also tweaked to convert supplied addresses, which it previously did not. It proved to be quite an easy enhancement
to the language, albeit as yet undocumented.
<lang Phix>function _debug_info()
-- use throw to convert a return address and routine number
-- from the call stack into a proper line number, etc.
-- (private, not called direct/from outside this file)
integer rtn
atom ret_addr
#ilASM{
[32]
mov edx,[ebp+20] -- prev_ebp
mov eax,[edx+28] -- return address
mov edx,[edx+20] -- prev_ebp
lea edi,[ret_addr]
call :%pStoreMint
mov eax,[edx+8] -- calling routine no
mov [rtn],eax
[64]
mov rdx,[rbp+40] -- prev_ebp
mov rax,[rdx+56] -- return address
mov rdx,[rdx+40] -- prev_ebp
lea rdi,[ret_addr]
call :%pStoreMint
mov rax,[rdx+16] -- calling routine no
mov [rtn],rax
[]
}
try
throw({1,ret_addr-1,-1,rtn,-1,-1,-1})
catch e
return e
end try
end function
 
-- NOTE: following four routines must all use the exact same nesting level.
 
global function debug_info()
return _debug_info()
end function
 
global function debug_line()
return _debug_info()[E_LINE]
end function
 
global function debug_file()
return _debug_info()[E_FILE]
end function
 
global function debug_path()
return _debug_info()[E_PATH]
end function</lang>
This can be used as follows (0.8.1+)
<lang Phix>include builtins/reflections.e
?debug_info()
?debug_line()
procedure test()
?debug_info()
?debug_line()
?debug_file()
printf(1,"This is line %d in file %s\n",{debug_line(),debug_file()})
end procedure
test()</lang>
{{out}}
<pre>
{1,7544919,3,21,"-1","test.exw","C:\\Program Files (x86)\\Phix\\"}
4
{1,7545341,6,1034,"test","test.exw","C:\\Program Files (x86)\\Phix\\"}
7
"test.exw"
This is line 9 in file test.exw
</pre>
See the throw() documentation for more details, especially regarding the debug_info() results.<br>
There is no routine name for the first debug_info() call, so you get "-1" in [E_NAME].<br>
The line numbers 3, 4, 6, 7, and 9 are returned for five of the seven calls.
 
=={{header|Pyret}}==
7,795

edits