Sign in Join
Wiki copytemporary

Debugger Console App: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Introduction
22
33This page contains information on how to debug console applications in Lazarus. That is where to find the output (writeln) of your app, and where to type the input (readln).
44
55This page was written for Lazarus 0.9.30 and above.
66
77## Windows
88
99On Windows, the application will open its own console window, just as it would outside the debugger. There is no need to do anything special apart from adding a `ReadLn;` before the application terminates so that the console window does not automatically close until you press `Enter`.
1010
1111## Linux
1212
1313### Lazarus 0.9.30 / 0.9.30.2
1414
1515Please see the notes in the#Mac section.
1616
1717### Lazarus 0.9.31 / 1.0 and higher
1818
1919You can open a console window from the menu: "View" =\> "Debug Windows" =\> "Terminal Output"
2020
2121It shows the raw output of your application. Note it has no support for any escape sequences, as some terminals may have. Anything you type into this window will be sent to your application.
2222
2323## Mac & Linux
2424
2525Unfortunately there is currently no built-in support for debugging console applications.
2626
2727Console applications do not have their own terminal, but use the terminal they were started from (the terminal of the shell they were started from). When started from within Lazarus, there is no such terminal. You can specify a "starter application" (like xterm) under menu "Run" =\> "Run parameters". however this only works if you start your application without a debugger. **This does not work for debugging.**
2828
2929There are a couple of workarounds.
3030
3131### Redirect output to file
3232
3333In menu "Run" =\> "Run parameters": specify in "command line parameters":
3434
35-` >/somedir/somefile`
35+```text
36+ >/somedir/somefile
37+```
3638
3739Then outside Lazarus open a shell and run
3840
3941``` bash
4042tail -f /somedir/somefile
4143```
4244
4345### Redirect input and output to a pty (terminal)
4446
4547Open a terminal outside of Lazarus and run
4648
4749```bash>
4850pty
4951</syntaxhighlight>
5052On Ubuntu 18, the command is
5153<syntaxhighlight lang=
5254```
5355
5456tty
5557
5658It should return something like
5759
58-` /dev/pty/2`
60+```text
61+ /dev/pty/2
62+```
5963
6064The exact value depends on the OS and the number of terminals already open.
6165
6266Note
6367The returned value is only valid as long as the terminal stays open, so if you open a new terminal next time you debug in Lazarus, you need to adapt all settings to the new value.
6468
6569In menu "Run" =\> "Run parameters": specify in "command line parameters":
6670
67-` >/dev/pty/2 </dev/pty/2`
71+```text
72+ >/dev/pty/2 </dev/pty/2
73+```
6874
6975If you want to use the terminal to send input to your application, then you may need to stop the shell in it from reacting to the input (unless you started a terminal without a shell). Run something like
7076
7177``` bash
7278tail -f /some/empty/file/that/does/not/change
7379```
7480
7581> **Note:** This means you still can't use ctrl-c in the input.
7682
7783#### Debugger startup options
7884
7985In the global options dialog, on the debugger page, you can specify parameters given to **gdb** (do ***not*** use the in/output redirect **\>** or **\<** here, which is meant for interactive **bash** but use something like **--args**).
8086
8187It may be possible to specify the **pty** here, but there is currently no further info.
8288
8389### gdbserver
8490
8591Use the gdbserver based debugger. You can start gdbserver on the same computer as the IDE. Run gdbserver inside the terminal that you want to use for your stdout/stdin
8692
8793### Lazarus 0.9.31
8894
8995In the global options dialog, on the debugger page is an entry "ConsoleTty". You can enter the result of "pty" here. Then you do not need to specify any **\<** or **\>** redirects.
9096
9197### Lazarus 2.x with LLDB on macOS aarch64
9298
9399In the global options dialog, on the debugger page there is a "LaunchNewTerminal" option when **LLDB** is selected (currently the only option for this platform).
94100
95101However if you tick that without making the following prerequisites, **lldb** (and thus Lazarus) will freeze.
96102
971031, First of all, check that your **terminal shell** is listed in the **/etc/shells** file.
98104
99105You can check your current shell by this command:
100106
101107``` bash
102108echo $SHELL
103109```
104110
105111And list the current contents of this file by this command:
106112
107113``` bash
108114cat /etc/shells
109115```
110116
111117If you installed your shell e.g. via Homebrew, your shell is probably not on the list. In this case you can add it manually, to make the following commands will work.
112118
113119If you have to edit this file, **be extremely cautious**, and do it **on your own risk**! You have been noticed!
114120
1151212, (Optional step) If you use different terminal emulator than the default one, you can test the following command.
116122
117123``` bash
118124osascript -e "tell application \"Terminal\" to do script \"echo lazarus\""
119125```
120126
121127The system protection dialog should pop-up, about do you allow xy application to control the terminal. You should answer yes, and check that the freshly popped-up built-in terminal app writes "lazarus".
122128
1231293, Open Lazarus. Define a new external tool in the Tools menu, with the program of **/bin/sh** and the following parameters.
124130
125131``` bash
126132-c 'osascript -e "tell application \"Terminal\" to do script \"echo lazarus\""'
127133```
128134
129135After you've added the tool, restart Lazarus, and click on it. The same system protection dialog will pop up, but for Lazarus now. Say yes, to allow it.
130136
131137After doing these prerequsities, you can safely tick the "LaunchNewTerminal" option in the global settings, and every time you start a debugging, you can control stdin/stdout of the debugged program in a separate terminal window.
132138
133139Note: Debugging will be really, really, really slow, unless you add the dedicated Debug/Release modes to your project, and use the Debug one for debugging. Use Dwarf2 symbols at the moment when possible.
134140
135141## See also
136142
137143- [[GDB Debugger Tips|General tips/info on debugging]]
138144
139145---
140146*From the Free Pascal wiki, page "Debugger Console App", as it stood on 2023-06-18 after a change by Shaglonux, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
141147Category: Debugging
142148Category: Lazarus

Back to history