3

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
  • I'll need more coffee before I really understand what you're asking but a couple of observations: (1) sudo performs an env_reset by default - only preserving a minimal environment (2) sh is not bash (at least, not by default) and in any case, .bashrc is only sourced for interactive shells. Fundamentally, I think this likely is an XY problem - see for example How to solve “ptrace operation not permitted” when trying to attach GDB to a process? – steeldriver Dec 3 '19 at 12:57
  • @steeldriver Thanks for pointing those out to me. I agree that this is likely an XY problem; because I could go with fixing the X, which is ptrace operation not permitted when attaching gdb to the process problem. And if that solved the problem, I wouldn't need to ask the Y, which is about dealing with sudo, environment variables, sh, ~/.bashrc, etc. – IgNite Dec 3 '19 at 14:50
  • There are similar questions: here, here, and here, but none of them directly answer the question (the Y) that I asked here. – IgNite Dec 3 '19 at 14:53
  • The only workaround that works for me is to look into the ~/.bashrc file and set CMAKE_PREFIX_PATH in CLion accordingly, by pointing this variable to the folder that contains the missing packages. Also set the variables as outlined by this post. However, ~/.bashrc is something that I change frequently, so I'm not sure if this is the best way to go. – IgNite Dec 3 '19 at 15:05
  • I guess what I'm saying is that you should fix your kernel parameters so that you don't need to run the IDE with sudo - in which case, it will inherit its parent environment (including things like CMAKE_PREFIX_PATH, so long as they have been exported) in the usual way – steeldriver Dec 3 '19 at 17:34
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.