Skip to content

Commit

Permalink
i#2006 generalize drcachesim: add drmodtrack_dump_buf
Browse files Browse the repository at this point in the history
Adds drmodtrack_dump_buf to dump module info string into a provided
buffer.

Review-URL: https://codereview.appspot.com/316770043
  • Loading branch information
zhaoqin committed Oct 23, 2016
1 parent 5d3c252 commit 19fe03b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 16 deletions.
10 changes: 10 additions & 0 deletions ext/drcovlib/drcovlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef enum {
DRCOVLIB_ERROR_INVALID_SETUP, /**< Operation failed: invalid DynamoRIO setup */
DRCOVLIB_ERROR_FEATURE_NOT_AVAILABLE, /**< Operation failed: not available */
DRCOVLIB_ERROR_NOT_FOUND, /**< Operation failed: query not found. */
DRCOVLIB_ERROR_BUF_TOO_SMALL, /**< Operation failed: buffer too small. */
} drcovlib_status_t;

/** Bitmask flags for use in #drcovlib_options_t.flags. */
Expand Down Expand Up @@ -233,6 +234,15 @@ DR_EXPORT
drcovlib_status_t
drmodtrack_dump(file_t file);

DR_EXPORT
/**
* Writes the complete module information string to \p buf.
* Returns DRCOVLIB_SUCCESS on success.
* If the buffer is too small, returns DRCOVLIB_ERROR_BUF_TOO_SMALL.
*/
drcovlib_status_t
drmodtrack_dump_buf(char *buf, size_t size);

DR_EXPORT
/**
* Cleans up the module tracking state.
Expand Down
101 changes: 85 additions & 16 deletions ext/drcovlib/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,50 +307,119 @@ typedef struct _module_read_info_t {
} module_read_info_t;

/* assuming caller holds the lock */
static void
module_table_entry_print(module_entry_t *entry, file_t log)
static int
module_table_entry_print(module_entry_t *entry, char *buf, size_t size)
{
const char *name;
module_data_t *data;
const char *full_path = "<unknown>";
int len, total_len = 0;
data = entry->data;
name = dr_module_preferred_name(data);
if (data->full_path != NULL && data->full_path[0] != '\0')
full_path = data->full_path;

dr_fprintf(log, "%3u, "PFX", "PFX", "PFX"",
entry->id, data->start, data->end, data->entry_point);
len = dr_snprintf(buf, size, "%3u, " PFX ", " PFX ", " PFX "",
entry->id, data->start, data->end, data->entry_point);
if (len == -1)
return -1;
buf += len;
total_len += len;
size -= len;
#ifdef WINDOWS
dr_fprintf(log, ", 0x%08x, 0x%08x", data->checksum, data->timestamp);
len = dr_snprintf(buf, size, ", 0x%08x, 0x%08x",
data->checksum, data->timestamp);
if (len == -1)
return -1;
buf += len;
total_len += len;
size -= len;
#endif
dr_fprintf(log, ", %s\n", full_path);
len = dr_snprintf(buf, size, ", %s\n", full_path);
if (len == -1)
return -1;
buf += len;
total_len += len;
size -= len;
return total_len;
}

drcovlib_status_t
drmodtrack_dump(file_t log)
{
drmodtrack_dump_buf(char *buf, size_t size) {
uint i;
module_entry_t *entry;
if (log == INVALID_FILE)
int len;
if (buf == NULL || size == 0)
return DRCOVLIB_ERROR_INVALID_PARAMETER;
size--; /* for the terminating null character */
drvector_lock(&module_table.vector);
dr_fprintf(log, "Module Table: version %u, count %u\n", MODULE_FILE_VERSION,
module_table.vector.entries);
len = dr_snprintf(buf, size, "Module Table: version %u, count %u\n",
MODULE_FILE_VERSION, module_table.vector.entries);
if (len == -1) {
drvector_unlock(&module_table.vector);
return DRCOVLIB_ERROR_BUF_TOO_SMALL;
}
buf += len;
size -= len;

dr_fprintf(log, "Columns: id, base, end, entry");
len = dr_snprintf(buf, size, "Columns: id, base, end, entry");
if (len == -1) {
drvector_unlock(&module_table.vector);
return DRCOVLIB_ERROR_BUF_TOO_SMALL;
}
#ifdef WINDOWS
dr_fprintf(log, ", checksum, timestamp");
buf += len;
size -= len;

len = dr_snprintf(buf, size, ", checksum, timestamp");
if (len == -1) {
drvector_unlock(&module_table.vector);
return DRCOVLIB_ERROR_BUF_TOO_SMALL;
}
#endif
dr_fprintf(log, ", path\n");

buf += len;
size -= len;
len = dr_snprintf(buf, size, ", path\n");
if (len == -1) {
drvector_unlock(&module_table.vector);
return DRCOVLIB_ERROR_BUF_TOO_SMALL;
}

buf += len;
size -= len;
for (i = 0; i < module_table.vector.entries; i++) {
entry = drvector_get_entry(&module_table.vector, i);
module_table_entry_print(entry, log);
}
len = module_table_entry_print(entry, buf, size);
if (len == -1) {
drvector_unlock(&module_table.vector);
return DRCOVLIB_ERROR_BUF_TOO_SMALL;
}
buf += len;
size -= len;
}
buf[0] = '\0';
drvector_unlock(&module_table.vector);
return DRCOVLIB_SUCCESS;
}

drcovlib_status_t
drmodtrack_dump(file_t log)
{
drcovlib_status_t res;
size_t size = 200 + module_table.vector.entries * (MAXIMUM_PATH + 40);
char *buf;
do {
buf = dr_global_alloc(size);
res = drmodtrack_dump_buf(buf, size);
if (res == DRCOVLIB_SUCCESS)
dr_write_file(log, buf, strlen(buf));
dr_global_free(buf, size);
size *= 2;
} while (res == DRCOVLIB_ERROR_BUF_TOO_SMALL);
return res;
}

static inline const char *
move_to_next_line(const char *ptr)
{
Expand Down

0 comments on commit 19fe03b

Please sign in to comment.