Skip to content

Commit fb66ea4

Browse files
author
Dimitri Rusin
committed
Stack trace printing code.
1 parent 54654d9 commit fb66ea4

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

include/ioh/common/factory.hpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <algorithm>
44
#include <cassert>
55
#include <cstdlib>
6+
#include <execinfo.h>
67
#include <functional>
8+
#include <iostream>
79
#include <map>
810
#include <memory>
911
#include <optional>
@@ -20,6 +22,7 @@
2022
#include "file.hpp"
2123

2224

25+
2326
namespace ioh::problem
2427
{
2528
//! Defintion interface of problems that are defined in static files
@@ -57,6 +60,21 @@ namespace ioh::problem
5760

5861
namespace ioh::common
5962
{
63+
64+
void printStackTrace() {
65+
const int MAX_CALLSTACK_SIZE = 128;
66+
void *callstack[MAX_CALLSTACK_SIZE];
67+
int stackSize = backtrace(callstack, MAX_CALLSTACK_SIZE);
68+
char **symbols = backtrace_symbols(callstack, stackSize);
69+
70+
std::cerr << "Call Stack:" << std::endl;
71+
for (int i = 0; i < stackSize; i++) {
72+
std::cerr << symbols[i] << std::endl;
73+
}
74+
75+
free(symbols);
76+
}
77+
6078
//! Function to get the next non zero value in an array of integers
6179
inline int get_next_id(const std::vector<int> &ids)
6280
{
@@ -135,7 +153,9 @@ namespace ioh::common
135153
std::cerr << error_message << std::endl;
136154
std::cout << error_message << std::endl;
137155

138-
assert(false);
156+
printStackTrace();
157+
158+
assert(!already_defined && name.c_str());
139159
}
140160

141161
name_map[name] = std::move(creator);

0 commit comments

Comments
 (0)