2016-12-06 04:13:02 +00:00
|
|
|
/*-
|
2017-11-26 02:00:33 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2016-12-06 04:13:02 +00:00
|
|
|
* Copyright (c) 2010 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2010-07-31 16:10:20 +00:00
|
|
|
* This software was developed by Rui Paulo under sponsorship from the
|
2016-12-06 04:13:02 +00:00
|
|
|
* FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2010-07-31 16:10:20 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2013-11-05 03:23:54 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
2016-12-06 04:13:02 +00:00
|
|
|
|
2010-07-31 16:10:20 +00:00
|
|
|
#include "_libproc.h"
|
|
|
|
|
2015-07-01 13:59:26 +00:00
|
|
|
#if defined(__aarch64__)
|
|
|
|
#define AARCH64_BRK 0xd4200000
|
|
|
|
#define AARCH64_BRK_IMM16_SHIFT 5
|
|
|
|
#define AARCH64_BRK_IMM16_VAL (0xd << AARCH64_BRK_IMM16_SHIFT)
|
|
|
|
#define BREAKPOINT_INSTR (AARCH64_BRK | AARCH64_BRK_IMM16_VAL)
|
|
|
|
#define BREAKPOINT_INSTR_SZ 4
|
|
|
|
#elif defined(__amd64__) || defined(__i386__)
|
|
|
|
#define BREAKPOINT_INSTR 0xcc /* int 0x3 */
|
2010-07-31 16:10:20 +00:00
|
|
|
#define BREAKPOINT_INSTR_SZ 1
|
2015-08-24 12:17:15 +00:00
|
|
|
#define BREAKPOINT_ADJUST_SZ BREAKPOINT_INSTR_SZ
|
2015-07-01 13:59:26 +00:00
|
|
|
#elif defined(__arm__)
|
|
|
|
#define BREAKPOINT_INSTR 0xe7ffffff /* bkpt */
|
|
|
|
#define BREAKPOINT_INSTR_SZ 4
|
2012-03-23 23:07:02 +00:00
|
|
|
#elif defined(__mips__)
|
2015-07-01 13:59:26 +00:00
|
|
|
#define BREAKPOINT_INSTR 0xd /* break */
|
2012-03-23 23:07:02 +00:00
|
|
|
#define BREAKPOINT_INSTR_SZ 4
|
2012-11-07 23:45:09 +00:00
|
|
|
#elif defined(__powerpc__)
|
2015-07-01 13:59:26 +00:00
|
|
|
#define BREAKPOINT_INSTR 0x7fe00008 /* trap */
|
|
|
|
#define BREAKPOINT_INSTR_SZ 4
|
2017-08-07 14:09:57 +00:00
|
|
|
#elif defined(__riscv)
|
2016-01-24 12:10:29 +00:00
|
|
|
#define BREAKPOINT_INSTR 0x00100073 /* sbreak */
|
|
|
|
#define BREAKPOINT_INSTR_SZ 4
|
2010-07-31 16:10:20 +00:00
|
|
|
#else
|
|
|
|
#error "Add support for your architecture"
|
|
|
|
#endif
|
|
|
|
|
2016-10-26 14:26:45 +00:00
|
|
|
/*
|
|
|
|
* Use 4-bytes holder for breakpoint instruction on all the platforms.
|
|
|
|
* Works for x86 as well until it is endian-little platform.
|
|
|
|
* (We are coping one byte only on x86 from this 4-bytes piece of
|
|
|
|
* memory).
|
|
|
|
*/
|
|
|
|
typedef uint32_t instr_t;
|
|
|
|
|
2013-11-05 03:23:54 +00:00
|
|
|
static int
|
|
|
|
proc_stop(struct proc_handle *phdl)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
|
|
|
|
DPRINTF("kill %d", proc_getpid(phdl));
|
|
|
|
return (-1);
|
|
|
|
} else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
|
|
|
|
DPRINTF("waitpid %d", proc_getpid(phdl));
|
|
|
|
return (-1);
|
|
|
|
} else if (!WIFSTOPPED(status)) {
|
|
|
|
DPRINTFX("waitpid: unexpected status 0x%x", status);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:10:20 +00:00
|
|
|
int
|
|
|
|
proc_bkptset(struct proc_handle *phdl, uintptr_t address,
|
|
|
|
unsigned long *saved)
|
|
|
|
{
|
|
|
|
struct ptrace_io_desc piod;
|
2014-05-04 03:34:32 +00:00
|
|
|
int ret = 0, stopped;
|
2016-10-26 14:26:45 +00:00
|
|
|
instr_t instr;
|
2010-07-31 16:10:20 +00:00
|
|
|
|
|
|
|
*saved = 0;
|
|
|
|
if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
|
|
|
|
phdl->status == PS_IDLE) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
Merge OpenZFS support in to HEAD.
The primary benefit is maintaining a completely shared
code base with the community allowing FreeBSD to receive
new features sooner and with less effort.
I would advise against doing 'zpool upgrade'
or creating indispensable pools using new
features until this change has had a month+
to soak.
Work on merging FreeBSD support in to what was
at the time "ZFS on Linux" began in August 2018.
I first publicly proposed transitioning FreeBSD
to (new) OpenZFS on December 18th, 2018. FreeBSD
support in OpenZFS was finally completed in December
2019. A CFT for downstreaming OpenZFS support in
to FreeBSD was first issued on July 8th. All issues
that were reported have been addressed or, for
a couple of less critical matters there are
pull requests in progress with OpenZFS. iXsystems
has tested and dogfooded extensively internally.
The TrueNAS 12 release is based on OpenZFS with
some additional features that have not yet made
it upstream.
Improvements include:
project quotas, encrypted datasets,
allocation classes, vectorized raidz,
vectorized checksums, various command line
improvements, zstd compression.
Thanks to those who have helped along the way:
Ryan Moeller, Allan Jude, Zack Welch, and many
others.
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D25872
2020-08-25 02:21:27 +00:00
|
|
|
DPRINTFX("adding breakpoint at 0x%lx", (unsigned long)address);
|
2013-11-05 03:23:54 +00:00
|
|
|
|
2014-05-04 03:34:32 +00:00
|
|
|
stopped = 0;
|
|
|
|
if (phdl->status != PS_STOP) {
|
2013-11-05 03:23:54 +00:00
|
|
|
if (proc_stop(phdl) != 0)
|
|
|
|
return (-1);
|
2014-05-04 03:34:32 +00:00
|
|
|
stopped = 1;
|
|
|
|
}
|
2013-11-05 03:23:54 +00:00
|
|
|
|
2010-07-31 16:10:20 +00:00
|
|
|
/*
|
|
|
|
* Read the original instruction.
|
|
|
|
*/
|
2016-10-26 14:26:45 +00:00
|
|
|
instr = 0;
|
2010-07-31 16:10:20 +00:00
|
|
|
piod.piod_op = PIOD_READ_I;
|
2019-07-19 23:10:43 +00:00
|
|
|
piod.piod_offs = (void *)address;
|
2016-10-26 14:26:45 +00:00
|
|
|
piod.piod_addr = &instr;
|
2010-07-31 16:10:20 +00:00
|
|
|
piod.piod_len = BREAKPOINT_INSTR_SZ;
|
|
|
|
if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
|
2016-12-06 04:13:02 +00:00
|
|
|
DPRINTF("ERROR: couldn't read instruction at address 0x%jx",
|
|
|
|
(uintmax_t)address);
|
2013-11-05 03:23:54 +00:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
2010-07-31 16:10:20 +00:00
|
|
|
}
|
2016-10-26 14:26:45 +00:00
|
|
|
*saved = instr;
|
2010-07-31 16:10:20 +00:00
|
|
|
/*
|
|
|
|
* Write a breakpoint instruction to that address.
|
|
|
|
*/
|
2016-10-26 14:26:45 +00:00
|
|
|
instr = BREAKPOINT_INSTR;
|
2010-07-31 16:10:20 +00:00
|
|
|
piod.piod_op = PIOD_WRITE_I;
|
2019-07-19 23:10:43 +00:00
|
|
|
piod.piod_offs = (void *)address;
|
2016-10-26 14:26:45 +00:00
|
|
|
piod.piod_addr = &instr;
|
2010-07-31 16:10:20 +00:00
|
|
|
piod.piod_len = BREAKPOINT_INSTR_SZ;
|
|
|
|
if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
|
2016-12-06 04:13:02 +00:00
|
|
|
DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
|
|
|
|
(uintmax_t)address);
|
2013-11-05 03:23:54 +00:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
2010-07-31 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 03:23:54 +00:00
|
|
|
done:
|
2014-05-04 03:34:32 +00:00
|
|
|
if (stopped)
|
2013-11-05 03:23:54 +00:00
|
|
|
/* Restart the process if we had to stop it. */
|
2014-05-04 03:34:32 +00:00
|
|
|
proc_continue(phdl);
|
2013-11-05 03:23:54 +00:00
|
|
|
|
|
|
|
return (ret);
|
2010-07-31 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
|
|
|
|
unsigned long saved)
|
|
|
|
{
|
|
|
|
struct ptrace_io_desc piod;
|
2014-05-04 03:34:32 +00:00
|
|
|
int ret = 0, stopped;
|
2016-10-26 14:26:45 +00:00
|
|
|
instr_t instr;
|
2010-07-31 16:10:20 +00:00
|
|
|
|
|
|
|
if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
|
|
|
|
phdl->status == PS_IDLE) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return (-1);
|
|
|
|
}
|
2013-11-05 03:23:54 +00:00
|
|
|
|
Merge OpenZFS support in to HEAD.
The primary benefit is maintaining a completely shared
code base with the community allowing FreeBSD to receive
new features sooner and with less effort.
I would advise against doing 'zpool upgrade'
or creating indispensable pools using new
features until this change has had a month+
to soak.
Work on merging FreeBSD support in to what was
at the time "ZFS on Linux" began in August 2018.
I first publicly proposed transitioning FreeBSD
to (new) OpenZFS on December 18th, 2018. FreeBSD
support in OpenZFS was finally completed in December
2019. A CFT for downstreaming OpenZFS support in
to FreeBSD was first issued on July 8th. All issues
that were reported have been addressed or, for
a couple of less critical matters there are
pull requests in progress with OpenZFS. iXsystems
has tested and dogfooded extensively internally.
The TrueNAS 12 release is based on OpenZFS with
some additional features that have not yet made
it upstream.
Improvements include:
project quotas, encrypted datasets,
allocation classes, vectorized raidz,
vectorized checksums, various command line
improvements, zstd compression.
Thanks to those who have helped along the way:
Ryan Moeller, Allan Jude, Zack Welch, and many
others.
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D25872
2020-08-25 02:21:27 +00:00
|
|
|
DPRINTFX("removing breakpoint at 0x%lx", (unsigned long)address);
|
2013-11-05 03:23:54 +00:00
|
|
|
|
2014-05-04 03:34:32 +00:00
|
|
|
stopped = 0;
|
|
|
|
if (phdl->status != PS_STOP) {
|
2013-11-05 03:23:54 +00:00
|
|
|
if (proc_stop(phdl) != 0)
|
|
|
|
return (-1);
|
2014-05-04 03:34:32 +00:00
|
|
|
stopped = 1;
|
|
|
|
}
|
2013-11-05 03:23:54 +00:00
|
|
|
|
2010-07-31 16:10:20 +00:00
|
|
|
/*
|
|
|
|
* Overwrite the breakpoint instruction that we setup previously.
|
|
|
|
*/
|
2016-10-26 14:26:45 +00:00
|
|
|
instr = saved;
|
2010-07-31 16:10:20 +00:00
|
|
|
piod.piod_op = PIOD_WRITE_I;
|
2019-07-19 23:10:43 +00:00
|
|
|
piod.piod_offs = (void *)address;
|
2016-10-26 14:26:45 +00:00
|
|
|
piod.piod_addr = &instr;
|
2010-07-31 16:10:20 +00:00
|
|
|
piod.piod_len = BREAKPOINT_INSTR_SZ;
|
|
|
|
if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
|
2016-12-06 04:13:02 +00:00
|
|
|
DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
|
|
|
|
(uintmax_t)address);
|
2013-11-05 03:23:54 +00:00
|
|
|
ret = -1;
|
2010-07-31 16:10:20 +00:00
|
|
|
}
|
2013-11-05 03:23:54 +00:00
|
|
|
|
2014-05-04 03:34:32 +00:00
|
|
|
if (stopped)
|
2013-11-05 03:23:54 +00:00
|
|
|
/* Restart the process if we had to stop it. */
|
2014-05-04 03:34:32 +00:00
|
|
|
proc_continue(phdl);
|
2016-12-06 04:13:02 +00:00
|
|
|
|
2013-11-05 03:23:54 +00:00
|
|
|
return (ret);
|
2010-07-31 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrement pc so that we delete the breakpoint at the correct
|
|
|
|
* address, i.e. at the BREAKPOINT_INSTR address.
|
2015-08-24 12:17:15 +00:00
|
|
|
*
|
|
|
|
* This is only needed on some architectures where the pc value
|
|
|
|
* when reading registers points at the instruction after the
|
|
|
|
* breakpoint, e.g. x86.
|
2010-07-31 16:10:20 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
proc_bkptregadj(unsigned long *pc)
|
|
|
|
{
|
2015-08-24 12:17:15 +00:00
|
|
|
|
|
|
|
(void)pc;
|
|
|
|
#ifdef BREAKPOINT_ADJUST_SZ
|
|
|
|
*pc = *pc - BREAKPOINT_ADJUST_SZ;
|
|
|
|
#endif
|
2010-07-31 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step over the breakpoint.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
|
|
|
|
{
|
|
|
|
unsigned long pc;
|
|
|
|
unsigned long samesaved;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (proc_regget(phdl, REG_PC, &pc) < 0) {
|
2013-10-27 20:39:10 +00:00
|
|
|
DPRINTFX("ERROR: couldn't get PC register");
|
2010-07-31 16:10:20 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
proc_bkptregadj(&pc);
|
|
|
|
if (proc_bkptdel(phdl, pc, saved) < 0) {
|
2013-10-27 20:39:10 +00:00
|
|
|
DPRINTFX("ERROR: couldn't delete breakpoint");
|
2010-07-31 16:10:20 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Go back in time and step over the new instruction just
|
|
|
|
* set up by proc_bkptdel().
|
|
|
|
*/
|
|
|
|
proc_regset(phdl, REG_PC, pc);
|
|
|
|
if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
|
2013-10-27 20:39:10 +00:00
|
|
|
DPRINTFX("ERROR: ptrace step failed");
|
2010-07-31 16:10:20 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2010-08-11 17:33:26 +00:00
|
|
|
proc_wstatus(phdl);
|
|
|
|
status = proc_getwstat(phdl);
|
2010-07-31 16:10:20 +00:00
|
|
|
if (!WIFSTOPPED(status)) {
|
2013-10-27 20:39:10 +00:00
|
|
|
DPRINTFX("ERROR: don't know why process stopped");
|
2010-07-31 16:10:20 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Restore the breakpoint. The saved instruction should be
|
|
|
|
* the same as the one that we were passed in.
|
|
|
|
*/
|
|
|
|
if (proc_bkptset(phdl, pc, &samesaved) < 0) {
|
2013-10-27 20:39:10 +00:00
|
|
|
DPRINTFX("ERROR: couldn't restore breakpoint");
|
2010-07-31 16:10:20 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
assert(samesaved == saved);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|