2016-02-26 13:36:01 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright:
|
|
|
|
* 2011-2016, Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "debug.hpp"
|
2016-02-15 17:21:01 +11:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "log.hpp"
|
|
|
|
#include "platform.hpp"
|
2016-02-15 17:21:01 +11:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ptrace.h>
|
2016-03-21 14:19:08 +11:00
|
|
|
#include <sys/wait.h>
|
2016-02-15 17:21:01 +11:00
|
|
|
#include <signal.h>
|
|
|
|
|
2016-05-12 18:19:24 +10:00
|
|
|
|
|
|
|
#if defined(PLATFORM_FREEBSD)
|
|
|
|
#define PTRACE_ATTACH PT_ATTACH
|
|
|
|
#define PTRACE_DETACH PT_DETACH
|
|
|
|
#endif
|
|
|
|
|
2016-02-15 17:21:01 +11:00
|
|
|
|
2016-03-21 14:19:08 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Will return true if it is known we are under a debugger. This isn't
|
|
|
|
// designed to exhaustively check for debuggers, only to offer conveniences.
|
|
|
|
// Specifically: errors will return false.
|
|
|
|
static
|
|
|
|
bool
|
|
|
|
is_debugged (void)
|
|
|
|
{
|
|
|
|
auto pid = fork ();
|
|
|
|
if (pid == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We are the child
|
|
|
|
if (pid == 0) {
|
|
|
|
auto ppid = getppid ();
|
|
|
|
int res;
|
|
|
|
|
|
|
|
// attempt to trace our parent. this will fail if we're being debugged.
|
2016-05-12 18:19:24 +10:00
|
|
|
if (ptrace (PTRACE_ATTACH, ppid, nullptr, 0) == 0) {
|
2016-03-21 14:19:08 +11:00
|
|
|
waitpid (ppid, nullptr, 0);
|
2016-05-12 18:19:24 +10:00
|
|
|
ptrace (PTRACE_DETACH, ppid, nullptr, 0);
|
2016-03-21 14:19:08 +11:00
|
|
|
|
|
|
|
res = 0;
|
|
|
|
} else {
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_exit (res);
|
|
|
|
// We are the parent. Wait for our child to tell us the result.
|
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
waitpid (pid, &status, 0);
|
|
|
|
return WEXITSTATUS (status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-15 17:21:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
breakpoint (void)
|
|
|
|
{
|
2016-03-21 14:19:08 +11:00
|
|
|
raise (SIGTRAP);
|
2016-02-15 17:21:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
prepare_debugger (void)
|
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
await_debugger (void)
|
|
|
|
{
|
2016-03-21 14:19:08 +11:00
|
|
|
if (is_debugged ())
|
|
|
|
breakpoint ();
|
|
|
|
else
|
|
|
|
raise (SIGSTOP);
|
|
|
|
|
2016-02-15 17:21:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <fenv.h>
|
|
|
|
void
|
|
|
|
enable_fpe (void)
|
|
|
|
{
|
2018-03-27 15:50:29 +11:00
|
|
|
feenableexcept (FE_INVALID);
|
2016-02-15 17:21:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
disable_fpe (void)
|
|
|
|
{
|
|
|
|
feenableexcept (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
force_console (void)
|
|
|
|
{
|
2018-03-27 15:50:29 +11:00
|
|
|
if (!isatty (fileno (stdout) && !isatty (fileno (stderr)))) {
|
2016-05-18 10:27:19 +10:00
|
|
|
LOG_WARN ("expected debugging under a console");
|
2016-02-15 17:21:01 +11:00
|
|
|
}
|
|
|
|
}
|