C++ QNX cross-compilation and static analysis with pvs-studio

Karina
2 min readJan 13, 2021

--

Assume we have cross-platform code including QNX distributive. We build code via cross compilation in docker containers based on Ubuntu distributive so static analysis tool can’t simply find compiler’s include directories and platform specific defines. Based on pvs-studio I will show how it can be used with cross compilation process for QNX RTOS.

pvs-studio

pvs-studio is a proprietary static analysis tool with cross-platform code support. It seems to me that there is a little mess in their documentation so it’s not obvious how to use pvs-studio for QNX cross-compilation for example.

For making static analysis a tool need compilation information, e.g. each translation unit path, include directories, defines etc. The first way is generating compilation base compile_commands.json. Cmake can do this with -DCMAKE_EXPORT_COMPILE_COMMANDS=On defined so you’ll get sth like this

{
"directory": "/home/build/.conan/data/test_project/src",
"command": "/usr/toolchain/qnx700/host/linux/x86_64/usr/bin/ntox86_64-g++ -DBOOST_STACKTRACE_USE_BACKTRACE -DBUILD_ID=\\\"None\\\" -DPRODUCT_VERSION=\\\"1.0.0\\\" -D_GLIBCXX_USE_CXX11_ABI=1 -I/home/build/.conan/data/test_project/include -D_QNX_SOURCE -D_XOPEN_SOURCE=700 -m64 -std=c++14 -O3 -DNDEBUG -std=gnu++14 -o CMakeFiles/test_project.dir/main.cpp.o -c /home/build/.conan/data/test_project/src/main.cpp",
"file": "/home/build/.conan/data/test_project/src/main.cpp"
},

You may notice that in this example QNX compiler is gcc ntox86_64-g++ and not vanilla qcc. That’s because pvs-studio for Linux doesn’t support qcc preprocessor. This is Windows-only feature. And for Linux we have gcc or clang.

As you can see compile_commands.json above lacks QNX system include directories and also some defines as for example __X86_64__ , __QNXNTO__ or __BIGENDIAN__. They are typically defined by compiler itself. That’s why compile_commands.json way doesn’t suit us and we need another pvs-studio option — strace usage.

strace is a Linux utility that allows syscall tracking. pvs-studio launches strace during build process and gets all compiler arguments. This is the main difference from compile_commands.json way — it can be generated with one cmake call and strace way needs one more pvs-studio-analyzer command. So do the following:

pvs-studio-analyzer trace -- make

or if you use conan, for example

pvs-studio-analyzer trace -- conan create . build-agent/stable

This call will generate strace_out file that will be used during analysis stage. Please be sure that you don’t have compile_commands.json file in the same directory because it has higher priority by default or you can specify a strace file with -f option. The full options list you can get with

pvs-studio-analyzer analyze ?

For analyzing your code you also need to define your cross-compiler:

pvs-studio-analyzer analyze -e /usr/ -C ntox86_64-g++

After this step you’ll get default PVS-Studio.log that you can convert to the convenient format, e.g. html:

plog-converter -a GA:1,2 -t html -o pvs_out.html PVS-Studio.log

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Karina
Karina

Written by Karina

I’m not a programmer, I’m just pretending https://github.com/keyrnk

No responses yet

Write a response