Skip to content

Commit

Permalink
Add dr_vfprintf (#2823)
Browse files Browse the repository at this point in the history
Adds a new API routine dr_vfprintf().
Adds a sanity check test.
  • Loading branch information
percontation authored and derekbruening committed Feb 10, 2018
1 parent 761e88e commit cb09eec
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/docs/release.dox
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Further non-compatibility-affecting changes include:
- Added hashtable_apply_to_all_payloads() to iterate over all payloads in a
hashtable.
- Added drutil_insert_get_mem_addr_ex().
- Added dr_vfprintf().

**************************************************
<hr>
Expand Down
16 changes: 12 additions & 4 deletions core/lib/instrument.c
Original file line number Diff line number Diff line change
Expand Up @@ -4547,11 +4547,9 @@ dr_printf(const char *fmt, ...)
}

DR_API ssize_t
dr_fprintf(file_t f, const char *fmt, ...)
dr_vfprintf(file_t f, const char *fmt, va_list ap)
{
ssize_t written;
va_list ap;
va_start(ap, fmt);
#ifdef WINDOWS
if ((f == STDOUT || f == STDERR) && print_to_console) {
written = dr_write_to_console(f == STDOUT, fmt, ap);
Expand All @@ -4560,10 +4558,20 @@ dr_fprintf(file_t f, const char *fmt, ...)
} else
#endif
written = do_file_write(f, fmt, ap);
va_end(ap);
return written;
}

DR_API ssize_t
dr_fprintf(file_t f, const char *fmt, ...)
{
va_list ap;
ssize_t res;
va_start(ap, fmt);
res = dr_vfprintf(f, fmt, ap);
va_end(ap);
return res;
}

DR_API int
dr_snprintf(char *buf, size_t max, const char *fmt, ...)
{
Expand Down
7 changes: 7 additions & 0 deletions core/lib/instrument_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -4149,6 +4149,13 @@ DR_API
ssize_t
dr_fprintf(file_t f, const char *fmt, ...);

DR_API
/**
* Identical to dr_fprintf() but exposes va_list.
*/
ssize_t
dr_vfprintf(file_t f, const char *fmt, va_list ap);

#ifdef WINDOWS
DR_API
/**
Expand Down
30 changes: 30 additions & 0 deletions suite/tests/client-interface/file_io.dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static void test_dir(void);
static void test_relative(void);
static void test_map_exe(void);
static void test_times(void);
static void test_vfprintf(void);

byte *
find_prot_edge(const byte *start, uint prot_flag)
Expand Down Expand Up @@ -302,6 +303,8 @@ void dr_init(client_id_t id)
test_map_exe();

test_times();

test_vfprintf();
}

/* Creates a closed, unique temporary file and returns its filename.
Expand Down Expand Up @@ -491,3 +494,30 @@ test_times(void)
*/
dr_get_time(&drtime);
}

static void
test_vfprintf_helper(file_t f, const char *fmt, ...)
{
va_list ap;
ssize_t len1, len2;

va_start(ap, fmt);
len1 = dr_vfprintf(f, fmt, ap);
va_end(ap);

/* check length consistency, because why not. */
char buf[100];
va_start(ap, fmt);
len2 = dr_vsnprintf(buf, BUFFER_SIZE_ELEMENTS(buf), fmt, ap);
va_end(ap);

if (len1 != len2 && !(len1 > BUFFER_SIZE_ELEMENTS(buf) && len2 == -1)) {
dr_fprintf(STDERR, "dr_vfprintf and dr_vsnprintf disagree.\n");
}
}

static void
test_vfprintf(void)
{
test_vfprintf_helper(STDERR, "vfprintf check: %d\n", 1234);
}
1 change: 1 addition & 0 deletions suite/tests/client-interface/file_io.expect
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ read_only_buf is r
dr_safe_read() check
DR_TRY_EXCEPT check
dr_safe_write() check
vfprintf check: 1234
file separation check
float i/o test: 3.1416
done

0 comments on commit cb09eec

Please sign in to comment.