Board index » delphi » Tracing program execution, & logging to file

Tracing program execution, & logging to file

It ought to be possible, provided that a program is compiled with the
right debug options, to monitor a program's execution, and as each
procedure/function is entered or exited, log that fact to a file. I know
enough about the program formats to know that all the information
required is there, but not enough to know how to use it :-(

The IDE de{*word*81} can present me with a call stack at any point - what I
want to do is generate an ongoing report of this form as the program
runs. Onviously the log is going to get quite large, and the program is
going to be slowed down quite a lot, but it would be a very useful
debugging aid. Since I am sure it is possible I am also sure that someone
else must already have done something similar, but I have no ideas where
to find it.

As an example of what I want to achieve, consider a simple program:

    program Sample;
    uses Trace;

    procedure Display (X : Integer);
    begin
        write (X, ', ');
    end;

    function MyFunc (a, b, c : Integer) : Integer;
    begin
        Display (a);
        Display (b);
        Display (c);
        MyFunc := a + b + c;
    end;

    begin
        MyFunc (1,2,3);
        MyFunc (2,3,4);
    end.

When run it would produce a trace report something like:

    >> Sample
     >> MyFunc(1,2,3)
      >> Display(1)
      << Display
      >> Display(2)
      << Display
      >> Display(3)
      << Display
     << MyFunc=6
     >> MyFunc(2,3,4)
      >> Display(2)
      << Display
      >> Display(3)
      << Display
      >> Display(4)
      << Display
     << MyFunc=9
    << Sample

Trace would take the form of a unit which, presumably, somehow
intercepted procedure/function calls/returns and examined the stack to
log the required information (maybe by use of the processor's single-step
interrupt?) Some method of disabling the trace for certain parts of the
code would be useful, but not essential. Can anyone give me some clues on
how to write this, or where I can find something similar already in
existance (comercial, if necessary)?

Mark.
--
int MeaningOfLife (void) { return 42; }   // m...@holly.demon.co.uk
function MeaningOfLife :integer; begin MeaningOfLife := 6 * 9 end;

 

Re:Tracing program execution, & logging to file


Quote
Mark Rogers (m...@holly.demon.co.uk) wrote:

: It ought to be possible, provided that a program is compiled with the
: right debug options, to monitor a program's execution, and as each
: procedure/function is entered or exited, log that fact to a file. I know
: enough about the program formats to know that all the information
: required is there, but not enough to know how to use it :-(

: The IDE de{*word*81} can present me with a call stack at any point - what I
: want to do is generate an ongoing report of this form as the program
: runs. Onviously the log is going to get quite large, and the program is
: going to be slowed down quite a lot, but it would be a very useful
: debugging aid. Since I am sure it is possible I am also sure that someone
: else must already have done something similar, but I have no ideas where
: to find it.

: As an example of what I want to achieve, consider a simple program:

:     program Sample;
:     uses Trace;

:     procedure Display (X : Integer);
:     begin
:         write (X, ', ');
:     end;

:     function MyFunc (a, b, c : Integer) : Integer;
:     begin
:         Display (a);
:         Display (b);
:         Display (c);
:         MyFunc := a + b + c;
:     end;

:     begin
:         MyFunc (1,2,3);
:         MyFunc (2,3,4);
:     end.

: When run it would produce a trace report something like:

:     >> Sample
:      >> MyFunc(1,2,3)
:       >> Display(1)
:       << Display
:       >> Display(2)
:       << Display
:       >> Display(3)
:       << Display
:      << MyFunc=6
:      >> MyFunc(2,3,4)
:       >> Display(2)
:       << Display
:       >> Display(3)
:       << Display
:       >> Display(4)
:       << Display
:      << MyFunc=9
:     << Sample

: Trace would take the form of a unit which, presumably, somehow
: intercepted procedure/function calls/returns and examined the stack to
: log the required information (maybe by use of the processor's single-step
: interrupt?) Some method of disabling the trace for certain parts of the
: code would be useful, but not essential. Can anyone give me some clues on
: how to write this, or where I can find something similar already in
: existance (comercial, if necessary)?

: Mark.
: --
: int MeaningOfLife (void) { return 42; }   // m...@holly.demon.co.uk
: function MeaningOfLife :integer; begin MeaningOfLife := 6 * 9 end;

    Hello Mark,

  This is an interesting idea. If you have the sources for the runtime
library, e.g. if you have BP not TP, you could rewrite the stack check
routine in the System unit, to output the info.
  Then BP with $S+ calls this stack check routine everytime a function is
called.
  In the stack-check routine you could install an INT03 handler, search the
returning retf (or ret) instruction in the calling function and overwrite it
with an INT 03 ($CC) instruction. When the function is completed it calls
int 03... Int 03 could do the logging.

  The only problem with this solution is that you cannot use it in the IDE
if you want to debug the program, since the BP de{*word*81} uses INT03 itself.

  This is only a rough idea, and may be impossible to implement...

                        Best regards,

                                 Balazs Scheidler

----------------------------------------------------------------------
Bem Street 42.  | Learning informatics at the University of Veszprem
7100 Szekszard  | e-mail: ba...@hal2000.hal.vein.hu
Hungary         | Don't use that evil Wimpdoze, get Warped !  :-)
======================================================================
Author of Turbo Vision Resource WorkShop, the new 3.0 version is able
to generate C++ & Pascal source code. Feel free to ask for details.
----------------------------------------------------------------------

Other Threads