Sign in Join
Wiki copytemporary

Remote Debugging: what changed

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

OldNewLine
11## Introduction
22
33Remote debugging means you work on your local computer and you want to start and debug a program on another computer, the remote machine. In the following examples the name of the local computer is 'localcomp' and the name of the remote computer is 'remotecomp'.
44
55> **Note:** Remote debugging will only work if the gdb supports async mode (see gdb doc for "set target async"). GDBServer usually does. GDB does for some targets only.
66>
77> In some cases gdb/gdbserver does support it, but not if "mi" mode is used (used by the IDE. A workaround for this must yet be implemented in the IDE.\
88> **IF async mode is not supported, or not for "mi" then:**
99>
1010> - It is currently not possible to use the "Pause" function to interrupt the running application.
1111> - It is not possible to modify break-/watch-points, while the application in the debugger is running.
1212> - One should no switch editor tabs, or open/close units, while the application in the debugger is running.
1313>
1414> The 2nd and 3rd can be done, if the app in the debugger is paused. The only way for the app to become paused is by reaching a breakpoint that was set before the app was run.
1515
1616## Using SSH (Secure Shell)
1717
1818SSH (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. It provides secure encrypted communications between two untrusted hosts over an insecure network. X11 connections and arbitrary TCP/IP ports can also be forwarded over the secure channel.
1919
2020See the SSH man page for details. This text only covers SSH protocol 2. For differences for protocol 1 see the SSH man page.
2121
2222### Requirements for Lazarus
2323
2424You must be able to log in via ssh to the remote machine (the computer where the program will run). This means the remote machine has an installed and running SSH server and you have an account allowed to login from your local machine (the computer where the Lazarus IDE is running).
2525
2626You can test this by doing:
2727
2828``` bash
2929
3030ssh username@remotecomp ls -la
3131```
3232
3333This will create an SSH connection to 'remotecomp' with the username 'username'. After authentication it will print out a directory listing and return.
3434
3535#### Configuring SSH
3636
3737The IDE needs an SSH connection that does not prompt for a password. There are a lot of possibilities to achieve this. This text only describes a few. For security reasons it is strongly recommended that you read the SSH manpage.
3838
3939Solution 1: User based authentication.
4040
4141This will allow one specific user on the local computer to establish an SSH connection to the remote computer as a specific user without prompting for passwords.
4242
4343
4444ToDo: describe the server settings. On redhat this works without any change.
4545
4646Step 1
4747create the public and private keys on the local machine
4848
4949This will create two files on the local machine:
5050
51-` ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub`
51+```text
52+ ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub
53+```
5254
5355If you already have these files, skip this step.
5456
5557``` bash
5658
5759ssh-keygen -t rsa
5860```
5961
6062Keep the default and leave the passphrase empty.
6163
6264Step 2
6365copy the public key of the local machine to the remote machine
6466
6567``` bash
6668
6769scp ~/.ssh/id_rsa.pub user@remotecomp:remote.pub
6870```
6971
7072Step 3
7173create the file ~/.ssh/authorized_keys2 on the remote machine
7274
7375``` bash
7476
7577ssh user@remotecomp
7678touch ~/.ssh/authorized_keys2
7779chmod 600 ~/.ssh/authorized_keys2
7880```
7981
8082The chmod will set the permissions to only allow yourself to read the file. SSH requires this.
8183
8284``` bash
8385
8486cat remote.pub >> ~/.ssh/authorized_keys2
8587rm remote.pub
8688exit
8789```
8890
8991Step 4
9092test
9193
9294You should now be able to login without password.
9395
9496``` bash
9597
9698ssh user@remotecomp
9799```
98100
99101Step 5
100102Setup the ssh debugger in the IDE
101103
102104*ToDo*
103105
104106------------------------------------------------------------------------
105107
106108Step 6
107109Set "Run parameters"
108110
109111For remote debug we need to set the correct value for "Host application", "Command line parameters" and "Working directory". Remember: **It is remote!**
110112
111113![](/wiki-images/1/18/Remote_debug_run_parameters.png)
112114
113115Step 7
114116Set "Debugger parameters"
115117
116118Select "Debugger type and path" to "GNU Debugger trough SSH(ssh)"
117119
118120![](/wiki-images/7/7f/Remote_debug_debuger_parameters.png) *ToDo*
119121
120122## Using gdbserver
121123
122124For now this is the best approach I could find: Steps for remote debugging from Windows to Linux machine, other situations should not be so much different:
123125
1241261- Manually include gdbmidebugger the public "uses" clause of debugmanager.pas file located in lazarus/ide and recompile ide.
125127
1261282- Install gdb and gdbserver on Linux.
127129
1281303- Make a cross compile Lazarus.
129131
1301324- Set IDE options/Debugger to gdbserver and in the debugger options on the bottom set Debugger_Remote_Hostname to remote Linux machine IP or name and set the port to 2159.
131133
1321345- Add a Linux build mode to project option.
133135
1341366- Build project on Linux build mode.
135137
1361387- Use whatever tool like WinSCP to move binary file to Linux machine. for WinSCP use it like this : WinSCP.com /script=script.txt and content of script.txt will be like this (you can use WinSCP 5.8 and later session/ script menu to make and modify it) :
137139
138140``` pascal
139141open sftp://username:remotepassword@remotenameorip/ -hostkey="XXXXXX"
140142
141143cd ./projectdir
142144put -permissions=744 "C:\Project\project1"
143145
144146exit
145147```
146148
1471498- Run gdbserver on remote machine like this : gdbserver host:2159 ./project1
148150
1491519- Set breakpoints where you want.
150152
15115310- Run project in your IDE and test it.
152154
153155For more reading see :
154156
155157- [[http://forum.lazarus.freepascal.org/index.php/topic,32393.0.html Remote debugging from Windows to Linux]]
156158- [[http://forum.lazarus.freepascal.org/index.php/topic,19014.msg107930.html|http://forum.lazarus.freepascal.org/index.php/topic,19014.msg107930.html#msg107930 Initial GdbServer post on forum]]
157159- [[http://forum.lazarus.freepascal.org/index.php/topic,20101.msg115408.html|http://forum.lazarus.freepascal.org/index.php/topic,20101.msg115408.html#msg115408 target async support (ssh and gdbserver)]]
158160- [[https://forum.lazarus.freepascal.org/index.php/topic,66960.msg514304.html|https://forum.lazarus.freepascal.org/index.php/topic,66960.msg514304.html#msg514304 Some of the gdb-server backend settings explained]]
159161
160162## Using gdbserver on cross-platform remote
161163
162164When, say, you want cross-build an application for Raspberry Pi on Windows and then debug it remotely on RasPi, the default **gdb.exe** wouldn't work. You need to obtain **gdb-multiarch** (supports different remote architectures). For Windows, the easiest way is to install [MSYS2 distribution](https://www.msys2.org) and then install the package gdb-multiarch ([w64 gdb-multiarch](https://packages.msys2.org/package/mingw-w64-x86_64-gdb-multiarch)).
163165
164166Then, in Lazarus, in **Debugger backend** options, select "GNU remote debugger (gdbserver)", set "Debugger path" to **gdb-multiarch.exe**, and specify "Architecture" **arm**.
165167
166168## Using avr-gdb
167169
168170I've managed to debug AVR programs using two different methods:
169171
170172Executing code through [simavr](https://github.com/buserror/simavr) (an AVR simulator)
171173
172174simavr includes a gdbserver interface. To simulate and debug code, launch simavr with the -g option:
173175
174-`./runavr -g -m atmega328p -f 16000000 <firmware>`
176+```text
177+./runavr -g -m atmega328p -f 16000000 <firmware>
178+```
175179
176180By default a remote gdb connection is available on port 1234.
177181
178182Running on hardware and debugging over DEBUGwire using [dwire-debug](https://github.com/dcwbrown/dwire-debug)
179183
180184Launch dwire as follows:
181185
182-`./dwdebug device <serial port>, gdbserver, qr`
186+```text
187+./dwdebug device <serial port>, gdbserver, qr
188+```
183189
184190By default a remote gdb connection is available on port 4444. The *qr* option will result in dwire exiting with the target running when the debug connection is killed. Note that currently dwire returns *S00* when hitting a breakpoint. This results in Lazarus displaying a popup dialog with an error message. This is to indicate that a non-standard signal was received and is merely an annoyance. See [this](https://github.com/dcwbrown/dwire-debug/issues/33) for a fix.
185191
186192To configure Lazarus for debugging over avr-gdb
187193
188194- Select *GNU remote debugger* under Debugger options
189195- Enter path to avr-gdb
190196- Enter the appropriate port number
191197- At the moment a Lazarus [patch](https://bugs.freepascal.org/view.php?id=32084) is required because no process ID is available on the embedded platform.
192198
193199I've tested different versions of gdb compiled for AVR target:
194200
195201- gdb 6.6 - Doesn't work because async mode isn't supported.
196202- gdb 7.0.1 - ~~Works~~ Sometimes work, see note below.
197203- gdb 8.0-9.1 - Doesn't work as is, but work with this [patch](https://sourceware.org/bugzilla/show_bug.cgi?id=13519#c13).
198204- gdb 10.1 - This version finally includes the patch to fix the disassemble bug.
199205
200206Notes
201207
202208- After I upgraded Linux Mint and retested gdb 7.0.1 I noticed the following warning while testing a debug session:
203209
204210`warning: (Internal error: pc 0xa2 in read in psymtab, but not in symtab.)` GDB would sometimes not work properly and debugging from Lazarus using this version of gdb could hang the IDE.
205211
206212- Using gdb 10.1 with Lazarus gdbmi debugger gives an error on startup if the AVR RTL has been compiled with debug information. This error does not appear to affect the stability or functionality of gdb with Lazarus (based on preliminary testing).
207213
208214---
209215*From the Free Pascal wiki, page "Remote Debugging", as it stood on 2024-04-13 after a change by Martin, 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).*

Back to history