Rizin
unix-like reverse engineering framework and cli tools
drx.c File Reference
#include <rz_types.h>

Go to the source code of this file.

Macros

#define DRXN   8
 
#define DR_STATUS   6
 
#define DR_CONTROL   7
 
#define DR_LOCAL_ENABLE_SHIFT   0 /* Extra shift to the local enable bit. */
 
#define DR_GLOBAL_ENABLE_SHIFT   1 /* Extra shift to the global enable bit. */
 
#define DR_ENABLE_SIZE   2 /* Two enable bits per debug register. */
 
#define DR_CONTROL_RESERVED   (0xFC00)
 
#define I386_DR_CONTROL_MASK   (~DR_CONTROL_RESERVED)
 
#define DR_LOCAL_SLOWDOWN   (0x100)
 
#define DR_GLOBAL_SLOWDOWN   (0x200)
 
#define DR_CONTROL_SHIFT   16
 
#define DR_CONTROL_SIZE   4
 
#define DR_RW_EXECUTE   (0x0) /* Break on instruction execution. */
 
#define DR_RW_WRITE   (0x1) /* Break on data writes. */
 
#define DR_RW_IORW   (0x2) /* Break on I/O reads or writes (not supported (2001) */
 
#define DR_RW_READ   (0x3) /* Break on data reads or writes. */
 
#define DR_NADDR   4 /* The number of debug address registers. */
 
#define DR_STATUS   6 /* Index of debug status register (DR6). */
 
#define DR_CONTROL   7 /* Index of debug control register (DR7). */
 
#define drxt   size_t
 
#define DR_LEN_1   (0 << 2) /* 1-byte region watch or breakpoint. */
 
#define DR_LEN_2   (1 << 2) /* 2-byte region watch. */
 
#define DR_LEN_4   (3 << 2) /* 4-byte region watch. */
 
#define DR_LEN_8   (2 << 2) /* 8-byte region watch (AMD64). */
 
#define I386_DR_CONTROL_MASK   (~DR_CONTROL_RESERVED)
 
#define I386_DR_VACANT(control, i)    ((control & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
 
#define I386_DR_LOCAL_ENABLE(control, i)    control |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))
 
#define I386_DR_GLOBAL_ENABLE(control, i)    control |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))
 
#define I386_DR_IS_LOCAL_ENABLED(control, i)    (control & (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))))
 
#define I386_DR_IS_ENABLED(control, i)    control &(3 << (DR_ENABLE_SIZE * (i)))
 
#define I386_DR_ENABLE(control, i)    control |= (3 << (DR_ENABLE_SIZE * (i)))
 
#define I386_DR_DISABLE(control, i)    control &= ~(3 << (DR_ENABLE_SIZE * (i)))
 
#define I386_DR_SET_RW_LEN(control, i, rwlen)
 
#define I386_DR_GET_RW_LEN(control, i)    ((control >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
 

Functions

int drx_set (drxt *drx, int n, ut64 addr, int len, int rwx, int global)
 
ut64 drx_get (drxt *drx, int n, int *rwx, int *len, int *global, int *enabled)
 
int drx_next (drxt *drx)
 
int drx_get_at (drxt *drx, ut64 at_addr)
 
void drx_list (drxt *drx)
 
void drx_init (drxt *r)
 
void drx_enable (drxt *r, int n, int enabled)
 
bool drx_add (RzDebug *dbg, RzBreakpoint *bp, RzBreakpointItem *b)
 
bool drx_del (RzDebug *dbg, RzBreakpoint *bp, RzBreakpointItem *b)
 

Macro Definition Documentation

◆ DR_CONTROL [1/2]

#define DR_CONTROL   7

Definition at line 41 of file drx.c.

◆ DR_CONTROL [2/2]

#define DR_CONTROL   7 /* Index of debug control register (DR7). */

Definition at line 41 of file drx.c.

◆ DR_CONTROL_RESERVED

#define DR_CONTROL_RESERVED   (0xFC00)

Definition at line 20 of file drx.c.

◆ DR_CONTROL_SHIFT

#define DR_CONTROL_SHIFT   16

Definition at line 29 of file drx.c.

◆ DR_CONTROL_SIZE

#define DR_CONTROL_SIZE   4

Definition at line 31 of file drx.c.

◆ DR_ENABLE_SIZE

#define DR_ENABLE_SIZE   2 /* Two enable bits per debug register. */

Definition at line 13 of file drx.c.

◆ DR_GLOBAL_ENABLE_SHIFT

#define DR_GLOBAL_ENABLE_SHIFT   1 /* Extra shift to the global enable bit. */

Definition at line 12 of file drx.c.

◆ DR_GLOBAL_SLOWDOWN

#define DR_GLOBAL_SLOWDOWN   (0x200)

Definition at line 25 of file drx.c.

◆ DR_LEN_1

#define DR_LEN_1   (0 << 2) /* 1-byte region watch or breakpoint. */

Definition at line 46 of file drx.c.

◆ DR_LEN_2

#define DR_LEN_2   (1 << 2) /* 2-byte region watch. */

Definition at line 47 of file drx.c.

◆ DR_LEN_4

#define DR_LEN_4   (3 << 2) /* 4-byte region watch. */

Definition at line 48 of file drx.c.

◆ DR_LEN_8

#define DR_LEN_8   (2 << 2) /* 8-byte region watch (AMD64). */

Definition at line 49 of file drx.c.

◆ DR_LOCAL_ENABLE_SHIFT

#define DR_LOCAL_ENABLE_SHIFT   0 /* Extra shift to the local enable bit. */

Definition at line 11 of file drx.c.

◆ DR_LOCAL_SLOWDOWN

#define DR_LOCAL_SLOWDOWN   (0x100)

Definition at line 24 of file drx.c.

◆ DR_NADDR

#define DR_NADDR   4 /* The number of debug address registers. */

Definition at line 39 of file drx.c.

◆ DR_RW_EXECUTE

#define DR_RW_EXECUTE   (0x0) /* Break on instruction execution. */

Definition at line 33 of file drx.c.

◆ DR_RW_IORW

#define DR_RW_IORW   (0x2) /* Break on I/O reads or writes (not supported (2001) */

Definition at line 35 of file drx.c.

◆ DR_RW_READ

#define DR_RW_READ   (0x3) /* Break on data reads or writes. */

Definition at line 36 of file drx.c.

◆ DR_RW_WRITE

#define DR_RW_WRITE   (0x1) /* Break on data writes. */

Definition at line 34 of file drx.c.

◆ DR_STATUS [1/2]

#define DR_STATUS   6

Definition at line 40 of file drx.c.

◆ DR_STATUS [2/2]

#define DR_STATUS   6 /* Index of debug status register (DR6). */

Definition at line 40 of file drx.c.

◆ DRXN

#define DRXN   8

Definition at line 7 of file drx.c.

◆ drxt

#define drxt   size_t

Definition at line 44 of file drx.c.

◆ I386_DR_CONTROL_MASK [1/2]

#define I386_DR_CONTROL_MASK   (~DR_CONTROL_RESERVED)

Definition at line 51 of file drx.c.

◆ I386_DR_CONTROL_MASK [2/2]

#define I386_DR_CONTROL_MASK   (~DR_CONTROL_RESERVED)

Definition at line 51 of file drx.c.

◆ I386_DR_DISABLE

#define I386_DR_DISABLE (   control,
  i 
)     control &= ~(3 << (DR_ENABLE_SIZE * (i)))

Definition at line 70 of file drx.c.

◆ I386_DR_ENABLE

#define I386_DR_ENABLE (   control,
  i 
)     control |= (3 << (DR_ENABLE_SIZE * (i)))

Definition at line 68 of file drx.c.

◆ I386_DR_GET_RW_LEN

#define I386_DR_GET_RW_LEN (   control,
  i 
)     ((control >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)

Definition at line 78 of file drx.c.

◆ I386_DR_GLOBAL_ENABLE

#define I386_DR_GLOBAL_ENABLE (   control,
  i 
)     control |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))

Definition at line 59 of file drx.c.

◆ I386_DR_IS_ENABLED

#define I386_DR_IS_ENABLED (   control,
  i 
)     control &(3 << (DR_ENABLE_SIZE * (i)))

Definition at line 65 of file drx.c.

◆ I386_DR_IS_LOCAL_ENABLED

#define I386_DR_IS_LOCAL_ENABLED (   control,
  i 
)     (control & (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))))

Definition at line 62 of file drx.c.

◆ I386_DR_LOCAL_ENABLE

#define I386_DR_LOCAL_ENABLE (   control,
  i 
)     control |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))

Definition at line 57 of file drx.c.

◆ I386_DR_SET_RW_LEN

#define I386_DR_SET_RW_LEN (   control,
  i,
  rwlen 
)
Value:
do { \
control &= ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
control |= ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
} while (0)
lzma_index ** i
Definition: index.h:629
#define DR_CONTROL_SIZE
Definition: drx.c:31
#define DR_CONTROL_SHIFT
Definition: drx.c:29

Definition at line 73 of file drx.c.

◆ I386_DR_VACANT

#define I386_DR_VACANT (   control,
  i 
)     ((control & (3 << (DR_ENABLE_SIZE * (i)))) == 0)

Definition at line 54 of file drx.c.

Function Documentation

◆ drx_add()

bool drx_add ( RzDebug dbg,
RzBreakpoint bp,
RzBreakpointItem b 
)

Definition at line 209 of file drx.c.

209  {
210  if (bp->nhwbps < 4) {
212  rz_debug_drx_set(dbg, bp->nhwbps, b->addr, b->size, b->perm, 0);
214  bp->nhwbps++;
215  return true;
216  }
217  return false;
218 }
RzDebug * dbg
Definition: desil.c:30
RZ_API int rz_debug_reg_sync(RzDebug *dbg, int type, int write)
Definition: dreg.c:9
RZ_API int rz_debug_drx_set(RzDebug *dbg, int idx, ut64 addr, int len, int rwx, int g)
Definition: debug.c:1668
@ RZ_REG_TYPE_DRX
Definition: rz_reg.h:22
#define b(i)
Definition: sha256.c:42
int nhwbps
Definition: rz_bp.h:92

References b, dbg, rz_bp_t::nhwbps, rz_debug_drx_set(), rz_debug_reg_sync(), and RZ_REG_TYPE_DRX.

◆ drx_del()

bool drx_del ( RzDebug dbg,
RzBreakpoint bp,
RzBreakpointItem b 
)

Definition at line 220 of file drx.c.

220  {
221  if (bp->nhwbps > 0) {
223  rz_debug_drx_unset(dbg, bp->nhwbps - 1);
225  bp->nhwbps--;
226  }
227  return true;
228 }
RZ_API int rz_debug_drx_unset(RzDebug *dbg, int idx)
Definition: debug.c:1675

References dbg, rz_bp_t::nhwbps, rz_debug_drx_unset(), rz_debug_reg_sync(), and RZ_REG_TYPE_DRX.

◆ drx_enable()

void drx_enable ( drxt r,
int  n,
int  enabled 
)

Definition at line 201 of file drx.c.

201  {
202  if (enabled) {
204  } else {
206  }
207 }
#define r
Definition: crypto_rc6.c:12
#define I386_DR_ENABLE(control, i)
Definition: drx.c:68
#define I386_DR_DISABLE(control, i)
Definition: drx.c:70
#define DR_CONTROL
Definition: drx.c:41
int n
Definition: mipsasm.c:19

References DR_CONTROL, I386_DR_DISABLE, I386_DR_ENABLE, n, and r.

◆ drx_get()

ut64 drx_get ( drxt drx,
int  n,
int rwx,
int len,
int global,
int enabled 
)

Definition at line 128 of file drx.c.

128  {
129  int ret = I386_DR_GET_RW_LEN(drx[DR_CONTROL], n);
130  if (global) {
131  *global = I386_DR_IS_LOCAL_ENABLED(drx[7], n);
132  }
133  if (len) {
134  switch (ret & 0xC) {
135  case DR_LEN_1: *len = 1; break;
136  case DR_LEN_2: *len = 2; break;
137  case DR_LEN_4: *len = 4; break;
138  case DR_LEN_8: *len = 8; break;
139  default: *len = 0; break;
140  }
141  }
142  if (enabled) {
143  *enabled = I386_DR_IS_ENABLED(drx[7], n);
144  }
145  if (rwx) {
146  *rwx = ret & 0x3;
147  }
148  return (ut64)drx[n];
149 }
size_t len
Definition: 6502dis.c:15
#define DR_LEN_2
Definition: drx.c:47
#define I386_DR_GET_RW_LEN(control, i)
Definition: drx.c:78
#define DR_LEN_1
Definition: drx.c:46
#define DR_LEN_8
Definition: drx.c:49
#define I386_DR_IS_ENABLED(control, i)
Definition: drx.c:65
#define DR_LEN_4
Definition: drx.c:48
#define I386_DR_IS_LOCAL_ENABLED(control, i)
Definition: drx.c:62
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References DR_CONTROL, DR_LEN_1, DR_LEN_2, DR_LEN_4, DR_LEN_8, I386_DR_GET_RW_LEN, I386_DR_IS_ENABLED, I386_DR_IS_LOCAL_ENABLED, len, n, and ut64().

Referenced by drx_get_at(), and drx_list().

◆ drx_get_at()

int drx_get_at ( drxt drx,
ut64  at_addr 
)

Definition at line 161 of file drx.c.

161  {
162  ut64 addr;
163  int i, rwx, len, g, en;
164 
165  for (i = 0; i < 8; i++) {
166  if (i == 4 || i == 5) {
167  continue;
168  }
169  rwx = len = g = en = 0;
170  addr = drx_get(drx, i, &rwx, &len, &g, &en);
171  if (addr == at_addr) {
172  return i;
173  }
174  }
175  return -1;
176 }
ut64 drx_get(drxt *drx, int n, int *rwx, int *len, int *global, int *enabled)
Definition: drx.c:128
struct @667 g
static int addr
Definition: z80asm.c:58

References addr, drx_get(), g, i, len, and ut64().

◆ drx_init()

void drx_init ( drxt r)

Definition at line 197 of file drx.c.

197  {
198  memset(r, 0, sizeof(drxt) * (DRXN + 1));
199 }
#define drxt
Definition: drx.c:44
#define DRXN
Definition: drx.c:7
return memset(p, 0, total)

References DRXN, drxt, memset(), and r.

◆ drx_list()

void drx_list ( drxt drx)

Definition at line 178 of file drx.c.

178  {
179  ut64 addr;
180  int i, rwx, len, g, en;
181  for (i = 0; i < 8; i++) {
182  if (i == 4 || i == 5) {
183  continue;
184  }
185  rwx = len = g = en = 0;
186  addr = drx_get(drx, i, &rwx, &len, &g, &en);
187  printf("%c dr%d %c%c 0x%08" PFMT64x " %d\n",
188  en ? '*' : '-', i, g ? 'G' : 'L',
189  (rwx == DR_RW_READ) ? 'r' : (rwx == DR_RW_WRITE) ? 'w'
190  : (rwx == DR_RW_EXECUTE) ? 'x'
191  : (rwx == DR_RW_IORW) ? 'i'
192  : '?',
193  addr, len);
194  }
195 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
#define DR_RW_EXECUTE
Definition: drx.c:33
#define DR_RW_READ
Definition: drx.c:36
#define DR_RW_IORW
Definition: drx.c:35
#define DR_RW_WRITE
Definition: drx.c:34
#define PFMT64x
Definition: rz_types.h:393

References addr, DR_RW_EXECUTE, DR_RW_IORW, DR_RW_READ, DR_RW_WRITE, drx_get(), g, i, len, PFMT64x, printf(), and ut64().

◆ drx_next()

int drx_next ( drxt drx)

Definition at line 151 of file drx.c.

151  {
152  int i;
153  for (i = 0; i < 4; i++) {
154  if (!drx[i]) {
155  return i;
156  }
157  }
158  return -1;
159 }

References i.

◆ drx_set()

int drx_set ( drxt drx,
int  n,
ut64  addr,
int  len,
int  rwx,
int  global 
)

Definition at line 83 of file drx.c.

83  {
84  ut32 control = drx[DR_CONTROL];
85  if (n < 0 || n >= DR_NADDR) {
86  eprintf("Invalid DRX index (0-%d)\n", DR_NADDR - 1);
87  return false;
88  }
89  switch (rwx) {
90  case 1: rwx = 0; break;
91  case 2: rwx = 1; break;
92  case 4: rwx = 3; break;
93  default:
94  rwx = 0;
95  }
96  switch (len) {
97  case 1: len = 0; break;
98  case 2: len = 1 << 2; break;
99  case 4: len = 3 << 2; break;
100  case 8: len = 2 << 2; break; // AMD64 only
101  case -1: {
104  drx[DR_CONTROL] = control;
105  drx[n] = 0;
106  return true;
107  }
108  default:
109  eprintf("Invalid DRX length (%d) must be 1, 2, 4, 8 bytes\n", len);
110  return false;
111  }
112  I386_DR_SET_RW_LEN(control, n, len | rwx);
113  if (global) {
115  // control |= DR_GLOBAL_SLOWDOWN;
116  } else {
118  // control |= DR_LOCAL_SLOWDOWN; // XXX: This is wrong
119  }
121  drx[n] = addr;
122  // eprintf ("drx[DR_CONTROL] = %x \n", drx[DR_CONTROL]);
123  drx[DR_CONTROL] = control;
124  // eprintf ("CONTROL = %x\n", control);
125  return true;
126 }
uint32_t ut32
#define I386_DR_SET_RW_LEN(control, i, rwlen)
Definition: drx.c:73
#define I386_DR_LOCAL_ENABLE(control, i)
Definition: drx.c:57
#define DR_NADDR
Definition: drx.c:39
#define I386_DR_GLOBAL_ENABLE(control, i)
Definition: drx.c:59
#define I386_DR_CONTROL_MASK
Definition: drx.c:51
#define eprintf(x, y...)
Definition: rlcc.c:7
control

References addr, control, DR_CONTROL, DR_NADDR, eprintf, I386_DR_CONTROL_MASK, I386_DR_DISABLE, I386_DR_GLOBAL_ENABLE, I386_DR_LOCAL_ENABLE, I386_DR_SET_RW_LEN, len, and n.