Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a Delphi application with redirected output

I've got a console application that crashes with an I/O error 6 when the output is redirected to a file. It probably has something to do with the fact that the console application changes the text color, which doesn't make much sense in a file.

  • This works: c:\dir\app.exe
  • This crashes: c:\dir\app.exe >out.txt

When I supply >out.txt as a parameter in the IDE (run\parameters\parameters\), I just get >out.txt as a parameter.

How can I debug the application with the stdout redirected to a file instead of the console?

like image 357
Wouter van Nifterick Avatar asked Nov 22 '10 18:11

Wouter van Nifterick


2 Answers

Redirection is made by the command line interpreter, in windows it is cmd.exe

To debug the application, just launch a cmd.exe with propers arguments to launch your application and redirect the output, for example:

cmd.exe /c "yourapplication.exe >redirect.txt"

To make this happen from inside IDE in order to debug, configure cmd.exe as the host application (Run/Parameters):

Run/Parameters dialog

Put a breakpoint where you want to stop, and launch a new cmd.exe (Project/Load process) with "Run to first source" after load action:

Load process dialog

And you're done... the debugger must stop the application at your breakpoint.

like image 152
jachguate Avatar answered Nov 03 '22 01:11

jachguate


You could try remote debugging:

  • at the beginning of the application, add a ReadLn; which gives you time to attach to the process from within Delphi

  • start the application from a command line (specifying the >out.txt parameter)

  • in Delphi, connect with the app process (Run | Attach to Process...), set a breakpoint and then switch to the application to enter a key

Hint: a debugger breakpoint can also be set in code:

asm
  int 3
end;
like image 27
mjn Avatar answered Nov 03 '22 00:11

mjn