Skip to content

Commit

Permalink
i#2006 generalize drcachesim: add DRX_FILE_SKIP_OPEN for skip file open
Browse files Browse the repository at this point in the history
Adds DRX_FILE_SKIP_OPEN so that drx_open_unique_file/drx_open_unique_appid_file
can skip the file open and return the resulting file name string only.

Adds test for using DRX_FILE_SKIP_OPEN.

Review-URL: https://codereview.appspot.com/311960043
  • Loading branch information
zhaoqin committed Oct 23, 2016
1 parent 19fe03b commit dc73844
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions api/docs/release.dox
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Further non-compatibility-affecting changes include:
modules for post-processing and across library reloads. This is part of
the \p drcovlib Extension. See #drmodtrack_init() and related functions.
- Added drx_open_unique_appid_dir().
- Added #DRX_FILE_SKIP_OPEN.
- Added %[] support to dr_sscanf.
- Added dr_map_executable_file() and dr_unmap_executable_file().
- Added dr_get_microseconds().
Expand Down
14 changes: 9 additions & 5 deletions ext/drx/drx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,17 +1304,21 @@ drx_open_unique_file(const char *dir, const char *prefix, const char *suffix,
uint extra_flags, char *result OUT, size_t result_len)
{
char buf[MAXIMUM_PATH];
file_t f;
file_t f = INVALID_FILE;
int i;
ssize_t len;
for (i = 0; i < 10000; i++) {
len = dr_snprintf(buf, BUFFER_SIZE_ELEMENTS(buf),
"%s%c%s.%04d.%s", dir, DIRSEP, prefix, i, suffix);
"%s%c%s.%04d.%s", dir, DIRSEP, prefix,
(extra_flags == DRX_FILE_SKIP_OPEN) ?
dr_get_random_value(9999) : i,
suffix);
if (len < 0)
return false;
return INVALID_FILE;
NULL_TERMINATE_BUFFER(buf);
f = dr_open_file(buf, DR_FILE_WRITE_REQUIRE_NEW | extra_flags);
if (f != INVALID_FILE) {
if (extra_flags != DRX_FILE_SKIP_OPEN)
f = dr_open_file(buf, DR_FILE_WRITE_REQUIRE_NEW | extra_flags);
if (f != INVALID_FILE || extra_flags == DRX_FILE_SKIP_OPEN) {
if (result != NULL)
dr_snprintf(result, result_len, "%s", buf);
return f;
Expand Down
23 changes: 21 additions & 2 deletions ext/drx/drx.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,31 @@ drx_register_soft_kills(bool (*event_cb)(process_id_t pid, int exit_code));
* LOGGING
*/

/**
* Flag for use with drx_open_unique_file() or drx_open_unique_appid_file()
* in \p extra_flags to skip the file open and get the path string only.
*
* \note This flag value must not conflict with any DR_FILE_* flag value
* used by dr_open_file().
*/
#define DRX_FILE_SKIP_OPEN 0x8000

DR_EXPORT
/**
* Opens a new file with a name constructed from "dir/prefix.xxxx.suffix",
* where xxxx is a 4-digit number incremented until a unique name is found
* that does not collide with any existing file.
*
* Passes \p extra_flags through to the dr_open_file() call.
* Passes \p extra_flags through to the dr_open_file() call if \p extra_flags
* is not DRX_FILE_SKIP_OPEN.
*
* On success, returns the file handle and optionally the resulting path
* in \p result. On failure, returns INVALID_FILE.
*
* Skips dr_open_file() if \p extra_flags is DRX_FILE_SKIP_OPEN.
* Returns INVALID_FILE and optionally the resulting path in \p result.
* Unique name is not guaranteed and xxxx is set randomly.
*
* \note May be called without calling drx_init().
*/
file_t
Expand All @@ -244,11 +258,16 @@ DR_EXPORT
* from dr_get_application_name(). The id portion of the string is from \p id,
* which is meant to be either the process id or the thread id.
*
* Passes \p extra_flags through to the dr_open_file() call.
* Passes \p extra_flags through to the dr_open_file() call if \p extra_flags
* is not DRX_FILE_SKIP_OPEN.
*
* On success, returns the file handle and optionally the resulting path
* in \p result. On failure, returns INVALID_FILE.
*
* Skips dr_open_file() if \p extra_flags is DRX_FILE_SKIP_OPEN.
* Returns INVALID_FILE and optionally the resulting path in \p result.
* Unique name is not guaranteed and xxxx is set randomly.
*
* \note May be called without calling drx_init().
*/
file_t
Expand Down
14 changes: 14 additions & 0 deletions suite/tests/client-interface/drx-test.dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "dr_api.h"
#include "drx.h"
#include "client_tools.h"
#include "string.h"

#define CHECK(x, msg) do { \
if (!(x)) { \
Expand Down Expand Up @@ -113,6 +114,12 @@ test_unique_files(void)
NULL_TERMINATE_BUFFER(cwd);
#endif

f = drx_open_unique_file(cwd, "drx-test", "log",
DRX_FILE_SKIP_OPEN,
buf, BUFFER_SIZE_ELEMENTS(buf));
CHECK(f == INVALID_FILE, "drx_open_unique_file should skip file open");
CHECK(strstr(buf, "drx-test.") != NULL,
"drx_open_unique_file fail to return path string");
f = drx_open_unique_file(cwd, "drx-test", "log",
0, buf, BUFFER_SIZE_ELEMENTS(buf));
CHECK(f != INVALID_FILE, "drx_open_unique_file failed");
Expand All @@ -121,6 +128,13 @@ test_unique_files(void)
res = dr_delete_file(buf);
CHECK(res, "drx_open_unique_file failed");

f = drx_open_unique_appid_file(cwd, 1234, "drx-test", "txt",
DRX_FILE_SKIP_OPEN,
buf, BUFFER_SIZE_ELEMENTS(buf));
CHECK(f == INVALID_FILE, "drx_open_unique_appid_file should skip file open");
CHECK(strstr(buf,
"drx-test.client.drx-test.") != NULL,
"drx_open_unique_appid_file fail to return path string");
f = drx_open_unique_appid_file(cwd, dr_get_process_id(), "drx-test", "txt",
0, buf, BUFFER_SIZE_ELEMENTS(buf));
CHECK(f != INVALID_FILE, "drx_open_unique_appid_file failed");
Expand Down

0 comments on commit dc73844

Please sign in to comment.