How to debug a unit test (native)

Hi,

How should I abe able to debug a unit test?

According to this page I should only have to set the debug_test.but it seems to be ignored when i run `pio test -e native’

This is my platform io:

[env:native]
platform = native
test_transport = custom
lib_compat_mode = off
lib_ignore = hal
lib_deps = google/googletest@^1.10.0
debug_test = ringbuffertest.cpp

Should I also issue a different pio command?
Or should I add an argument to pio test -e native to instruct it to attach local debugger?
Or should I manually install a local debugger and add debug_tool = custom ?

Documentation isn’t overwhelming clear on this topic it seems.

Hope somebody can shed a light here

Well if I take the project you’ve posted at PlatformIO stm32: Intellisense for google mock is broken and follow the docs by adding

debug_test = *

the platformio.ini in that case (test file is in the root of test/), and I go to the debugger sidebar and press the debug button, it throws me in a debug session starting in main().

And I can debug as usual, e.g. placing breakpoints in the unit test function and forwarding execution there.

Works no problems.

If I wanted to do it from the CLI, then again per official documentation I start a debugging session in the native environment

C:\Users\Max\temp\platformiogmockintellisense>pio debug -e native --interface=gdb -x .pioinit
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe: warning: Couldn't determine a path for the index cache directory.
Reading symbols from C:\Users\Max\temp\platformiogmockintellisense\.pio\build\native\program.exe...
PlatformIO Unified Debugger -> http://bit.ly/pio-debug
PlatformIO: debug_tool = None
PlatformIO: Initializing remote target...
Temporary breakpoint 1 at 0x140001833: file test\test.cpp, line 9.
PlatformIO: Initialization completed
(gdb) PlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> http://bit.ly/pio-debug
Starting program: C:\Users\Max\temp\platformiogmockintellisense\.pio\build\native\program.exe
[New Thread 11908.0x2368]
[New Thread 11908.0x2f2c]
[New Thread 11908.0x3488]

Thread 1 hit Temporary breakpoint 1, main (argc=1, argv=0x165f3b71990)
    at test\test.cpp:9
9       int main(int argc, char **argv) {
(gdb)

And it fires up the debugger and execution is again in the main() function, ready for further debugging.

(gdb) list
4
5       void bla(void) {
6           TEST_ASSERT_EQUAL(32, 32);
7       }
8
9       int main(int argc, char **argv) {
10          UNITY_BEGIN();
11          RUN_TEST(bla);
12          UNITY_END();
13
(gdb) break 6
Breakpoint 2 at 0x7ff7ad0c17c4: file test\test.cpp, line 6.
(gdb) continue
Continuing.

Thread 1 hit Breakpoint 2, bla () at test\test.cpp:6
6           TEST_ASSERT_EQUAL(32, 32);
(gdb) backtrace
#0  bla () at test\test.cpp:6
#1  0x00007ff7ad0c38e2 in UnityDefaultTestRun (Func=0x7ff7ad0c17c0 <bla()>,
    FuncName=<optimized out>, FuncLineNum=<optimized out>)
    at C:\Users\Max\.platformio\packages\tool-unity\unity.c:1817
#2  0x00007ff7ad0c1861 in main (argc=<optimized out>, argv=<optimized out>)
    at test\test.cpp:11
(gdb) 
1 Like

Hey! Thx a lot for helping out.

Hmmm, that’s weird. Can’t get it to work in my real project. So tried to follow your steps exactly in the “intellisense problem project”, but also without success.

I added the debug_test = *


(test.cpp is read because of intellisense, it compiles fine ofc)

I don’t know which button you press in the “debug sidebar”. I assume this one:

image

When I do that, I immediately get this error

image

When I try to follow your steps in CLI:

  1. pio debug
    (compiles all in debug mode I guess)

  2. pio debug -e native --interface=gdb -x .pioinit

I immediately get this error:

image

Why is it ignoring debug_test?
Why is it complaining about the environment in platformio.ini, if you use the exact same one?

Do I need to install gdb manually? Or is that part of the whole gcc tool chain I installed?

Your core needs to be new enough for this feature. Use the CLI to execute pio upgrade --dev and retry.

OK, done that. Upgraded with pio upgrade --dev

Now I get this exception:

PS D:\dev\bp\platformiogmockintellisense> pio debug -e native --interface=gdb -x .pioinit
Error: Traceback (most recent call last):
  File "c:\users\baprins\.platformio\penv\lib\site-packages\platformio\__main__.py", line 115, in main
    cli()  # pylint: disable=no-value-for-parameter
  File "c:\users\baprins\.platformio\penv\lib\site-packages\click\core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\platformio\commands\__init__.py", line 44, in invoke
    return super(PlatformioCLI, self).invoke(ctx)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\baprins\.platformio\penv\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\click\decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\platformio\commands\debug.py", line 158, in cli     
    loop.run_until_complete(coro)
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\base_events.py", line 642, in run_until_complete       
    return future.result()
  File "c:\users\baprins\.platformio\penv\lib\site-packages\platformio\debug\process\gdb.py", line 58, in run   
    await self.spawn(*args, cwd=self.project_dir, wait_until_exit=True)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\platformio\debug\process\base.py", line 77, in spawn
    await loop.subprocess_exec(
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\base_events.py", line 1661, in subprocess_exec
    transport = await self._make_subprocess_transport(
    transp = _WindowsSubprocessTransport(self, protocol, args, shell,
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\base_subprocess.py", line 36, in __init__
    self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\windows_events.py", line 885, in _start
    self._proc = windows_utils.Popen(
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\windows_utils.py", line 153, in __init__
    super().__init__(args, stdin=stdin_rfd, stdout=stdout_wfd,
  File "C:\Users\baprins\.platformio\python3\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\baprins\.platformio\python3\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

============================================================

An unexpected error occurred. Further steps:

* Verify that you have the latest version of PlatformIO using
  `pip install -U platformio` command

* Try to find answer in FAQ Troubleshooting section
  https://docs.platformio.org/page/faq.html

* Report this problem to the developers
  https://github.com/platformio/platformio-core/issues

============================================================

The first bullet suggests to pip install, done that too, but doesn't change anything.

PS D:\dev\bp\platformiogmockintellisense> pip install -U platformio
Requirement already satisfied: platformio in c:\users\baprins\.platformio\penv\lib\site-packages (5.2.0a6)
Requirement already satisfied: marshmallow<4,>=2 in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (3.11.1)
Requirement already satisfied: bottle==0.12.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.12.19)
Requirement already satisfied: starlette==0.14.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.14.2)
Requirement already satisfied: requests==2.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (2.25.1)
Requirement already satisfied: pyelftools<1,>=0.27 in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.27)
Requirement already satisfied: colorama in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.4.4)
Requirement already satisfied: aiofiles==0.6.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.6.0)
Requirement already satisfied: uvicorn==0.13.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.13.4)
Requirement already satisfied: wsproto==1.0.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (1.0.0)
Requirement already satisfied: tabulate==0.8.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.8.9)
Requirement already satisfied: ajsonrpc==1.1.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (1.1.0)
Requirement already satisfied: zeroconf==0.29.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (0.29.0)
Requirement already satisfied: semantic-version==2.8.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (2.8.5)
Requirement already satisfied: pyserial==3.* in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (3.5)
Requirement already satisfied: click!=7.1,!=7.1.1,<8,>=5 in c:\users\baprins\.platformio\penv\lib\site-packages (from platformio) (7.1.2)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\baprins\.platformio\penv\lib\site-packages (from requests==2.*->platformio) (1.26.4)
Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\baprins\.platformio\penv\lib\site-packages (from requests==2.*->platformio) (4.0.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\baprins\.platformio\penv\lib\site-packages (from requests==2.*->platformio) (2020.12.5)
Requirement already satisfied: idna<3,>=2.5 in c:\users\baprins\.platformio\penv\lib\site-packages (from requests==2.*->platformio) (2.10)
Requirement already satisfied: h11>=0.8 in c:\users\baprins\.platformio\penv\lib\site-packages (from uvicorn==0.13.*->platformio) (0.12.0)
Requirement already satisfied: ifaddr>=0.1.7 in c:\users\baprins\.platformio\penv\lib\site-packages (from zeroconf==0.29.*->platformio) (0.1.7)
PS D:\dev\bp\platformiogmockintellisense> 

Hope you still have time to try to help me further.

… Oh I could have read myself I guess…

File "c:\users\baprins\.platformio\penv\lib\site-packages\platformio\debug\process\gdb.py", line 58, in run   
    await self.spawn(*args, cwd=self.project_dir, wait_until_exit=True)
  File "c:\users\baprins\.platformio\penv\lib\site-packages\platformio\debug\process\base.py", line 77, in spawn
    await loop.subprocess_exec(
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\base_events.py", line 1661, in subprocess_exec
    transport = await self._make_subprocess_transport(
    transp = _WindowsSubprocessTransport(self, protocol, args, shell,
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\base_subprocess.py", line 36, in __init__
    self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\windows_events.py", line 885, in _start
    self._proc = windows_utils.Popen(
  File "C:\Users\baprins\.platformio\python3\lib\asyncio\windows_utils.py", line 153, in __init__
    super().__init__(args, stdin=stdin_rfd, stdout=stdout_wfd,
  File "C:\Users\baprins\.platformio\python3\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\baprins\.platformio\python3\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

So I am missing gdb I guess? I’ll start looking how to get that installed in parallel

Open a normal commandline prompt and execute gdb --version. What does it return?

I didn’t have to, I checked in the folder, I don’t have gdb installed. I followed some tutorial to install gcc using MinGW. Either I missed it, or they didn’t suggest to install gdb. Anyway, installed gdb, restart VScode:

image

Hurray!!!

Thx so much for your help (and time!).

Also for the other question, now I am starting to have a nice environment :slight_smile:

Ahh right the MinGW GCC package from the msys2 installer or something, yeah GDB is in separate package, that’s why I like that other installer :smiley: