Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Stack

Status
Not open for further replies.

lachlanK

Programmer
Dec 4, 2002
7
NZ
Hi, occasionally when VFP crashes, you receive a call stack trace, with line numbers (similar to what you can view from the debugger).

Being able to access this from an error handling routine would be extremely useful in trying to track bugs, as often a program will crash in a common function or procedure, and you have no idea what the user was actually doing to cause the error...

Any ideas?

Cheers,

- Lachlan
 
Thanks, but that article didn't really address my query...

Anyone else?

- Lachlan
 
Here's a little routine I wrote. It displays a small window notifying the user that an error occured, lists the program call stack, and does status and memory dumps to temp files so you can see the status of tables, memvars, and so on. Use the following line in your start up code:

Code:
ON ERROR DO errhand WITH ;
   ERROR( ), MESSAGE( ), MESSAGE(1), PROGRAM( ), LINENO( )

Then, put this routine in a form method or procedure file:
Code:
PROCEDURE errhand
   PARAMETER merror, MESS, mess1, prog, mlineno
   
   IF merror = 43 .OR. merror = 1405
      DO nomem
   ENDIF ( merror = 43 .OR. merror = 1405 )
   
   STORE DTOC(DATE()) TO datestr
   STORE TIME() TO timestr
   
   STORE SYS(3) TO errfile
   
   STORE errfile + '.err' TO prgdump
   x=FCREATE(prgdump)
   IF x < 0
      DO caution WITH ;
         &quot;FATAL ERROR | Retval: &quot;+ ALLTRIM(STR(x)) ,;
         &quot;Unable to open error log file: {&quot;+ prgdump + &quot;}&quot;
      POP MENU _msysmenu
      ON ERROR
      CANCEL
   ELSE
      =FPUTS(x, '------' + datestr + ' at ' + timestr + &quot; File: &quot; + errfile + '------')
      =FPUTS(x, 'Username...................: ' + m.s_username)
      =FPUTS(x, 'Error number...............: ' + LTRIM(STR(merror)))
      =FPUTS(x, 'Error message..............: ' + MESS)
      =FPUTS(x, 'Error message parameter....: ' + SYS(2018))
      =FPUTS(x, 'Line of code with error....: ' + mess1)
      =FPUTS(x, 'Line number of error.......: ' + LTRIM(STR(mlineno)))
      
      STORE 1 TO prgcountr
      STORE '~~~~' TO theprg
      DO WHILE ALLTRIM(theprg) # ''
         theprg = PROGRAM(prgcountr)
         =FPUTS(x, 'Program level ' + LTRIM(STR(prgcountr)) + ')...........: ' + theprg)
         prgcountr = prgcountr + 1
      ENDDO ( ALLTRIM(theprg) # '' )
      
      =FPUTS(x, 'User allocated memory......: ' + SYS(1016))
      =FPUTS(x, 'Selected DBF...............: ' + DBF())
      =FPUTS(x, 'Current lock status........: ' + SYS(2011))
      =FPUTS(x, 'Controlling index number...: ' + SYS(21))
      =FPUTS(x, 'Controlling index name.....: ' + SYS(22))
      =FPUTS(x, '-------------------------------------------------------------------')
      
      =FCLOSE(x)
   ENDIF ( x < 0 )
   
   LIST MEMORY TO errfile+'.erm' NOCONSOLE
   LIST STATUS TO errfile+'.ers' NOCONSOLE
   
   DEFINE WINDOW err;
      FROM  0.000, 0.000  ;
      TO 12, 80 ;
      TITLE &quot;*** Internal Error ***&quot; ;
      FONT &quot;FoxFont&quot;, 8 ;
      FLOAT ;
      CLOSE ;
      nominimize ;
      SYSTEM ;
      COLOR RGB(,,,255,0,0)
   MOVE WINDOW err CENTER
   
   ACTIVATE WINDOW err TOP
   
   @   1,  1 SAY &quot;A system error has occurred.&quot; FONT &quot;System&quot;, 9 COLOR RGB(255,255,0,255,0,0)
   @ 2.5,  1 SAY &quot;Please inform programming staff.&quot; FONT &quot;System&quot;, 9 COLOR RGB(255,255,0,255,0,0)
   @   4,  1 SAY &quot;You may try 'Retry', but if errors&quot; FONT &quot;System&quot;, 9 COLOR RGB(255,255,0,255,0,0)
   @ 5.5,  1 SAY &quot;persist, press 'Cancel'.&quot; FONT &quot;System&quot;, 9 COLOR RGB(255,255,0,255,0,0)
   STORE '' TO booboo
   
   @ 9.5, 20 GET booboo FUNCTION '*HT \<Retry;\<Cancel' ;
      SIZE 1.5, 10, 0.667 ;
      FONT &quot;MS Sans Serif&quot;, 8 ;
      STYLE &quot;B&quot;
   
   READ CYCLE
   RELEASE WINDOW err
   
   IF booboo = 'Retry'
      RETURN
   ELSE
      ON ERROR
      POP MENU _msysmenu
      CANCEL
   ENDIF ( booboo = 'Retry' )
   
   RETURN
Dave S.
 
Thank You!

Your error handling routine looks very familiar, I guess all VFP programmers go down similar roads in this respect.

The vital piece of information I was missing was the existence of the PROGRAM() function. Shame can't get a line number for each...

Cheers,

- Lachlan
 
Dave, or anyone else who can answer...

I read through this and mostly understand

I am looking to loop through the call stack and simply return the information to an errorlog file.

Is this the section I should be interested in?

STORE 1 TO prgcountr
STORE '~~~~' TO theprg
DO WHILE ALLTRIM(theprg) # ''
theprg = PROGRAM(prgcountr)
=FPUTS(x, 'Program level ' + LTRIM(STR(prgcountr)) + ')...........: ' + theprg)
prgcountr = prgcountr + 1
ENDDO ( ALLTRIM(theprg) # '' )

Is this the call stack info? I don't really know what I am looking for, I have never had this task before and this is new to me. I can't find anything in the help files in Foxpro... can anyone give me some insight on this? Is this what I need?

 
Yes that is the relevant section. Look in VFP help on the PROGRAM() function, this will give you some insight into how the code snippet works.

In addition to the section of code you have cut out, you will need the part which opens the error file:

STORE errfile + '.err' TO prgdump
x=FCREATE(prgdump)
IF x < 0
DO caution WITH ;
&quot;FATAL ERROR | Retval: &quot;+ ALLTRIM(STR(x)) ,;
&quot;Unable to open error log file: {&quot;+ prgdump + &quot;}&quot;
POP MENU _msysmenu
ON ERROR
CANCEL
ELSE

Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top