Skip to content

Commit

Permalink
i#4083 func trace: Change the -record_function interface
Browse files Browse the repository at this point in the history
Changes -record_function interface to *not* specify an id.  The tracer
now assigns id's, with separate id's per library!function instance.

Each "library!function" string is written to a new "funclist" file in
the destination directory, with the line # as the id.  Uses the
passed-in file write interface to write the file.

Exposes drmemtrace_get_funclist_path() in the drmemtrace.h interface
and adds a test of it.

A separate change will add a new option "op_funclist_file" and file
reading support for the forthcoming func_view tool.

Issue: #4083
  • Loading branch information
derekbruening committed Feb 28, 2020
1 parent eaa3dee commit 24d93b6
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 63 deletions.
4 changes: 4 additions & 0 deletions api/docs/release.dox
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ changes:
This is a binary compatibility change for the OPSZ_ enum.
- Added a new encoding hint field to #instr_t.
- Added a requirement that a C++11-complient compiler be used with \ref page_droption.
- Changed the syntax of the drcachesim -record_function option. It now takes
pairs instead of triples, with the identifier computed by the tracer.
The tracer writes out a file listing the "library!symbol" functions traced,
with the line ordinal indicating the identifier.

The changes between version \DR_VERSION and 7.1.0 include the following minor
compatibility changes:
Expand Down
26 changes: 14 additions & 12 deletions clients/drcachesim/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,24 @@ droption_t<std::string> op_record_function(
"Record invocations trace for the specified function(s).",
"Record invocations trace for the specified function(s) in the option"
" value. Default value is empty. The value should fit this format:"
" function_name|function_id|func_args_num"
" (e.g., -record_function \"memset|10|3\"). The trace would contain"
" function_name|func_args_num"
" (e.g., -record_function \"memset|3\"). The trace would contain"
" information for function return address, function argument value(s),"
" and function return value. We only record pointer-sized arguments and"
" return value. The trace is labeled with the function_id via an ID entry"
" prior to each set of value entries."
" return values. The trace identifies which function is involved"
" via a numeric ID entry prior to each set of value entries."
" The mapping from numeric ID to library-qualified symbolic name is recorded"
" during tracing in a file \"funclist.log\" which simply lists the names in ID"
" order starting from 0, one name per line."
" If the target function is in the dynamic symbol table, then the function_name"
" should be a mangled name (e.g. \"_Znwm\" for \"operator new\", \"_ZdlPv\" for"
" \"operator delete\"). Otherwise, the function_name should be a demangled name."
" Recording multiple functions can be achieved by using the separator"
" \"" OP_RECORD_FUNC_ITEM_SEP
"\" (e.g., -record_function \"memset|10|3" OP_RECORD_FUNC_ITEM_SEP
"memcpy|11|3\"), or"
"\" (e.g., -record_function \"memset|3" OP_RECORD_FUNC_ITEM_SEP "memcpy|3\"), or"
" specifying multiple -record_function options (e.g., -record_function"
" \"memset|10|3\" -record_function \"memcpy|11|3\")."
" Note that the provided function id should be unique, and not collide with"
" \"memset|3\" -record_function \"memcpy|3\")."
" Note that the provided function name should be unique, and not collide with"
" existing heap functions (see -record_heap_value) if -record_heap"
" option is enabled.");
droption_t<bool> op_record_heap(
Expand All @@ -433,10 +435,10 @@ droption_t<bool> op_record_heap(
droption_t<std::string> op_record_heap_value(
DROPTION_SCOPE_ALL, "record_heap_value", DROPTION_FLAG_ACCUMULATE,
OP_RECORD_FUNC_ITEM_SEP,
"malloc|0|1" OP_RECORD_FUNC_ITEM_SEP "free|1|1" OP_RECORD_FUNC_ITEM_SEP
"tc_malloc|2|1" OP_RECORD_FUNC_ITEM_SEP "tc_free|3|1" OP_RECORD_FUNC_ITEM_SEP
"__libc_malloc|4|1" OP_RECORD_FUNC_ITEM_SEP "__libc_free|5|1" OP_RECORD_FUNC_ITEM_SEP
"calloc|6|2",
"malloc|1" OP_RECORD_FUNC_ITEM_SEP "free|1" OP_RECORD_FUNC_ITEM_SEP
"tc_malloc|1" OP_RECORD_FUNC_ITEM_SEP "tc_free|1" OP_RECORD_FUNC_ITEM_SEP
"__libc_malloc|1" OP_RECORD_FUNC_ITEM_SEP "__libc_free|1" OP_RECORD_FUNC_ITEM_SEP
"calloc|2",
"Functions recorded by -record_heap",
"Functions recorded by -record_heap. The option value should fit the same"
" format required by -record_function. These functions will not"
Expand Down
30 changes: 29 additions & 1 deletion clients/drcachesim/tests/burst_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
/* Like burst_static we deliberately do not include configure.h here */

#include "dr_api.h"
#include "drmemtrace/drmemtrace.h"
#include <assert.h>
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <stdlib.h>

Expand Down Expand Up @@ -86,6 +89,28 @@ do_some_work(int arg)
return (temp > 0);
}

static void
exit_cb(void *)
{
const char *funclist_path;
drmemtrace_status_t res = drmemtrace_get_funclist_path(&funclist_path);
assert(res == DRMEMTRACE_SUCCESS);
std::ifstream stream(funclist_path);
assert(stream.good());
std::string line;
bool found_malloc = false;
bool found_return_big_value = false;
while (std::getline(stream, line)) {
assert(line.find('!') != std::string::npos);
if (line.find("!return_big_value") != std::string::npos)
found_return_big_value = true;
if (line.find("!malloc") != std::string::npos)
found_malloc = true;
}
assert(found_malloc);
assert(found_return_big_value);
}

int
main(int argc, const char *argv[])
{
Expand All @@ -94,9 +119,12 @@ main(int argc, const char *argv[])
"-stderr_mask 0xc -rstats_to_stderr"
" -client_lib ';;-offline -record_heap"
// Test large values that require two entries.
" -record_function \"malloc|0|1&return_big_value|42|1\"'"))
" -record_function \"malloc|1&return_big_value|1\"'"))
std::cerr << "failed to set env var!\n";

drmemtrace_status_t res = drmemtrace_buffer_handoff(nullptr, exit_cb, nullptr);
assert(res == DRMEMTRACE_SUCCESS);

for (int i = 0; i < 3; i++) {
std::cerr << "pre-DR init\n";
dr_app_setup();
Expand Down
2 changes: 1 addition & 1 deletion clients/drcachesim/tests/offline-burst_malloc.templatex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pre-DR init
Warning: duplicated function id .*
Warning: duplicated function name .*
.*
pre-DR start
pre-DR detach
Expand Down
19 changes: 18 additions & 1 deletion clients/drcachesim/tracer/drmemtrace.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2016-2018 Google, Inc. All rights reserved.
* Copyright (c) 2016-2020 Google, Inc. All rights reserved.
* **********************************************************/

/*
Expand Down Expand Up @@ -213,9 +213,16 @@ drmemtrace_buffer_handoff(drmemtrace_handoff_func_t handoff_func,
* Its creation can be customized using drmemtrace_custom_module_data()
* and then modified before passing to raw2trace via
* drmodtrack_add_custom_data() and drmodtrack_offline_write().
* Use drmemtrace_get_modlist_path() to obtain the full path.
*/
#define DRMEMTRACE_MODULE_LIST_FILENAME "modules.log"

/**
* The name of the file in -offline mode where function tracing names
* are written. Use drmemtrace_get_funclist_path() to obtain the full path.
*/
#define DRMEMTRACE_FUNCTION_MAP_FILENAME "funclist.log"

DR_EXPORT
/**
* Retrieves the full path to the output directory in -offline mode
Expand All @@ -233,6 +240,16 @@ DR_EXPORT
drmemtrace_status_t
drmemtrace_get_modlist_path(OUT const char **path);

DR_EXPORT
/**
* Retrieves the full path to the file in -offline mode where function tracing
* information is written. Each "library!symbol" function that was traced occupies
* one line of the file, with the line number (starting from 0) equal to the
* identifier recorded in the function trace entries.
*/
drmemtrace_status_t
drmemtrace_get_funclist_path(OUT const char **path);

DR_EXPORT
/**
* Adds custom data stored with each module in the module list produced for
Expand Down
Loading

0 comments on commit 24d93b6

Please sign in to comment.