Skip to content

Instantly share code, notes, and snippets.

@rs333
Last active September 10, 2022 21:30
Show Gist options
  • Select an option

  • Save rs333/6a415aee56a1d26ee8511b8e2beefcd9 to your computer and use it in GitHub Desktop.

Select an option

Save rs333/6a415aee56a1d26ee8511b8e2beefcd9 to your computer and use it in GitHub Desktop.

Remote debugging with VSCode

  • Note: If a step has already been done, it is not necessary to repeat it.

Setting up your remote ssh login.

  1. Open a terminal
  2. Generate your ssh public/private keypair using ssh-keygen. The program will prompt you for a passphrase. Leaving it blank and pressing enter will simplify the process. Note that it is important you do not share your private key.
$ ssh-keygen -t ed25519 -C "[email protected]"
  1. Copy it to the remote server using ssh-copy-id where <public_key_filename>.pub is the filename of the public key.
ssh-copy-id -i ~/.ssh/<public_key_filename>.pub <your_username>@<remote_hostname>
  1. Test that the setup by logging in via ssh to the remote server. It should not require a password.
ssh <your_username>@<remote_hostname>

Setting up VSCode

  1. Start VSCode

  2. Install the following VSCode extensions.

  • C/C++ Extension by Microsoft
  • Remote-SSH Extension by Microsoft
  1. Click the Remote Explorer on left hand tool bar.

  2. Add a new remote target by clicking on the plus sign.

  3. When given the option to Enter SSH Connection Command, type in the appropriate command and press Enter. ssh <your_username>@<remote_hostname>

  4. When asked to Select SSH configuration file to update, select the config file for your user account. Note that /home may be a much longer path. /home/<your_local_username>/.ssh/config

  5. In a local terminal, verify the config file permissions are set to only be readable by you. The file permissions for the file should be 600 or -rw------- ls -la /home/<your_local_username>/.ssh/config

  6. If the file permissions are not 600 or -rw-------, then you can change them using the following command. chmod 600 /home/<your_local_username>/.ssh/config

  7. The <remote_hostname> will appear in the SSH TARGETS list. Click the small icon at the right of the hostname which looks like a folder with a plus sign on top of it.

  8. A new VSCode window will open. Confirm it is connected to the remote host by opening a terminal in VSCode using Ctrl+`. You should see a terminal open where the prompt matches that for your <remote_host>.

Example Program

  1. Open VSCode

  2. Connect to the <remote_hostname> server via the remote explorer.

  3. Create a folder to store your program. mkdir cremote

  4. Click File->Open Folder

  5. Select the folder you created and click Ok.

  6. If prompted with dialogs asking about trust, click the trust buttons. (Assuming you trust yourself and the code you will put in the directories. :)

  7. Next to the CREMOTE folder in the VSCode Explorer, Click the New File button. (Note the button may not appear until you hover over the entry for the CREMOTE folder.)

  8. Type in the name of your file. cremote.c

  9. In the newly created file enter the following simple C code and save the file.

#include <stdio.h>

int main(int argc, char** argv){
    printf("Hello Remote World!\n");
    return 0;
}
  1. Immediately to the left of line 6 set a break point by clicking on the dark red dot that appears as you move your cursor to the left of the line number. When set, it should be bright red and stay even after you move your cursor.

  2. Click the Debug C/C++ File button.

  3. Select the appropriate configuration from the drop down menu. Probably some variation of gcc. If there is more than one, make sure it matches the architecture of the <remote_hostname> machine.

  4. Note the output in the TERMINAL and that the program counter has stopped at the line with the breakpoint you set.

  5. Enjoy remote coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment