A passive debugging solution for CLX

By: Chee Wee Chua

Abstract: Debugging cross-platform applications is easy with this useful code. By Chee Wee Chua.

Developing a cross-platform application using Borland CLX -- the cross-platform application framework for Delphi 6 and Kylix -- is a joy. But sometimes even developers who use the most powerful tools encounter problems.

You may have come across situations in which the IDE's integrated debugger is interfering with the process of debugging. No problem -- that's why NuMega invented SoftICE. Right?

Er, wrong. At this writing, there is no SoftICE for Linux.

What's a poor programmer to do?

When the going gets tough, tough programmers dust off the old tried-and-true solutions. Like embedding debug messages in your code and writing them to the screen. Preferably to a separate window -- maybe a console window -- so the application you're trying to debug doesn't get its activities messed up.

If you have developed a console application in Windows, youl know that there is an application type directive in the beginning of the project source that tells Delphi to produce a console app. If you are a Windows programming old-timer, you're aware that a Windows application can have either a Windows GUI or a command-line interface. What is not clear is how you make a Delphi application do both at the same time.

It is, in fact, very simple to do.

Develop your GUI application normally. When you're done, view the project source and insert the application type directive for a console application into the project source, right after your application name, like this.

In Linux, the same code works. Now, in order to passively debug your project, all you have to do is develop a Pascal unit that contains a global Boolean variable such as IsDebugging and a routine such as OutputDebugStr that takes a string parameter. (Write a command-line parser that determines whether an option such as -debug is present or not. If it is present, set IsDebugging to true.) Now pepper each block of code that you want to debug or monitor with OutputDebugStr messages.

I have developed an example CLI_IntegratedDebugger unit to get you started.

For this technique to work in Linux, you should start your application from the console. If your application is named CLXRunner in the current directory, open up a console and type ./CLXRunner in order to execute your application. Remember to add CLI_IntegratedDebugger to the uses clause of each unit, otherwise, your project might not compile.

In my example, I added an IsConsole check to the IsDebugging check so that, should you accidentally delete the application type directive, your application will not write blindly to the console and cause your application to fail or behave unpredictably.

I hope this article will provide you another tool in your arsenal of weapons that can help you track down bugs in CLX applications.

Chee Wee lives in Singapore. He is certified under CCNA, MCSE, SCJP, and SCSA. He can be reached at chuacw@rightsecurity.biz.

Server Response from: ETNASC01