[PATCH] D90759: Escape command line arguments in backtraces

Luke Drummond via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 4 04:51:34 PST 2020


ldrumm created this revision.
ldrumm added reviewers: leonardchan, jhenderson.
ldrumm added a project: LLVM.
Herald added subscribers: dexonsmith, JDevlieghere, hiraditya.
ldrumm requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90759

Files:
  llvm/lib/Support/PrettyStackTrace.cpp


Index: llvm/lib/Support/PrettyStackTrace.cpp
===================================================================
--- llvm/lib/Support/PrettyStackTrace.cpp
+++ 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,15 @@
 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 (HaveSpace)
+      OS << '"';
+    OS.write_escaped(ArgV[I]);
+    if (HaveSpace)
+      OS << '"';
+    OS << ' ';
+  }
   OS << '\n';
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90759.302814.patch
Type: text/x-patch
Size: 806 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201104/be621502/attachment.bin>


More information about the llvm-commits mailing list