debug.c++ 7.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Kenton Varda's avatar
Kenton Varda committed
24
#include "debug.h"
25 26
#include <stdlib.h>
#include <ctype.h>
27 28
#include <string.h>
#include <errno.h>
29

30
namespace kj {
Kenton Varda's avatar
Kenton Varda committed
31
namespace _ {  // private
32

Kenton Varda's avatar
Kenton Varda committed
33
Debug::Severity Debug::minSeverity = Debug::Severity::WARNING;
34

Kenton Varda's avatar
Kenton Varda committed
35
ArrayPtr<const char> KJ_STRINGIFY(Debug::Severity severity) {
36 37 38 39
  static const char* SEVERITY_STRINGS[] = {
    "info",
    "warning",
    "error",
40 41
    "fatal",
    "debug"
42 43 44 45 46 47
  };

  const char* s = SEVERITY_STRINGS[static_cast<uint>(severity)];
  return arrayPtr(s, strlen(s));
}

48 49 50 51 52 53 54 55
namespace {

enum DescriptionStyle {
  LOG,
  ASSERTION,
  SYSCALL
};

Kenton Varda's avatar
Kenton Varda committed
56 57
static String makeDescription(DescriptionStyle style, const char* code, int errorNumber,
                              const char* macroArgs, ArrayPtr<String> argValues) {
58
  KJ_STACK_ARRAY(ArrayPtr<const char>, argNames, argValues.size(), 8, 64);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  if (argValues.size() > 0) {
    size_t index = 0;
    const char* start = macroArgs;
    while (isspace(*start)) ++start;
    const char* pos = start;
    uint depth = 0;
    bool quoted = false;
    while (char c = *pos++) {
      if (quoted) {
        if (c == '\\' && *pos != '\0') {
          ++pos;
        } else if (c == '\"') {
          quoted = false;
        }
      } else {
        if (c == '(') {
          ++depth;
        } else if (c == ')') {
          --depth;
        } else if (c == '\"') {
          quoted = true;
        } else if (c == ',' && depth == 0) {
          if (index < argValues.size()) {
            argNames[index] = arrayPtr(start, pos - 1);
          }
          ++index;
          while (isspace(*pos)) ++pos;
          start = pos;
        }
      }
    }
    if (index < argValues.size()) {
      argNames[index] = arrayPtr(start, pos - 1);
    }
    ++index;

    if (index != argValues.size()) {
97 98
      getExceptionCallback().logMessage(__FILE__, __LINE__, 0,
          str("Failed to parse logging macro args into ",
99 100 101 102
              argValues.size(), " names: ", macroArgs, '\n'));
    }
  }

103 104 105 106 107 108 109 110 111 112 113 114
  if (style == SYSCALL) {
    // Strip off leading "foo = " from code, since callers will sometimes write things like:
    //   ssize_t n;
    //   RECOVERABLE_SYSCALL(n = read(fd, buffer, sizeof(buffer))) { return ""; }
    //   return std::string(buffer, n);
    const char* equalsPos = strchr(code, '=');
    if (equalsPos != nullptr && equalsPos[1] != '=') {
      code = equalsPos + 1;
      while (isspace(*code)) ++code;
    }
  }

115 116 117 118
  if (style == ASSERTION && code == nullptr) {
    style = LOG;
  }

119
  {
Kenton Varda's avatar
Kenton Varda committed
120 121 122 123 124
    StringPtr expected = "expected ";
    StringPtr codeArray = style == LOG ? nullptr : StringPtr(code);
    StringPtr sep = " = ";
    StringPtr delim = "; ";
    StringPtr colon = ": ";
125

Kenton Varda's avatar
Kenton Varda committed
126
    StringPtr sysErrorArray;
127 128 129
#if __USE_GNU
    char buffer[256];
    if (style == SYSCALL) {
Kenton Varda's avatar
Kenton Varda committed
130
      sysErrorArray = strerror_r(errorNumber, buffer, sizeof(buffer));
131 132
    }
#else
Kenton Varda's avatar
Kenton Varda committed
133 134 135 136 137
    char buffer[256];
    if (style == SYSCALL) {
      strerror_r(errorNumber, buffer, sizeof(buffer));
      sysErrorArray = buffer;
    }
138
#endif
139 140

    size_t totalSize = 0;
141 142 143 144 145 146 147 148 149
    switch (style) {
      case LOG:
        break;
      case ASSERTION:
        totalSize += expected.size() + codeArray.size();
        break;
      case SYSCALL:
        totalSize += codeArray.size() + colon.size() + sysErrorArray.size();
        break;
150
    }
151

152
    for (size_t i = 0; i < argValues.size(); i++) {
153 154 155
      if (i > 0 || style != LOG) {
        totalSize += delim.size();
      }
156 157 158 159 160 161
      if (argNames[i].size() > 0 && argNames[i][0] != '\"') {
        totalSize += argNames[i].size() + sep.size();
      }
      totalSize += argValues[i].size();
    }

Kenton Varda's avatar
Kenton Varda committed
162 163
    String result = heapString(totalSize);
    char* pos = result.begin();
164

165 166 167 168
    switch (style) {
      case LOG:
        break;
      case ASSERTION:
169
        pos = _::fill(pos, expected, codeArray);
170 171
        break;
      case SYSCALL:
172
        pos = _::fill(pos, codeArray, colon, sysErrorArray);
173
        break;
174 175
    }
    for (size_t i = 0; i < argValues.size(); i++) {
176
      if (i > 0 || style != LOG) {
177
        pos = _::fill(pos, delim);
178
      }
179
      if (argNames[i].size() > 0 && argNames[i][0] != '\"') {
180
        pos = _::fill(pos, argNames[i], sep);
181
      }
182
      pos = _::fill(pos, argValues[i]);
183 184
    }

Kenton Varda's avatar
Kenton Varda committed
185
    return result;
186 187 188
  }
}

189 190
}  // namespace

Kenton Varda's avatar
Kenton Varda committed
191 192
void Debug::logInternal(const char* file, int line, Severity severity, const char* macroArgs,
                        ArrayPtr<String> argValues) {
193 194
  getExceptionCallback().logMessage(file, line, 0,
      str(severity, ": ", makeDescription(LOG, nullptr, 0, macroArgs, argValues), '\n'));
195 196
}

Kenton Varda's avatar
Kenton Varda committed
197
Debug::Fault::~Fault() noexcept(false) {
198 199 200 201 202
  if (exception != nullptr) {
    Exception copy = mv(*exception);
    delete exception;
    getExceptionCallback().onRecoverableException(mv(copy));
  }
203 204
}

Kenton Varda's avatar
Kenton Varda committed
205
void Debug::Fault::fatal() {
206 207 208 209
  Exception copy = mv(*exception);
  delete exception;
  exception = nullptr;
  getExceptionCallback().onFatalException(mv(copy));
210 211 212
  abort();
}

Kenton Varda's avatar
Kenton Varda committed
213
void Debug::Fault::init(
214 215 216 217 218
    const char* file, int line, Exception::Nature nature, int errorNumber,
    const char* condition, const char* macroArgs, ArrayPtr<String> argValues) {
  exception = new Exception(nature, Exception::Durability::PERMANENT, file, line,
      makeDescription(nature == Exception::Nature::OS_ERROR ? SYSCALL : ASSERTION,
                      condition, errorNumber, macroArgs, argValues));
219 220
}

Kenton Varda's avatar
Kenton Varda committed
221
String Debug::makeContextDescriptionInternal(const char* macroArgs, ArrayPtr<String> argValues) {
222
  return makeDescription(LOG, nullptr, 0, macroArgs, argValues);
223 224
}

Kenton Varda's avatar
Kenton Varda committed
225
int Debug::getOsErrorNumber() {
226 227 228 229
  int result = errno;
  return result == EINTR ? -1 : result;
}

Kenton Varda's avatar
Kenton Varda committed
230
Debug::Context::Context(): logged(false) {}
231
Debug::Context::~Context() noexcept(false) {}
232

Kenton Varda's avatar
Kenton Varda committed
233
Debug::Context::Value Debug::Context::ensureInitialized() {
234 235 236 237 238 239 240 241 242
  KJ_IF_MAYBE(v, value) {
    return Value(v->file, v->line, heapString(v->description));
  } else {
    Value result = evaluate();
    value = Value(result.file, result.line, heapString(result.description));
    return result;
  }
}

Kenton Varda's avatar
Kenton Varda committed
243
void Debug::Context::onRecoverableException(Exception&& exception) {
244 245
  Value v = ensureInitialized();
  exception.wrapContext(v.file, v.line, mv(v.description));
Kenton Varda's avatar
Kenton Varda committed
246
  next.onRecoverableException(kj::mv(exception));
247
}
Kenton Varda's avatar
Kenton Varda committed
248
void Debug::Context::onFatalException(Exception&& exception) {
249 250
  Value v = ensureInitialized();
  exception.wrapContext(v.file, v.line, mv(v.description));
Kenton Varda's avatar
Kenton Varda committed
251
  next.onFatalException(kj::mv(exception));
252
}
Kenton Varda's avatar
Kenton Varda committed
253
void Debug::Context::logMessage(const char* file, int line, int contextDepth, String&& text) {
254 255 256 257 258 259 260
  if (!logged) {
    Value v = ensureInitialized();
    next.logMessage(v.file, v.line, 0, str("context: ", mv(v.description), '\n'));
    logged = true;
  }

  next.logMessage(file, line, contextDepth + 1, mv(text));
261 262
}

Kenton Varda's avatar
Kenton Varda committed
263
}  // namespace _ (private)
264
}  // namespace kj