Long Story (This is X)
I need to start CLion with
sudoso that I can attachgdbto the running process from CLion (for a debugging purpose). The reason is because when I run CLion withoutsudo, 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 runninggdbin the terminal manually withoutsudo:
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 rungdbwithsudo:
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
~/.bashrcfile:source /opt/ros/kinetic/setup.bashwhose 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
sudoperforms anenv_resetby default - only preserving a minimal environment (2)shis notbash(at least, not by default) and in any case,.bashrcis 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 03 '19 at 12:57ptrace operation not permittedwhen attachinggdbto the process problem. And if that solved the problem, I wouldn't need to ask the Y, which is about dealing withsudo, environment variables,sh,~/.bashrc, etc. – IgNite Dec 03 '19 at 14:50~/.bashrcfile and setCMAKE_PREFIX_PATHin CLion accordingly, by pointing this variable to the folder that contains the missing packages. Also set the variables as outlined by this post. However,~/.bashrcis something that I change frequently, so I'm not sure if this is the best way to go. – IgNite Dec 03 '19 at 15:05sudo- in which case, it will inherit its parent environment (including things likeCMAKE_PREFIX_PATH, so long as they have been exported) in the usual way – steeldriver Dec 03 '19 at 17:34/etc/sysctl.d/10-ptrace.confas in the link that you posted does solve the problem indirectly. By modifying this file, I can now 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 Jan 09 '20 at 15:26