Eli
Eli

Reputation: 693

Step through C++ code with CLion while PyCharm plugin is enabled

I am having a problem in CLion when the PyCharm plugin is enabled. This is what I do:

  1. Run a Python program from the shell. This program creates multiple processes (like workers) in which the Python code calls C++ code that I want to debug.
  2. From CLion, attach to the original main Python process.
  3. Step through code after C++ breakpoint gets hit in one or more of the created processes.

Usually, this works fine. But if I have the PyCharm plugin enabled, CLion seems to treat the main process differently, and does not hit any of my C++ breakpoints.

Does anyone know how I can get the C++ breakpoints to work, even when the PyCharm plugin is enabled?

Upvotes: 2

Views: 930

Answers (1)

Patrick Roncagliolo
Patrick Roncagliolo

Reputation: 336

I just found a solution that suits my needs, and maybe yours.

This has been tested on Clion 2019.3 with Ubuntu 18.04, Python 3 and GDB. I have a Python process that spawns a C++ process, and I want to debug both. The condition is to know the name or PID of the child, and to have time to manually attach to the child process (so like a "wait user key" in the master process, or a breakpoint somewhere after the fork).

According to this guide, on Ubuntu you either need to temporary or permanently allow attaching to a foreign, local process.

To disable this restriction temporarily, enter the command:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

To disable this restriction permanently, open the file /etc/sysctl.d/10-ptrace.conf for editing and change the line kernel.yama.ptrace_scope = 1 to kernel.yama.ptrace_scope = 0. To apply the changes, enter sudo service procps restart or restart your system, at your choice.

Then:

  • Create a run configuration for your Python script
  • Put breakpoints where needed
  • Run the Python script in debug mode
  • Wait for it to fork and break on wait condition/breakpoint
  • Run->Attach to process...
  • type PID or name of C++ child
  • voilà, you have control on both master and child processes

Unfortunately, I don't know how to automate it, but that works fine in my project because the two processes exchange messages so when I block one, the other is waiting for data and I have time to manually attach to it.

As a side note, this is probably an answer also for this question on StackOverflow.

Upvotes: 1

Related Questions