Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pugs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
code
pugs
Commits
aa2ee664
Commit
aa2ee664
authored
7 years ago
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Backtrace working
parent
42ca21a6
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils/SignalManager.cpp
+62
-40
62 additions, 40 deletions
utils/SignalManager.cpp
with
62 additions
and
40 deletions
utils/SignalManager.cpp
+
62
−
40
View file @
aa2ee664
#include
<SignalManager.hpp>
#include
<ConsoleManager.hpp>
#include
<Kokkos_Core.hpp>
#include
<csignal>
#include
<rang.hpp>
#include
<iomanip>
...
...
@@ -9,39 +11,57 @@
#include
<regex>
#include
<vector>
void
print_bt
()
{
const
int
size
=
20
;
std
::
vector
<
std
::
string
>
getBacktrace
()
{
const
int
size
=
100
;
void
*
buffer
[
size
];
int
ret
=
backtrace
(
buffer
,
size
);
int
ret
=
::
backtrace
(
buffer
,
size
);
char
**
ptr
=
backtrace_symbols
(
buffer
,
ret
);
std
::
vector
<
std
::
string
>
lines
;
for
(
int
i
=
0
;
i
<
ret
;
++
i
)
{
lines
.
push_back
(
ptr
[
i
]);
std
::
vector
<
std
::
string
>
backtrace
;
for
(
int
i
=
2
;
i
<
ret
;
++
i
)
{
backtrace
.
push_back
(
ptr
[
i
]);
}
free
(
ptr
);
// abi::__cxa_demangle( "_Z16displayBacktracev", NULL, NULL, &status );
std
::
regex
mangled_function
(
R"(\(.*\+)"
);
for
(
const
auto
&
line
:
lines
)
{
std
::
cerr
<<
"demangling "
<<
line
<<
'\n'
;
return
backtrace
;
}
void
print_bt
()
{
const
std
::
vector
<
std
::
string
>
lines
=
getBacktrace
();
const
std
::
regex
mangled_function
(
R"%(\(.*\+)%"
);
const
int
width
=
std
::
log10
(
lines
.
size
())
+
1
;
for
(
size_t
i_line
=
0
;
i_line
<
lines
.
size
();
++
i_line
)
{
const
auto
&
line
=
lines
[
i_line
];
std
::
cerr
<<
rang
::
fg
::
green
<<
"["
<<
std
::
setw
(
width
)
<<
i_line
+
1
<<
'/'
<<
lines
.
size
()
<<
"] "
<<
rang
::
fg
::
reset
;
std
::
smatch
matchex
;
int
status
=
-
1
;
if
(
std
::
regex_search
(
line
,
matchex
,
mangled_function
))
{
std
::
string
prefix
=
matchex
.
prefix
().
str
();
std
::
string
function
=
line
.
substr
(
matchex
.
position
()
+
1
,
matchex
.
length
()
-
2
);
std
::
string
suffix
=
matchex
.
suffix
().
str
();
std
::
cerr
<<
"Found "
<<
function
.
c_str
()
<<
'\n'
;
std
::
string
demangled
=
abi
::
__cxa_demangle
(
function
.
c_str
(),
NULL
,
NULL
,
&
status
);
std
::
cerr
<<
"status="
<<
status
<<
'\n'
;
std
::
cerr
<<
demangled
<<
'\n'
<<
'\n'
;
std
::
cerr
<<
prefix
<<
'('
;
if
(
function
.
size
()
>
0
)
{
char
*
demangled
=
abi
::
__cxa_demangle
(
function
.
c_str
(),
NULL
,
NULL
,
&
status
);
if
(
status
==
0
)
{
std
::
cerr
<<
rang
::
fgB
::
yellow
<<
demangled
<<
rang
::
fg
::
reset
;
free
(
demangled
);
}
else
{
std
::
cerr
<<
rang
::
fgB
::
yellow
<<
function
<<
rang
::
fg
::
reset
;
}
}
std
::
cerr
<<
'+'
<<
suffix
<<
'\n'
;
}
else
{
std
::
cerr
<<
line
<<
'\n'
;
}
}
}
#warning must create a specific class for pausing and use enum class
#include
<string>
...
...
@@ -54,12 +74,12 @@ void SignalManager::setPauseForDebug(const std::string& pause_on_error)
std
::
string
SignalManager
::
signalName
(
int
s
)
{
switch
(
s
)
{
case
SIGTERM
:
return
"SIGTERM"
;
case
SIGSEGV
:
return
"SIGSEGV"
;
case
SIGINT
:
return
"SIGINT"
;
case
SIGILL
:
return
"SIGILL"
;
case
SIGABRT
:
return
"SIGABRT"
;
case
SIGFPE
:
return
"SIGFPE"
;
case
SIGABRT
:
return
"SIGABRT"
;
case
SIGINT
:
return
"SIGINT"
;
case
SIGSEGV
:
return
"SIGSEGV"
;
case
SIGTERM
:
return
"SIGTERM"
;
}
return
"SIGNAL undefined!"
;
}
...
...
@@ -67,35 +87,42 @@ std::string SignalManager::signalName(int s)
void
SignalManager
::
pauseForDebug
()
{
std
::
c
err
<<
"
\n
======================================
\n
"
;
std
::
c
err
<<
rang
::
style
::
reset
std
::
c
out
<<
"
\n
======================================
"
<<
std
::
endl
;
std
::
c
out
<<
rang
::
style
::
reset
<<
rang
::
fg
::
reset
<<
rang
::
style
::
bold
;
std
::
c
err
<<
"to attach gdb to this process run
\n
"
;
std
::
c
err
<<
"
\t
gdb -pid "
std
::
c
out
<<
"to attach gdb to this process run
"
<<
std
::
endl
;
std
::
c
out
<<
"
\t
gdb -pid "
<<
rang
::
fg
::
red
<<
getpid
()
<<
rang
::
fg
::
reset
<<
'\n'
;
std
::
cerr
<<
"else press Control-C to exit
\n
"
;
std
::
cerr
<<
rang
::
style
::
reset
;
std
::
cerr
<<
"======================================
\n
"
;
std
::
cout
<<
"else press Control-C to exit"
<<
std
::
endl
;
std
::
cout
<<
rang
::
style
::
reset
;
std
::
cout
<<
"======================================"
<<
std
::
endl
;
std
::
cout
<<
std
::
flush
;
::
sigset_t
sig
;
::
sigaddset
(
&
sig
,
SIGSTOP
);
::
sigsuspend
(
&
sig
);
}
//#include <libunwind.h>
std
::
exit
(
1
);
}
void
SignalManager
::
handler
(
int
s
)
{
std
::
signal
(
SIGFPE
,
SIG_DFL
);
std
::
signal
(
SIGSEGV
,
SIG_IGN
);
std
::
signal
(
SIGTERM
,
SIG_DFL
);
std
::
signal
(
SIGINT
,
SIG_DFL
);
std
::
signal
(
SIGABRT
,
SIG_DFL
);
std
::
c
err
<<
"
\n
*** "
std
::
c
out
<<
"
\n
*** "
<<
rang
::
style
::
reset
<<
rang
::
fg
::
reset
<<
rang
::
style
::
bold
;
std
::
c
err
<<
"Signal "
std
::
c
out
<<
"Signal "
<<
rang
::
fgB
::
yellow
<<
signalName
(
s
)
<<
rang
::
fg
::
reset
...
...
@@ -103,21 +130,14 @@ void SignalManager::handler(int s)
<<
rang
::
style
::
reset
<<
" ***
\n
"
;
std
::
signal
(
SIGFPE
,
SIG_DFL
);
std
::
signal
(
SIGSEGV
,
SIG_DFL
);
std
::
signal
(
SIGTERM
,
SIG_DFL
);
std
::
signal
(
SIGINT
,
SIG_DFL
);
std
::
signal
(
SIGABRT
,
SIG_DFL
);
print_bt
();
SignalManager
::
pauseForDebug
();
if
((
ConsoleManager
::
isTerminal
(
std
::
cout
)
and
(
s_pause_on_error
==
"auto"
))
or
(
s_pause_on_error
==
"yes"
))
{
SignalManager
::
pauseForDebug
();
}
else
{
//
std::exit(
0
);
std
::
exit
(
1
);
}
}
...
...
@@ -130,6 +150,8 @@ void SignalManager::init(const bool& enable)
std
::
signal
(
SIGTERM
,
SignalManager
::
handler
);
std
::
signal
(
SIGINT
,
SignalManager
::
handler
);
std
::
signal
(
SIGABRT
,
SignalManager
::
handler
);
std
::
signal
(
SIGPIPE
,
SignalManager
::
handler
);
std
::
cout
<<
"Signal management: "
<<
rang
::
style
::
bold
<<
rang
::
fgB
::
green
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment