Computer Laboratory

Debug.cpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include "Debug.h"
37 
38 #include <llvm/ADT/Twine.h>
39 #include <llvm/Support/raw_ostream.h>
40 
41 #include <fnmatch.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 
45 using namespace llvm;
46 
47 
48 static bool debugging(StringRef Name) {
49 #ifdef HAVE_ISSETUGID
50  /*
51  * Debugging paths could be more vulnerable to format string problems
52  * than other code; don't allow when running setuid or setgid.
53  */
54  if (issetugid())
55  return 0;
56 #endif
57 
58  // If TESLA_DEBUG isn't set, we don't do any debug printing.
59  const char *rawEnv = getenv("TESLA_DEBUG");
60  if (rawEnv == NULL)
61  return false;
62 
63  // Also don't print anything if TESLA_DEBUG is set to the empty string.
64  std::string env(rawEnv);
65  if (env.empty())
66  return false;
67 
68  // Let e.g. 'tesla.automata' match 'tesla.automata.anything'.
69  if (Name.size() > env.length()
70  and Name.startswith(env)
71  and Name[env.length()] == '.')
72  return true;
73 
74  // Use fnmatch()'s normal wildcard expansion.
75  return (fnmatch(env.c_str(), Name.str().c_str(), 0) == 0);
76 }
77 
78 void tesla::panic(Twine Message, bool PrintStackTrace) {
79  report_fatal_error("TESLA: " + Message, PrintStackTrace);
80 }
81 
82 raw_ostream& tesla::debugs(StringRef DebugModuleName) {
83 #ifndef NDEBUG
84  if (debugging(DebugModuleName)) {
85  static raw_ostream& ErrStream = llvm::errs();
86  return ErrStream;
87  }
88 #endif
89 
90  static raw_null_ostream NullStream;
91  return NullStream;
92 }
93 
94 #ifndef NDEBUG
95 #include <llvm/Support/Signals.h>
96 
97 namespace {
98 
99 class StaticDebugInit {
100 public:
101  StaticDebugInit() {
102  sys::PrintStackTraceOnErrorSignal();
103  }
104 
105 } DebugInit;
106 
107 }
108 #endif