aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/PrettyStackTrace.cpp
diff options
context:
space:
mode:
authorLuke Drummond <luke.drummond@codeplay.com>2020-11-04 12:10:25 +0000
committerLuke Drummond <luke.drummond@codeplay.com>2020-11-17 12:16:13 +0000
commit537cbd90c43dd9b166fa8f03347b1397505140e5 (patch)
tree135880f2679513bb4bfe8ba18efdb735fbee1fc0 /llvm/lib/Support/PrettyStackTrace.cpp
parentfcca6fe93f04f50c302f9ce2d1bad8f5f3fa369a (diff)
downloadllvm-537cbd90c43dd9b166fa8f03347b1397505140e5.zip
llvm-537cbd90c43dd9b166fa8f03347b1397505140e5.tar.gz
llvm-537cbd90c43dd9b166fa8f03347b1397505140e5.tar.bz2
Escape command line arguments in backtraces
A common routine is to have the compiler crash, and attempt to rerun the cc1 command-line by copying and pasting the arguments printed by `llvm::Support::PrettyStackProgram::print`. However, these arguments are not quoted or escaped which means they must be manually edited before working correctly. This patch ensures that shell-unfriendly characters are C-escaped, and arguments with spaces are double-quoted reducing the frustration of running cc1 inside a debugger. As the quoting is C, this is "best effort for most shells", but should be fine for at least bash, zsh, csh, and cmd.exe. Reviewed by: jhenderson Differential Revision: https://reviews.llvm.org/D90759
Diffstat (limited to 'llvm/lib/Support/PrettyStackTrace.cpp')
-rw-r--r--llvm/lib/Support/PrettyStackTrace.cpp13
1 files changed, 11 insertions, 2 deletions
diff --git a/llvm/lib/Support/PrettyStackTrace.cpp b/llvm/lib/Support/PrettyStackTrace.cpp
index 9072f9d2..5d3d95b 100644
--- a/llvm/lib/Support/PrettyStackTrace.cpp
+++ b/llvm/lib/Support/PrettyStackTrace.cpp
@@ -25,6 +25,7 @@
#include <cassert>
#include <cstdarg>
#include <cstdio>
+#include <cstring>
#include <tuple>
#ifdef HAVE_CRASHREPORTERCLIENT_H
@@ -253,8 +254,16 @@ void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
void PrettyStackTraceProgram::print(raw_ostream &OS) const {
OS << "Program arguments: ";
// Print the argument list.
- for (unsigned i = 0, e = ArgC; i != e; ++i)
- OS << ArgV[i] << ' ';
+ for (int I = 0; I < ArgC; ++I) {
+ const bool HaveSpace = ::strchr(ArgV[I], ' ');
+ if (I)
+ OS << ' ';
+ if (HaveSpace)
+ OS << '"';
+ OS.write_escaped(ArgV[I]);
+ if (HaveSpace)
+ OS << '"';
+ }
OS << '\n';
}