4

Long Story (This is X)

I need to start CLion with sudo so that I can attach gdb to the running process from CLion (for a debugging purpose). The reason is because when I run CLion without sudo, and trying to attach to a process (CLion GUI), I receive:

com.jetbrains.cidr.execution.debugger.backend.gdb.GDBDriver$GDBCommandException: ptrace: Operation not permitted.

As a second check, I tried running gdb in the terminal manually without sudo:

gdb -p 16741

...

Could not attach to process. If your uid matches the uid of the target process, check the setting of "/proc/sys/kernel/yama/ptrace_scope", or try again as the root user. For more details, see "/etc/sysctl.d/10-ptrace.conf"

ptrace: Operation not permitted.

However, if I run gdb with sudo:

sudo gdb -p 16714

...

Attaching to process 16714

So I think I should run CLion as root.


TLDR / The Problem (This is Y)

Now, if running sh /opt/clion/bin/clion.sh from the Ubuntu terminal, CLion does pick up the environment variables sourced in the ~/.bashrc file, and my program compiles with no error.

But because without sudo, I can't attach gdb to the process from within CLion for my debugging purpose, so I need to run the clion.sh startup script as root.

The problem is that when running sudo sh /opt/clion/bin/clion.sh, CLion doesn't seem to pick up the environment variables, leading to "CMake cannot find package ..." error, which makes my program not runnable⁠—worse.

CMake Error at CMakeLists.txt:64 (message):
  find_package(catkin) failed.  catkin was neither found in the workspace nor
  in the CMAKE_PREFIX_PATH.  One reason may be that no ROS setup.sh was
  sourced before.

Question

How do I run the CLion startup script with sudo, and preserve the environment variables that are sourced in ~/.bashrc?


If relevant

  • I think CLion doesn't pick up the following variables. To be very specific, I have this line in my ~/.bashrc file:

    source /opt/ros/kinetic/setup.bash
    

    whose content is

    #!/usr/bin/env bash
    # generated from catkin/cmake/templates/setup.bash.in
    
    CATKIN_SHELL=bash
    
    # source setup.sh from same directory as this file
    _CATKIN_SETUP_DIR=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > 
    /dev/null && pwd)
    . "$_CATKIN_SETUP_DIR/setup.sh"
    

    Perhaps, there might be a way to add this directly to the startup script of CLion?

  • XY Problem
  • I am running Ubuntu 16.04
IgNite
  • 231

3 Answers3

3

Source your variables in .profile not .bashrc.

2

I'm using Ubuntu 22.04 and got the same problem. My solution is to create another script clion-with-env.sh that setup all the environment variables

#!/bin/bash
source setup-vars.sh
source setup-vars2.sh
export MYVAR=...
...
sh /foo/bar/clion-*/bin/clion.sh

Then modify the application file e.g. /usr/share/applications/jetbrains-clion.desktop, change the Exec option to Exec="/path/to/clion-with-env.sh". This is the file that creates the CLion icon in your launcher. You might need to wait a few seconds or logout to update the settings.

If you want to print something in clion-with-env.sh to debug, you can change the Terminal option to true. Then a terminal will show before CLion starts.

I first tried to use source /home/user/.bashrc, but somehow it didn't work.

Addis
  • 21
1

I came back to accept @steeldriver 's comment as an answer.

Modifying /etc/sysctl.d/10-ptrace.conf as in the link in his comment does solve the problem indirectly.

That is, by modifying this file (changing from 1 to 0), I can now run GDB attach to a process within CLion without having to run CLion as root. And as usual, running CLion from terminal does pick up the environment variables.

IgNite
  • 231