Remote Debug C Program using CLion

I believed that most C programmers have encountered segmentation fault before. In this case, we have to write a lot of print statements or use the command-line tool gdb to find where is wrong. Either way, it's time-consuming to debug your code only using those basic tools.

This tutorial walks you through how to debug on CLion locally while having your code compile and run on remote CS Lab machines.

Note: If you haven't installed CLion before, you can read my previous tutorial here

1. Configure Remote Toolchain

A toolchain is a set of development tools including compiler, linker, debugger, etc. We want to use set up the remote toolchain for our project so that we can compile and run codes remotely.

1. Open setting and navigate to Build, Execution, Deployment > Toolchains

2. Click add at the bottom

3. Select Remote Host instead of System at the right of Name

4. Click open in the Credentials line

5. Fill in the information for the remote host

Use a specific server for the host (e.g. royal-10.cs.wisc.edu) instead of best-linux.cs.wisc.edu, since we don't want the project to be run on a different machine each time.

Here is a list of Linux server you can use:

  • rockhopper-01.cs.wisc.edu through rockhopper-09.cs.wisc.edu
  • royal-01.cs.wisc.edu through royal-30.cs.wisc.edu
  • snares-01.cs.wisc.edu through anares-10.cs.wisc.edu
  • emperor-01.cs.wisc.edu through emperor-07.cs.wisc.edu

6. If you have configured toolchain before, you need to drag the remote host to the top of the list to set it as the default toolchain.

Note 1: After you clicked OK, It may take some time for CLion to retrieve some information from the remote server. You can check the process by clicking the status information at the bottom of CLion. Please wait for that until you continue.

Note 2: If cmake reports "CMake 3.13 or higher is required. You are running version 3.10.2", you need to change the first line of CMakeLists.txt to "cmake_minimum_required(VERSION 3.10.2)"

Now you should be able to compile and run your code remotely on CS Lab machine by clicking the run button at the top right corner of CLion.

2. Remote Debug on CS Lab Machine

Now you can click on the line number to add a breakpoint and click the debug button to debug your code.

If you are encountering segmentation fault, the debugger will stop at the line where segfault happens.

3. Some Useful Tips

1. Make good use of the following three buttons on the debug tab.

  • Step over: go to the next line
  • Step into: look into the function call
  • Step out: get out of this function

2. You can add new watches to the variable list to check for a specific variable

3. Press F1 on a system function to show the documentation. You can also dock the documentation popup as a permanent tab on the right.

4. References

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.