forked from Trietptm-on-Security/tracer
/
utils.c
executable file
·173 lines (155 loc) · 4.86 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* _______
* |__ __|
* | |_ __ __ _ ___ ___ _ __
* | | '__/ _` |/ __/ _ \ '__|
* | | | | (_| | (_| __/ |
* |_|_| \__,_|\___\___|_|
*
* Written by Dennis Yurichev <dennis(a)yurichev.com>, 2013
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/.
*
*/
#include <stdbool.h>
#include "ostrings.h"
#include "oassert.h"
#include "datatypes.h"
#include "logging.h"
#include "utils.h"
#include "tracer.h"
#include "process.h"
#include "thread.h"
#include "BP.h"
#include "BPM.h"
#include "bp_address.h"
#include "opts.h"
#include "bitfields.h"
#include "x86.h"
#include "stuff.h"
#include "module.h"
void dump_PID_if_need(process *p)
{
if (rbtree_count(processes)>1)
L ("PID=%d|", p->PID);
};
void dump_TID_if_need(process *p, thread *t)
{
if (rbtree_count(p->threads)>1)
L ("TID=%d|", t->TID);
};
// FIXME: find another place for this function
void set_or_update_DRx_breakpoint(BP *bp, CONTEXT *ctx, unsigned DRx_no)
{
oassert (bp->a->resolved);
if (utils_c_debug)
{
strbuf sb=STRBUF_INIT;
address_to_string(bp->a, &sb);
L ("%s(): begin. setting DRx-breakpoint %d for %s at 0x" PRI_ADR_HEX "\n",
__func__, DRx_no, sb.buf, bp->a->abs_address);
strbuf_deinit (&sb);
};
if (bp->t==BP_type_BPF || bp->t==BP_type_BPX)
CONTEXT_setDRx_and_DR7 (ctx, DRx_no, bp->a->abs_address);
else if (bp->t==BP_type_BPM)
BPM_set_or_update_DRx_breakpoint(bp->u.bpm, bp->a->abs_address, DRx_no, ctx);
else
{
oassert(0);
fatal_error();
};
if (utils_c_debug)
L ("%s() end\n", __func__);
};
void set_or_update_DRx_for_thread(thread *t, BP *bp, unsigned DRx_no)
{
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_ALL;
DWORD rt;
rt=GetThreadContext (t->THDL, &ctx); oassert (rt!=FALSE);
if (utils_c_debug)
L ("%s() going to call set_or_update_DRx_breakpoint for TID %d\n", __func__, t->TID);
set_or_update_DRx_breakpoint(bp, &ctx, DRx_no);
rt=SetThreadContext (t->THDL, &ctx); oassert (rt!=FALSE);
};
void set_or_update_all_DRx_breakpoints(process *p)
{
if (utils_c_debug)
L ("%s() begin\n", __func__);
// enum all breakpoints, pick out a->resolved ones
for (unsigned DRx_no=0; DRx_no<4; DRx_no++)
{
BP *bp=breakpoints[DRx_no];
if (bp)
{
//L ("%s() DRx_breakpoints[%d]=0x%p\n", __func__, DRx_no, bp);
}
else
{
if (utils_c_debug)
L ("%s() breakpoints[%d]==NULL\n", __func__, DRx_no);
continue;
};
//dump_BP (bp);
if (bp->a->resolved==false)
{
if (utils_c_debug)
L ("%s() breakpoints[%d]->a->resolved==false\n", __func__, DRx_no);
continue;
};
if (load_filename && p->we_are_loading_and_OEP_was_executed==false)
{
if (utils_c_debug)
L ("%s() p->we_are_loading_and_OEP_was_executed==false\n", __func__);
continue;
};
for (struct rbtree_node_t *_t=rbtree_minimum(p->threads); _t; _t=rbtree_succ(_t))
{
thread *t=(thread*)(_t->value);
set_or_update_DRx_for_thread (t, bp, DRx_no);
};
};
if (utils_c_debug)
L ("%s() end\n", __func__);
};
bool MC_disas(address a, MemoryCache *mc, Da* out)
{
return Da_Da_callbacks (Fuzzy_Undefined, a,
(callback_read_byte)MC_ReadByte,
(callback_read_word)MC_ReadWyde,
(callback_read_dword)MC_ReadTetrabyte,
(callback_read_oword)MC_ReadOctabyte,
(void *)mc, out);
};
void dump_buf_as_array_of_strings(MemoryCache *mc, address a, size_t size)
{
strbuf sb=STRBUF_INIT;
BYTE *buf=DMALLOC (BYTE, size, "BYTE*");
if (MC_ReadBuffer (mc, a, size, buf)==false)
goto exit;
for (unsigned i=0; i<size; i+=sizeof(REG))
{
address a2=*(REG*)&buf[i];
if (MC_get_any_string(mc, a2, &sb))
{
L ("0x" PRI_ADR_HEX "+0x%x: ptr to %s\n", a, i, sb.buf);
strbuf_reinit(&sb, 0);
};
};
exit:
DFREE (buf);
strbuf_deinit(&sb);
};
// idx may be negative
bool read_REG_from_stack (MemoryCache *mc, CONTEXT *ctx, int idx, REG * out)
{
address SP=CONTEXT_get_SP(ctx);
return MC_ReadREG (mc, SP + idx*sizeof(REG), out);
};
bool read_argument_from_stack (MemoryCache *mc, CONTEXT *ctx, unsigned arg, REG * out)
{
return read_REG_from_stack (mc, ctx, arg+1, out);
};
/* vim: set expandtab ts=4 sw=4 : */