Search code examples
delphibreakpoints

How to save breakpoints using the Delphi IDE?


How can I save breakpoints using the Delphi IDE? I only know how to store the settings in a .dsk file.

I am using Delphi 2007.


Solution

  • I'm assuming from your mention of the .Dsk file that you are aware that the breakpoints are stored in there, but want to save them yourself for some reason. Of course, the easiest method of getting a list of saved breakpoints is simply to read them from the .Dsk file, but that assumes that it has been saved to disk, which usually occurs when you close the project file.

    You can write your own IDE plug-in to get a list of currently-set breakpoints and save them in any way you want. The minimalist example below shows how to do this - see the GetBreakpoints method for details. To use this in the IDE, you would create a new package which requires DesignIde.Dcp. Make sure that the output directory for the .Bpl file is either where your 3rd-party .Bpls are stored on or is on your path. You can then install the package in the IDE vie Install packages from the IDE's menu.

    As you can see, it works by using the BorlandIDEServices interface in the ToolsAPI units to get an IOTADebuggerServices interface, and then uses that to iterate its SourceBkpts list and saves a number of properties of each IOTASourceBreakpoint in that list.

    Note that

    • You can also retrieve a list of address breakpoints and save those in a similar fashion.

    • Both kinds of breakpoint interface in ToolsAPI have property setters as well as getters, so you could modify existing breakpoints in code and conceivably create new ones.

    Code

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ToolsApi;
    
    type
      TBreakpointSaveForm = class(TForm)
        Memo1: TMemo;
        btnGetBreakpoints: TButton;
        procedure btnGetBreakpointsClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
      protected
      public
        procedure GetBreakpoints;
      end;
    
    var
      BreakpointSaveForm: TBreakpointSaveForm;
    
    procedure Register;
    
    implementation
    
    {$R *.DFM}
    
    procedure TBreakpointSaveForm.GetBreakpoints;
    var
      DebugSvcs: IOTADebuggerServices;
    
      procedure SaveBreakpoint(BreakPoint : IOTASourceBreakpoint);
      begin
        Memo1.Lines.Add('File:      ' + Breakpoint.FileName);
        Memo1.Lines.Add('LineNo:    ' + IntToStr(Breakpoint.LineNumber));
        Memo1.Lines.Add('Passcount: ' + IntToStr(Breakpoint.Passcount));
        Memo1.Lines.Add('');
      end;
    
      procedure SaveBreakpoints;
      var
        i : Integer;
        BreakPoint : IOTASourceBreakpoint;
      begin
        Memo1.Lines.Add('Source breakpoint count : '+ IntToStr(DebugSvcs.GetSourceBkptCount));
        for i := 0 to DebugSvcs.GetSourceBkptCount - 1 do begin
          Breakpoint := DebugSvcs.SourceBkpts[i];
          SaveBreakpoint(Breakpoint);
        end;
      end;
    
    begin
      if not Supports(BorlandIDEServices, IOTADebuggerServices, DebugSvcs) then begin
        ShowMessage('Failed to get IOTADebuggerServices interface');
        exit;
      end;
      Memo1.Lines.Clear;
      SaveBreakpoints;
    end;
    
    procedure Register;
    begin
    end;
    
    initialization
      BreakpointSaveForm := TBreakpointSaveForm.Create(Application);
      BreakpointSaveForm.Show;
    
    finalization
    
      if Assigned(BreakpointSaveForm) then
        BreakpointSaveForm.Free;
    end.
    
    procedure TBreakpointSaveForm.btnGetBreakpointsClick(Sender: TObject);
    begin
      GetBreakpoints;
    end;