# Remote Debbuging \[NEW]

## What is remote debbuging and why is it useful?

Remote Debugging allows to debug the program running on another system as long we have access to the source code and debugger port is present on the system running the application. We can describe this process in a few steps:

1. Configure application to debug.
2. Load the code into Visual Studio Code.
3. Configure Visual Studio Code to connect to the remote debugger.

## Step by step

We can attach our debugger to remote program in a few simple steps:

### Check if remote debbuging port is open

Remember that application has to be run in the debugging mode in order to connect our debbuger.

{% tabs %}
{% tab title="Java" %}
Debug mode in Java applications (LOCAL):

```bash
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=[PORT] -jar ~/[PROGRAM].jar
```

Fully Remote with wildcard:

```bash
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:[PORT] -jar [PROGRAM].jar
```

{% endtab %}

{% tab title="Python" %}
**Reconfigure bench (tool for Frappe applications)**

1. Install ptvsd

```bash
/home/frappe/frappe-bench/env/bin/pip install ptvsd
```

2. Edit Procfile file and comment out starting web server

```bash
nano /home/frappe/frappe-bench/Procfile
```

Comment the line below:

```python
#web: bench serve --port 8000
```

3. Reconfigure the application and use ptvsd to open up a debugging port by editing app.py file

```bash
nano /home/frappe/frappe-bench/apps/frappe/frappe/app.py
```

Add the following code to app.py contents (which adds debugging server on *port* *5678*):

```python
import ptvsd
ptvsd.enable_attach(redirect_output=True)
print("Now you can connect IDE debugger.")
ptvsd.wait_for_attach()
```

When ***bench serve*** command from Procfile is executed the bench runs app.py file.

4. Run bench serve:

```bash
cd /home/frappe/frappe-bench/
```

```bash
bench start
```

5. Start web server (in another SSH session / different screen in TMUX)

Move to application folder:

```bash
cd /home/frappe/frappe-bench/sites
```

Run server:

```bash
../env/bin/python ../apps/frappe/frappe/utils/bench_helper.py frappe serve --port 8000 --noreload --nothreading
```

{% endtab %}
{% endtabs %}

### Download the code related to the program

You have to get the source code of the application that you want to debug. It does not need to be ready to run as long as we only want to remote debug the program.

**SSH Way:**

```bash
rsync -azP [USER]@[MACHINE_IP]:/home/frappe/frappe-bench ./
```

**Windows Way:**

Save decompiled sources to zip on the Desktop and host fileserver.

### Create launch.json

In the Debbuger part in VSCode the option for creating the configuration file will show up.

<figure><img src="https://1482599896-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnRXyYnODTImJmzJG5aHD%2Fuploads%2FW8d9bx9u5nSIJkGiU8Fp%2Fimage.png?alt=media&#x26;token=e4b44760-ae9d-4f71-9cd8-363c66411f72" alt=""><figcaption></figcaption></figure>

After creating it, inside we should add "\[Programming language] Attach to Remote Program option"

<figure><img src="https://1482599896-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnRXyYnODTImJmzJG5aHD%2Fuploads%2FB9nWRYJgdXDXR8o6uTZI%2Fimage.png?alt=media&#x26;token=1e06c8be-26e0-453c-a097-7b3129b10ee5" alt=""><figcaption></figcaption></figure>

In the body of the template we have to specify hostname and port on which debbuging port is turned on.

<details>

<summary>Python Version</summary>

```json
{
    "version": "0.2.0",
    "configurations": [
    
        {
            "name": "Python Debugger: Remote Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "[REMOTE_HOST]",
                "port": [REMOTE_PORT]
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/home/frappe/frappe-bench" <- PATH TO WEB
                }
            ]
        }
    ]
}
```

</details>

### Select Attach to Remote Program option

In the Run and Debug panel you have to check the following option:

<figure><img src="https://1482599896-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnRXyYnODTImJmzJG5aHD%2Fuploads%2Fcx7yElxmPuZVs3WM0dzw%2Fimage.png?alt=media&#x26;token=5a03b46f-a0a6-45a4-a694-aa05dc38000b" alt="" width="375"><figcaption></figcaption></figure>

### Run the debugger

Right now the debugger should work and the selected breakpoints should trigger when interacting with the application.
