Skip to content

Commit

Permalink
added separate program to test stack trace [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Oct 27, 2023
1 parent 524a5e8 commit 8a92f2c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI-unixish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ jobs:
- name: Test Signalhandler
run: |
cmake -S . -B cmake.output.signal -G "Unix Makefiles" -DBUILD_TESTS=On
cmake --build cmake.output.signal --target test-signalhandler -- -j$(nproc)
cmake --build cmake.output.signal --target test-signalhandler test-stacktrace -- -j$(nproc)
cp cmake.output.signal/bin/test-s* .
python3 -m pytest -Werror --strict-markers -vv test/signal/test-*.py
Expand Down
12 changes: 11 additions & 1 deletion test/signal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ target_include_directories(test-signalhandler PRIVATE ${PROJECT_SOURCE_DIR}/cli
target_compile_options_safe(test-signalhandler -Wno-missing-declarations)
target_compile_options_safe(test-signalhandler -Wno-missing-prototypes)
# required for backtrace() to produce function names
target_compile_options(test-signalhandler PRIVATE -rdynamic)
target_compile_options(test-signalhandler PRIVATE -rdynamic)

add_executable(test-stacktrace
test-stacktrace.cpp
${PROJECT_SOURCE_DIR}/cli/stacktrace.cpp)
target_include_directories(test-stacktrace PRIVATE ${PROJECT_SOURCE_DIR}/cli ${PROJECT_SOURCE_DIR}/lib)
# names for static functions are omitted from trace
target_compile_options_safe(test-stacktrace -Wno-missing-declarations)
target_compile_options_safe(test-stacktrace -Wno-missing-prototypes)
# required for backtrace() to produce function names
target_link_options(test-stacktrace PRIVATE -rdynamic)
43 changes: 43 additions & 0 deletions test/signal/test-stacktrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "stacktrace.h"

// static functions are omitted from trace

void my_func_2()
{
print_stacktrace(stdout, 0, true, -1);
}

void my_func()
{
my_func_2();
}

int main(int argc, const char * const argv[])
{
(void)argc;
(void)argv;
//if (argc != 2)
// return 1;

my_func();

return 0;
}
38 changes: 38 additions & 0 deletions test/signal/test-stacktrace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import subprocess
import os
import sys

def _lookup_cppcheck_exe(exe_name):
# path the script is located in
script_path = os.path.dirname(os.path.realpath(__file__))

if sys.platform == "win32":
exe_name += ".exe"

for base in (script_path + '/../../', './'):
for path in ('', 'bin/', 'bin/debug/'):
exe_path = base + path + exe_name
if os.path.isfile(exe_path):
print("using '{}'".format(exe_path))
return exe_path

return None

def _call_process():
exe = _lookup_cppcheck_exe('test-stacktrace')
if exe is None:
raise Exception('executable not found')
p = subprocess.Popen([exe], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
comm = p.communicate()
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
return p.returncode, stdout, stderr


def test_stack():
exitcode, stdout, stderr = _call_process()
assert stderr == ''
lines = stdout.splitlines()
assert lines[0] == 'Callstack:'
assert lines[1].endswith('my_func_2()')
assert lines[2].endswith('my_func()')

0 comments on commit 8a92f2c

Please sign in to comment.