Sign in Join
Wiki copytemporary

Debugger Console App: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Debugger Console App

OldNewLine
1+## Introduction
2+
3+This 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).
4+
5+This page was written for Lazarus 0.9.30 and above.
6+
7+## Windows
8+
9+On 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 <span class="kbd">Enter</span>.
10+
11+## Linux
12+
13+### Lazarus 0.9.30 / 0.9.30.2
14+
15+Please see the notes in the#Mac section.
16+
17+### Lazarus 0.9.31 / 1.0 and higher
18+
19+You can open a console window from the menu: "View" =\> "Debug Windows" =\> "Terminal Output"
20+
21+It 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.
22+
23+## Mac & Linux
24+
25+Unfortunately there is currently no built-in support for debugging console applications.
26+
27+Console 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.**
28+
29+There are a couple of workarounds.
30+
31+### Redirect output to file
32+
33+In menu "Run" =\> "Run parameters": specify in "command line parameters":
34+
35+` >/somedir/somefile`
36+
37+Then outside Lazarus open a shell and run
38+
39+``` bash
40+tail -f /somedir/somefile
41+```
42+
43+### Redirect input and output to a pty (terminal)
44+
45+Open a terminal outside of Lazarus and run
46+
47+```bash>
48+pty
49+</syntaxhighlight>
50+On Ubuntu 18, the command is
51+<syntaxhighlight lang=
52+```
53+
54+tty
55+
56+</syntaxhighlight>
57+
58+It should return something like
59+
60+` /dev/pty/2`
61+
62+The exact value depends on the OS and the number of terminals already open.
63+
64+Note
65+The 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.
66+
67+In menu "Run" =\> "Run parameters": specify in "command line parameters":
68+
69+` >/dev/pty/2 </dev/pty/2`
70+
71+If 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
72+
73+``` bash
74+tail -f /some/empty/file/that/does/not/change
75+```
76+
77+> **Note:** This means you still can't use ctrl-c in the input.
78+
79+#### Debugger startup options
80+
81+In 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**).
82+
83+It may be possible to specify the **pty** here, but there is currently no further info.
84+
85+### gdbserver
86+
87+Use 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
88+
89+### Lazarus 0.9.31
90+
91+In 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.
92+
93+### Lazarus 2.x with LLDB on macOS aarch64
94+
95+In the global options dialog, on the debugger page there is a "LaunchNewTerminal" option when **LLDB** is selected (currently the only option for this platform).
96+
97+However if you tick that without making the following prerequisites, **lldb** (and thus Lazarus) will freeze.
98+
99+1, First of all, check that your **terminal shell** is listed in the **/etc/shells** file.
100+
101+You can check your current shell by this command:
102+
103+``` bash
104+echo $SHELL
105+```
106+
107+And list the current contents of this file by this command:
108+
109+``` bash
110+cat /etc/shells
111+```
112+
113+If 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.
114+
115+If you have to edit this file, **be extremely cautious**, and do it **on your own risk**! You have been noticed!
116+
117+2, (Optional step) If you use different terminal emulator than the default one, you can test the following command.
118+
119+``` bash
120+osascript -e "tell application \"Terminal\" to do script \"echo lazarus\""
121+```
122+
123+The 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".
124+
125+3, Open Lazarus. Define a new external tool in the Tools menu, with the program of **/bin/sh** and the following parameters.
126+
127+``` bash
128+-c 'osascript -e "tell application \"Terminal\" to do script \"echo lazarus\""'
129+```
130+
131+After 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.
132+
133+After 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.
134+
135+Note: 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.
136+
137+## See also
138+
139+- [[GDB Debugger Tips|General tips/info on debugging]]
140+
141+---
142+*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).*
143+Category: Debugging
144+Category: Lazarus

Back to history