Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
DynamoRIO
dynamorio
Commits
b707a984
Commit
b707a984
authored
6 years ago
by
Branden Clark
Browse files
Options
Download
Email Patches
Plain Diff
drdbg add api samples
parent
0ba0567f
project-drdbg
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/samples/CMakeLists.txt
+1
-0
api/samples/CMakeLists.txt
api/samples/dbginstrace.c
+147
-0
api/samples/dbginstrace.c
api/samples/dbgmalloc.c
+181
-0
api/samples/dbgmalloc.c
api/samples/dbgxor.c
+157
-0
api/samples/dbgxor.c
with
486 additions
and
0 deletions
+486
-0
api/samples/CMakeLists.txt
+
1
-
0
View file @
b707a984
...
...
@@ -238,6 +238,7 @@ if (X86) # FIXME i#1551, i#1569: port to ARM and AArch64
# dr_insert_cbr_instrument_ex is NYI
add_sample_client
(
cbrtrace
"cbrtrace.c;utils.c"
"drx"
)
endif
(
X86
)
add_sample_client
(
dbgmalloc
"dbgmalloc.c"
"drmgr;drwrap;drdbg"
)
add_sample_client
(
wrap
"wrap.c"
"drmgr;drwrap"
)
add_sample_client
(
signal
"signal.c"
"drmgr"
)
add_sample_client
(
syscall
"syscall.c"
"drmgr"
)
...
...
This diff is collapsed.
Click to expand it.
api/samples/dbginstrace.c
0 → 100644
+
147
-
0
View file @
b707a984
/* **********************************************************
* Copyright (c) 2014 Google, Inc. All rights reserved.
* Copyright (c) 2008 VMware, Inc. All rights reserved.
* **********************************************************/
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 VMWARE, INC. 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.
*/
/* Counts the number of dynamic div instruction for which the
* divisor is a power of 2 (these are cases where div could be
* strength reduced to a simple shift). Demonstrates callout
* based profiling with live operand values. */
#include
"dr_api.h"
#include
"drmgr.h"
#include
"drdbg.h"
#include
<string.h>
#ifdef WINDOWS
# define DISPLAY_STRING(msg) dr_messagebox(msg)
#else
# define DISPLAY_STRING(msg) dr_printf("%s\n", msg);
#endif
#define NULL_TERMINATE(buf) buf[(sizeof(buf)/sizeof(buf[0])) - 1] = '\0'
static
dr_emit_flags_t
event_app_instruction
(
void
*
drcontext
,
void
*
tag
,
instrlist_t
*
bb
,
instr_t
*
instr
,
bool
for_trace
,
bool
translating
,
void
*
user_data
);
static
void
exit_event
(
void
);
static
app_pc
trace_pc
=
NULL
;
static
void
*
count_mutex
;
/* for multithread support */
drdbg_status_t
cmd_handler
(
char
*
buf
,
ssize_t
len
,
char
**
outbuf
,
ssize_t
*
outlen
)
{
dr_fprintf
(
STDERR
,
"HANDLER CALLED!! %s
\n
"
,
buf
);
if
(
!
strncmp
(
"itrace"
,
buf
,
strlen
(
"itrace"
)))
{
trace_pc
=
(
app_pc
)
strtoul
(
buf
+
strlen
(
"itrace "
),
NULL
,
16
);
dr_fprintf
(
STDERR
,
"trace_pc: "
PIFX
"
\n
"
,
trace_pc
);
return
DRDBG_SUCCESS
;
}
return
DRDBG_ERROR
;
}
DR_EXPORT
void
dr_client_main
(
client_id_t
id
,
int
argc
,
const
char
*
argv
[])
{
dr_set_client_name
(
"DynamoRIO Sample Client 'div'"
,
"http://dynamorio.org/issues"
);
if
(
!
drmgr_init
())
DR_ASSERT
(
false
);
dr_register_exit_event
(
exit_event
);
if
(
!
drmgr_register_bb_instrumentation_event
(
NULL
,
event_app_instruction
,
NULL
))
DR_ASSERT
(
false
);
count_mutex
=
dr_mutex_create
();
/* Register command handler with drdbg */
drdbg_api_register_cmd
(
cmd_handler
);
}
static
void
exit_event
(
void
)
{
dr_mutex_destroy
(
count_mutex
);
drmgr_exit
();
}
static
void
callback
(
app_pc
pc
)
{
void
*
drcontext
=
dr_get_current_drcontext
();
instr_t
instr
;
opnd_t
opnd
;
dr_mcontext_t
mc
;
dr_mutex_lock
(
count_mutex
);
/* get context */
mc
.
flags
=
DR_MC_INTEGER
|
DR_MC_CONTROL
;
mc
.
size
=
sizeof
(
dr_mcontext_t
);
dr_get_mcontext
(
drcontext
,
&
mc
);
/* Decode instruction */
instr_init
(
drcontext
,
&
instr
);
decode
(
drcontext
,
pc
,
&
instr
);
/* Print disassembly */
instr_disassemble
(
drcontext
,
&
instr
,
2
);
dr_fprintf
(
STDERR
,
"
\n
"
);
/* Print argument resolutions */
opnd
=
instr_get_src
(
&
instr
,
0
);
if
(
opnd_is_reg
(
opnd
))
{
dr_fprintf
(
STDERR
,
"
\t
%s: %p"
,
get_register_name
(
opnd_get_reg
(
opnd
)),
reg_get_value
(
opnd_get_reg
(
opnd
),
&
mc
));
}
opnd
=
instr_get_dst
(
&
instr
,
0
);
if
(
opnd_is_reg
(
opnd
))
{
dr_fprintf
(
STDERR
,
"
\t
%s: %p"
,
get_register_name
(
opnd_get_reg
(
opnd
)),
reg_get_value
(
opnd_get_reg
(
opnd
),
&
mc
));
}
dr_fprintf
(
STDERR
,
"
\n
"
);
dr_mutex_unlock
(
count_mutex
);
}
static
dr_emit_flags_t
event_app_instruction
(
void
*
drcontext
,
void
*
tag
,
instrlist_t
*
bb
,
instr_t
*
instr
,
bool
for_trace
,
bool
translating
,
void
*
user_data
)
{
/* if the right pc, insert a clean call to our instrumentation routine */
if
(
instr_get_app_pc
(
instr
)
==
trace_pc
)
{
dr_fprintf
(
STDERR
,
"Inserting clean call for "
PIFX
"
\n
"
,
instr_get_app_pc
(
instr
));
dr_insert_clean_call
(
drcontext
,
bb
,
instr
,
(
void
*
)
callback
,
false
/*no fp save*/
,
1
,
OPND_CREATE_INTPTR
(
instr_get_app_pc
(
instr
)));
}
return
DR_EMIT_DEFAULT
;
}
This diff is collapsed.
Click to expand it.
api/samples/dbgmalloc.c
0 → 100644
+
181
-
0
View file @
b707a984
/* **********************************************************
* Copyright (c) 2011-2014 Google, Inc. All rights reserved.
* **********************************************************/
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 GOOGLE, INC. 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.
*/
/* Illustrates using the drwrap extension.
*
* Wraps malloc on Linux, HeapAlloc on Windows. Finds the maximum
* malloc size requested, and randomly changes a malloc to return
* failure to test an application's handling of out-of-memory
* conditions.
*/
#include
<string.h>
#include
"dr_api.h"
#include
"drmgr.h"
#include
"drwrap.h"
#include
"drdbg.h"
#ifdef WINDOWS
# define IF_WINDOWS_ELSE(x,y) x
#else
# define IF_WINDOWS_ELSE(x,y) y
#endif
#ifdef WINDOWS
# define DISPLAY_STRING(msg) dr_messagebox(msg)
#else
# define DISPLAY_STRING(msg) dr_printf("%s\n", msg);
#endif
#define NULL_TERMINATE(buf) buf[(sizeof(buf)/sizeof(buf[0])) - 1] = '\0'
static
void
event_exit
(
void
);
static
void
alloc_wrap_pre
(
void
*
wrapcxt
,
OUT
void
**
user_data
);
static
void
free_wrap_pre
(
void
*
wrapcxt
,
OUT
void
**
user_data
);
static
ssize_t
malloc_total
;
static
ssize_t
max_malloc
=
0xffffffff
;
static
void
*
max_lock
;
/* to synch writes to max_malloc */
#define FREE_ROUTINE_NAME IF_WINDOWS_ELSE("HeapFree", "free")
#define MALLOC_ROUTINE_NAME IF_WINDOWS_ELSE("HeapAlloc", "malloc")
drdbg_status_t
cmd_handler
(
char
*
buf
,
ssize_t
len
,
char
**
outbuf
,
ssize_t
*
outlen
)
{
#define TOOL_KEY "dbgmalloc"
if
(
!
strncmp
(
TOOL_KEY
,
buf
,
strlen
(
TOOL_KEY
)))
{
max_malloc
=
strtoul
(
buf
+
strlen
(
TOOL_KEY
)
+
1
,
NULL
,
10
);
dr_fprintf
(
STDERR
,
"Set malloc maximum to %u
\n
"
,
max_malloc
);
return
DRDBG_SUCCESS
;
}
return
DRDBG_ERROR
;
}
static
void
module_load_event
(
void
*
drcontext
,
const
module_data_t
*
mod
,
bool
loaded
)
{
app_pc
allocwrap
=
(
app_pc
)
dr_get_proc_address
(
mod
->
handle
,
MALLOC_ROUTINE_NAME
);
app_pc
freewrap
=
(
app_pc
)
dr_get_proc_address
(
mod
->
handle
,
FREE_ROUTINE_NAME
);
if
(
allocwrap
!=
NULL
&&
freewrap
!=
NULL
)
{
#ifdef SHOW_RESULTS
bool
ok
=
#endif
drwrap_wrap
(
allocwrap
,
alloc_wrap_pre
,
NULL
);
ok
=
ok
&&
drwrap_wrap
(
freewrap
,
free_wrap_pre
,
NULL
);
#ifdef SHOW_RESULTS
if
(
ok
)
{
dr_fprintf
(
STDERR
,
"<wrapped "
MALLOC_ROUTINE_NAME
" @"
PFX
"
\n
"
,
allocwrap
);
dr_fprintf
(
STDERR
,
"<wrapped "
FREE_ROUTINE_NAME
" @"
PFX
"
\n
"
,
freewrap
);
}
else
{
/* We expect this w/ forwarded exports (e.g., on win7 both
* kernel32!HeapAlloc and kernelbase!HeapAlloc forward to
* the same routine in ntdll.dll)
*/
dr_fprintf
(
STDERR
,
"<FAILED to wrap "
MALLOC_ROUTINE_NAME
" @"
PFX
": already wrapped?
\n
"
,
allocwrap
);
dr_fprintf
(
STDERR
,
"<FAILED to wrap "
FREE_ROUTINE_NAME
" @"
PFX
": already wrapped?
\n
"
,
freewrap
);
}
#endif
}
}
DR_EXPORT
void
dr_client_main
(
client_id_t
id
,
int
argc
,
const
char
*
argv
[])
{
dr_set_client_name
(
"DynamoRIO Sample Client 'wrap'"
,
"http://dynamorio.org/issues"
);
/* make it easy to tell, by looking at log file, which client executed */
dr_log
(
NULL
,
LOG_ALL
,
1
,
"Client 'wrap' initializing
\n
"
);
/* also give notification to stderr */
#ifdef SHOW_RESULTS
if
(
dr_is_notify_on
())
{
# ifdef WINDOWS
/* ask for best-effort printing to cmd window. must be called at init. */
dr_enable_console_printing
();
# endif
dr_fprintf
(
STDERR
,
"Client wrap is running
\n
"
);
}
#endif
drmgr_init
();
drwrap_init
();
dr_register_exit_event
(
event_exit
);
drmgr_register_module_load_event
(
module_load_event
);
max_lock
=
dr_mutex_create
();
/* Register command handler with drdbg */
drdbg_api_register_cmd
(
cmd_handler
);
}
static
void
event_exit
(
void
)
{
dr_mutex_destroy
(
max_lock
);
drwrap_exit
();
drmgr_exit
();
}
static
void
alloc_wrap_pre
(
void
*
wrapcxt
,
OUT
void
**
user_data
)
{
/* malloc(size) or HeapAlloc(heap, flags, size) */
size_t
sz
=
(
size_t
)
drwrap_get_arg
(
wrapcxt
,
IF_WINDOWS_ELSE
(
2
,
0
));
/* Add malloc size to sum */
dr_mutex_lock
(
max_lock
);
malloc_total
+=
sz
;
dr_mutex_unlock
(
max_lock
);
if
(
malloc_total
>
max_malloc
)
{
drdbg_api_break
(
drwrap_get_retaddr
(
wrapcxt
));
}
dr_fprintf
(
STDERR
,
"Amount: %u
\n
"
,
malloc_total
);
}
static
void
free_wrap_pre
(
void
*
wrapcxt
,
OUT
void
**
user_data
)
{
/* malloc(size) or HeapAlloc(heap, flags, size) */
size_t
sz
=
(
size_t
)
drwrap_get_arg
(
wrapcxt
,
IF_WINDOWS_ELSE
(
2
,
0
));
/* Subtract free size from malloc total */
dr_mutex_lock
(
max_lock
);
if
(
sz
>
malloc_total
)
{
malloc_total
=
0
;
}
else
{
malloc_total
-=
sz
;
}
dr_mutex_unlock
(
max_lock
);
dr_fprintf
(
STDERR
,
"Amount: %u
\n
"
,
malloc_total
);
}
This diff is collapsed.
Click to expand it.
api/samples/dbgxor.c
0 → 100644
+
157
-
0
View file @
b707a984
/* **********************************************************
* Copyright (c) 2014 Google, Inc. All rights reserved.
* Copyright (c) 2008 VMware, Inc. All rights reserved.
* **********************************************************/
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 VMWARE, INC. 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.
*/
/* Counts the number of dynamic div instruction for which the
* divisor is a power of 2 (these are cases where div could be
* strength reduced to a simple shift). Demonstrates callout
* based profiling with live operand values. */
#include
"dr_api.h"
#include
"drmgr.h"
#include
"drdbg.h"
#include
<string.h>
#ifdef WINDOWS
# define DISPLAY_STRING(msg) dr_messagebox(msg)
#else
# define DISPLAY_STRING(msg) dr_printf("%s\n", msg);
#endif
#define NULL_TERMINATE(buf) buf[(sizeof(buf)/sizeof(buf[0])) - 1] = '\0'
static
dr_emit_flags_t
event_app_instruction
(
void
*
drcontext
,
void
*
tag
,
instrlist_t
*
bb
,
instr_t
*
instr
,
bool
for_trace
,
bool
translating
,
void
*
user_data
);
static
void
exit_event
(
void
);
/* Runtime option: If set, only count instructions in the application itself */
static
bool
only_from_app
=
false
;
/* Application module */
static
app_pc
exe_start
;
static
int
xor_count
=
0
;
static
void
*
count_mutex
;
/* for multithread support */
DR_EXPORT
void
dr_client_main
(
client_id_t
id
,
int
argc
,
const
char
*
argv
[])
{
int
i
;
dr_set_client_name
(
"DynamoRIO Sample Client 'div'"
,
"http://dynamorio.org/issues"
);
/* Options */
for
(
i
=
1
/*skip client*/
;
i
<
argc
;
i
++
)
{
if
(
strcmp
(
argv
[
i
],
"-only_from_app"
)
==
0
)
{
only_from_app
=
true
;
}
else
{
dr_fprintf
(
STDERR
,
"UNRECOGNIZED OPTION:
\"
%s
\"\n
"
,
argv
[
i
]);
DR_ASSERT_MSG
(
false
,
"invalid option"
);
}
}
if
(
!
drmgr_init
())
DR_ASSERT
(
false
);
dr_register_exit_event
(
exit_event
);
if
(
!
drmgr_register_bb_instrumentation_event
(
NULL
,
event_app_instruction
,
NULL
))
DR_ASSERT
(
false
);
count_mutex
=
dr_mutex_create
();
/* Get main module address */
if
(
only_from_app
)
{
module_data_t
*
exe
=
dr_get_main_module
();
if
(
exe
!=
NULL
)
exe_start
=
exe
->
start
;
dr_free_module_data
(
exe
);
}
}
static
void
exit_event
(
void
)
{
#ifdef SHOW_RESULTS
char
msg
[
512
];
int
len
;
len
=
dr_snprintf
(
msg
,
sizeof
(
msg
)
/
sizeof
(
msg
[
0
]),
"Instrumentation results:
\n
"
" saw %d non-zeroing xor instructions
\n
"
,
xor_count
);
DR_ASSERT
(
len
>
0
);
NULL_TERMINATE
(
msg
);
DISPLAY_STRING
(
msg
);
#endif
/* SHOW_RESULTS */
dr_mutex_destroy
(
count_mutex
);
drmgr_exit
();
}
static
void
callback
(
app_pc
addr
)
{
/* instead of a lock could use atomic operations to
* increment the counters */
dr_mutex_lock
(
count_mutex
);
xor_count
++
;
dr_mutex_unlock
(
count_mutex
);
drdbg_api_break
(
addr
);
}
static
dr_emit_flags_t
event_app_instruction
(
void
*
drcontext
,
void
*
tag
,
instrlist_t
*
bb
,
instr_t
*
instr
,
bool
for_trace
,
bool
translating
,
void
*
user_data
)
{
/* if find div, insert a clean call to our instrumentation routine */
if
(
instr_get_opcode
(
instr
)
==
OP_xor
)
{
// Check if non-zeroing
opnd_t
a
=
instr_get_dst
(
instr
,
0
);
opnd_t
b
=
instr_get_src
(
instr
,
0
);
if
(
opnd_is_reg
(
a
)
&&
opnd_is_reg
(
b
)
&&
opnd_get_reg
(
a
)
!=
opnd_get_reg
(
b
))
{
/* Only count in app BBs */
if
(
only_from_app
)
{
module_data_t
*
mod
=
dr_lookup_module
(
instr_get_app_pc
(
instr
));
mod
++
;
//if (mod != NULL) {
// bool from_exe = (mod->start == exe_start);
// dr_free_module_data(mod);
// if (!from_exe) {
// return DR_EMIT_DEFAULT;
// }
//}
}
dr_insert_clean_call
(
drcontext
,
bb
,
instr
,
(
void
*
)
callback
,
false
/*no fp save*/
,
1
,
OPND_CREATE_INTPTR
(
instr_get_app_pc
(
instr
)));
}
}
return
DR_EMIT_DEFAULT
;
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets