summaryrefslogtreecommitdiff
path: root/lib/xray/xray_inmemory_log.cc
diff options
context:
space:
mode:
authorDean Michael Berris <dberris@google.com>2017-01-03 04:35:24 +0000
committerDean Michael Berris <dberris@google.com>2017-01-03 04:35:24 +0000
commit55278303a867879391300d56e1a390b7db57e689 (patch)
tree1bdf91d720f19e5ac4c43ab42708b4930de50e61 /lib/xray/xray_inmemory_log.cc
parentb92697bddf3ec2631db9e850b6a885636748c08f (diff)
[XRay] [compiler-rt] Include argv[0] in the log file name.
Summary: If you decide to recompile parts of your Linux distro with XRay, it may be useful to know which trace belongs to which binary. While there, get rid of the incorrect strncat() usage; it always returns a pointer to the start which makes that if() always true. Replace with snprintf which is bounded so that enough from both strings fits nicely. Reviewers: dberris Subscribers: danalbert, srhines, kubabrecka, mgorny, llvm-commits Differential Revision: https://reviews.llvm.org/D27912 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@290861 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/xray/xray_inmemory_log.cc')
-rw-r--r--lib/xray/xray_inmemory_log.cc21
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/xray/xray_inmemory_log.cc b/lib/xray/xray_inmemory_log.cc
index 7ec56f486..adcb21671 100644
--- a/lib/xray/xray_inmemory_log.cc
+++ b/lib/xray/xray_inmemory_log.cc
@@ -112,14 +112,23 @@ static int __xray_OpenLogFile() XRAY_NEVER_INSTRUMENT {
// Open a temporary file once for the log.
static char TmpFilename[256] = {};
static char TmpWildcardPattern[] = "XXXXXX";
- auto E = internal_strncat(TmpFilename, flags()->xray_logfile_base,
- sizeof(TmpFilename) - 10);
- if (static_cast<size_t>((E + 6) - TmpFilename) > (sizeof(TmpFilename) - 1)) {
- Report("XRay log file base too long: %s\n", flags()->xray_logfile_base);
+ auto Argv = GetArgv();
+ const char *Progname = Argv[0] == nullptr ? "(unknown)" : Argv[0];
+ const char *LastSlash = internal_strrchr(Progname, '/');
+
+ if (LastSlash != nullptr)
+ Progname = LastSlash + 1;
+
+ const int HalfLength = sizeof(TmpFilename) / 2 - sizeof(TmpWildcardPattern);
+ int NeededLength = internal_snprintf(TmpFilename, sizeof(TmpFilename),
+ "%.*s%.*s.%s",
+ HalfLength, flags()->xray_logfile_base,
+ HalfLength, Progname,
+ TmpWildcardPattern);
+ if (NeededLength > int(sizeof(TmpFilename))) {
+ Report("XRay log file name too long (%d): %s\n", NeededLength, TmpFilename);
return -1;
}
- internal_strncat(TmpFilename, TmpWildcardPattern,
- sizeof(TmpWildcardPattern) - 1);
int Fd = mkstemp(TmpFilename);
if (Fd == -1) {
Report("XRay: Failed opening temporary file '%s'; not logging events.\n",