[PATCH] D27912: [XRay] [compiler-rt] Include argv[0] in the log file name.
Dean Michael Berris via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 2 20:46:18 PST 2017
This revision was automatically updated to reflect the committed changes.
Closed by commit rL290861: [XRay] [compiler-rt] Include argv[0] in the log file name. (authored by dberris).
Changed prior to commit:
https://reviews.llvm.org/D27912?vs=82581&id=82836#toc
Repository:
rL LLVM
https://reviews.llvm.org/D27912
Files:
compiler-rt/trunk/lib/xray/xray_inmemory_log.cc
compiler-rt/trunk/test/xray/TestCases/Linux/argv0-log-file-name.cc
Index: compiler-rt/trunk/test/xray/TestCases/Linux/argv0-log-file-name.cc
===================================================================
--- compiler-rt/trunk/test/xray/TestCases/Linux/argv0-log-file-name.cc
+++ compiler-rt/trunk/test/xray/TestCases/Linux/argv0-log-file-name.cc
@@ -0,0 +1,14 @@
+// Check to make sure argv[0] is contained within the (randomised) XRay log file
+// name.
+
+// RUN: %clangxx_xray -std=c++11 %s -o %t
+// RUN: %run %t > xray.log.file.name 2>&1
+// RUN: ls | FileCheck xray.log.file.name
+// RUN: rm xray-log.* xray.log.file.name
+
+#include <cstdio>
+#include <libgen.h>
+
+[[clang::xray_always_instrument]] int main(int argc, char *argv[]) {
+ printf("// CHECK: xray-log.%s.{{.*}}\n", basename(argv[0]));
+}
Index: compiler-rt/trunk/lib/xray/xray_inmemory_log.cc
===================================================================
--- compiler-rt/trunk/lib/xray/xray_inmemory_log.cc
+++ compiler-rt/trunk/lib/xray/xray_inmemory_log.cc
@@ -112,14 +112,23 @@
// 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",
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27912.82836.patch
Type: text/x-patch
Size: 2436 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170103/0e91b4e8/attachment.bin>
More information about the llvm-commits
mailing list