Skip to content

Commit e0432f4

Browse files
committed
Initial commit of CGO tutorial demo 1 code.
0 parents  commit e0432f4

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

CMakeLists.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# **********************************************************
2+
# Copyright (c) 2012 Google, Inc. All rights reserved.
3+
# **********************************************************
4+
5+
cmake_minimum_required(VERSION 2.6)
6+
7+
project(countingexample)
8+
9+
# Boiler plate to find DynamoRIO. Invoke cmake like this to configure:
10+
# cmake . -DDynamoRIO_DIR=DynamoRIO-Windows-3.2.0-3/cmake
11+
if (NOT DEFINED DynamoRIO_DIR)
12+
set(DynamoRIO_DIR "${PROJECT_SOURCE_DIR}/../cmake" CACHE PATH
13+
"DynamoRIO installation's cmake directory")
14+
endif (NOT DEFINED DynamoRIO_DIR)
15+
find_package(DynamoRIO 3.1)
16+
if (NOT DynamoRIO_FOUND)
17+
message(FATAL_ERROR "DynamoRIO package required to build")
18+
endif(NOT DynamoRIO_FOUND)
19+
20+
# Make some shared library CMake targets and configure them to be DR clients.
21+
add_library(bbcount SHARED bbcount.c)
22+
configure_DynamoRIO_client(bbcount)
23+
24+
add_library(tracecount SHARED tracecount.c)
25+
configure_DynamoRIO_client(tracecount)

bbcount.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* **********************************************************
2+
* Copyright (c) 2012 Google, Inc. All rights reserved.
3+
* **********************************************************/
4+
5+
/* Basic block count. */
6+
7+
#include "dr_api.h"
8+
9+
/* Forward decls. */
10+
static void event_exit(void);
11+
12+
static dr_emit_flags_t
13+
event_basic_block(void *drcontext, void *tag,
14+
instrlist_t *bb, bool for_trace,
15+
bool translating);
16+
17+
/* Exported entry point. */
18+
DR_EXPORT void
19+
dr_init(client_id_t id)
20+
{
21+
/* Can't use libc printf: conflict with app! */
22+
dr_printf("Client 'bbcount' initializing\n");
23+
/* Register events. */
24+
dr_register_bb_event(event_basic_block);
25+
dr_register_exit_event(event_exit);
26+
}
27+
28+
/************************************************************
29+
* Basic block counting.
30+
*/
31+
32+
static uint bbs_instrumented;
33+
static uint bbs_executed;
34+
35+
/* Basic block execution callback, aka Pin's "analysis" callback. */
36+
static void bb_count(void) { bbs_executed++; }
37+
38+
/* Basic block instrumentation callback. */
39+
static dr_emit_flags_t
40+
event_basic_block(void *drcontext, void *tag, instrlist_t *bb,
41+
bool for_trace, bool translating)
42+
{
43+
/* bb event called again for tracing and translating, don't count those. */
44+
if (!for_trace && !translating)
45+
bbs_instrumented++;
46+
dr_insert_clean_call(drcontext, bb, instrlist_first(bb),
47+
(void *)bb_count, false/*fpstate*/, 0/*num args*/);
48+
return DR_EMIT_DEFAULT;
49+
}
50+
51+
static void
52+
event_exit(void)
53+
{
54+
dr_messagebox("Instrumentation results:\n"
55+
"%10u bbs instrumented\n"
56+
"%10u bbs executed\n",
57+
bbs_instrumented,
58+
bbs_executed);
59+
}

cgo_example.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
TODO: practice on Windows laptop
2+
3+
- Download Windows zip package
4+
- unzip and chmod
5+
- Plain DR usage:
6+
- drrun ls
7+
- drrun calc
8+
- drrun -debug -loglevel 3 ls
9+
- search "interp: start" to see app code
10+
- speed run: drrun bzip2 ls.exe log (~3 MB)
11+
- if time, compress thread log (~27 MB)
12+
- Developing a client:
13+
- open CMakeLists.txt
14+
- open bbcount.c in Vim
15+
- dr_init entry point, registers events, can't use libc (much)
16+
- event_basic_block, instrumentation vs. execution
17+
- event_exit, use results
18+
- open tracecount.c in Vim
19+
- ditto

tracecount.c

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* **********************************************************
2+
* Copyright (c) 2012 Google, Inc. All rights reserved.
3+
* **********************************************************/
4+
5+
/* Trace and basic block count. */
6+
7+
#include "dr_api.h"
8+
9+
/* Forward decls. */
10+
static void event_exit(void);
11+
12+
static dr_emit_flags_t
13+
event_basic_block(void *drcontext, void *tag,
14+
instrlist_t *bb, bool for_trace,
15+
bool translating);
16+
17+
static dr_emit_flags_t
18+
event_trace(void *drcontext, void *tag, instrlist_t *trace,
19+
bool translating);
20+
21+
/* Exported entry point. */
22+
DR_EXPORT void
23+
dr_init(client_id_t id)
24+
{
25+
/* Can't use libc printf: conflict with app! */
26+
dr_printf("Client 'bbcount' initializing\n");
27+
/* Register events. */
28+
dr_register_bb_event(event_basic_block);
29+
dr_register_trace_event(event_trace);
30+
dr_register_exit_event(event_exit);
31+
}
32+
33+
/************************************************************
34+
* Basic block counting.
35+
*/
36+
37+
static uint bbs_instrumented;
38+
static uint bbs_executed;
39+
40+
/* Basic block execution callback, aka Pin's "analysis" callback. */
41+
static void bb_count(void) { bbs_executed++; }
42+
43+
/* Basic block instrumentation callback. */
44+
static dr_emit_flags_t
45+
event_basic_block(void *drcontext, void *tag, instrlist_t *bb,
46+
bool for_trace, bool translating)
47+
{
48+
/* bb event called again for tracing and translating, don't count those. */
49+
if (!for_trace && !translating)
50+
bbs_instrumented++;
51+
if (!for_trace)
52+
dr_insert_clean_call(drcontext, bb, instrlist_first(bb),
53+
(void *)bb_count, false/*fpstate*/, 0/*num args*/);
54+
return DR_EMIT_DEFAULT;
55+
}
56+
57+
/************************************************************
58+
* Trace counting.
59+
*/
60+
61+
static uint traces_instrumented;
62+
static uint traces_executed;
63+
64+
/* Trace execution callback, aka Pin's "analysis" callback. */
65+
static void trace_count(void) { traces_executed++; }
66+
67+
/* Trace instrumentation callback. */
68+
static dr_emit_flags_t
69+
event_trace(void *drcontext, void *tag, instrlist_t *trace, bool translating)
70+
{
71+
/* trace event called again for translating, don't count those. */
72+
if (!translating)
73+
traces_instrumented++;
74+
dr_insert_clean_call(drcontext, trace, instrlist_first(trace),
75+
(void *)trace_count, false/*fpstate*/, 0/*num args*/);
76+
return DR_EMIT_DEFAULT;
77+
}
78+
79+
static void
80+
event_exit(void)
81+
{
82+
dr_messagebox("Instrumentation results:\n"
83+
"%10u bbs instrumented\n"
84+
"%10u bbs executed\n"
85+
"%10u traces instrumented\n"
86+
"%10u traces executed\n",
87+
bbs_instrumented,
88+
bbs_executed,
89+
traces_instrumented,
90+
traces_executed);
91+
}

0 commit comments

Comments
 (0)