DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (409f3966645a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsCocoaDebugUtils.h"

#include <pthread.h>
#include <libproc.h>
#include <stdarg.h>
#include <time.h>
#include <execinfo.h>
#include <asl.h>

static char gProcPath[PROC_PIDPATHINFO_MAXSIZE] = {0};
static char gBundleID[MAXPATHLEN] = {0};

static void MaybeGetPathAndID()
{
  if (!gProcPath[0]) {
    proc_pidpath(getpid(), gProcPath, sizeof(gProcPath));
  }
  if (!gBundleID[0]) {
    // Apple's CFLog() uses "com.apple.console" (in its call to asl_open()) if
    // it can't find the bundle id.
    CFStringRef bundleID = NULL;
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    if (mainBundle) {
      bundleID = CFBundleGetIdentifier(mainBundle);
    }
    if (!bundleID) {
      strcpy(gBundleID, "com.apple.console");
    } else {
      CFStringGetCString(bundleID, gBundleID, sizeof(gBundleID),
                         kCFStringEncodingUTF8);
    }
  }
}

static void GetThreadName(char* aName, size_t aSize)
{
  pthread_getname_np(pthread_self(), aName, aSize);
}

void
nsCocoaDebugUtils::DebugLog(const char* aFormat, ...)
{
  va_list args;
  va_start(args, aFormat);
  CFStringRef formatCFSTR =
    CFStringCreateWithCString(kCFAllocatorDefault, aFormat,
                              kCFStringEncodingUTF8);
  DebugLogV(true, formatCFSTR, args);
  CFRelease(formatCFSTR);
  va_end(args);
}

void
nsCocoaDebugUtils::DebugLogInt(bool aDecorate, const char* aFormat, ...)
{
  va_list args;
  va_start(args, aFormat);
  CFStringRef formatCFSTR =
    CFStringCreateWithCString(kCFAllocatorDefault, aFormat,
                              kCFStringEncodingUTF8);
  DebugLogV(aDecorate, formatCFSTR, args);
  CFRelease(formatCFSTR);
  va_end(args);
}

void
nsCocoaDebugUtils::DebugLogV(bool aDecorate, CFStringRef aFormat,
                             va_list aArgs)
{
  MaybeGetPathAndID();

  CFStringRef message =
    CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, NULL,
                                         aFormat, aArgs);

  int msgLength =
    CFStringGetMaximumSizeForEncoding(CFStringGetLength(message),
                                      kCFStringEncodingUTF8);
  char* msgUTF8 = (char*) calloc(msgLength, 1);
  CFStringGetCString(message, msgUTF8, msgLength, kCFStringEncodingUTF8);
  CFRelease(message);

  int finishedLength = msgLength + PROC_PIDPATHINFO_MAXSIZE;
  char* finished = (char*) calloc(finishedLength, 1);
  const time_t currentTime = time(NULL);
  char timestamp[30] = {0};
  ctime_r(&currentTime, timestamp);
  if (aDecorate) {
    char threadName[MAXPATHLEN] = {0};
    GetThreadName(threadName, sizeof(threadName));
    snprintf(finished, finishedLength, "(%s) %s[%u] %s[%p] %s\n",
            timestamp, gProcPath, getpid(), threadName, pthread_self(), msgUTF8);
  } else {
    snprintf(finished, finishedLength, "%s\n", msgUTF8);
  }
  free(msgUTF8);

  fputs(finished, stdout);

  // Use the Apple System Log facility, as NSLog and CFLog do.
  aslclient asl = asl_open(NULL, gBundleID, ASL_OPT_NO_DELAY);
  aslmsg msg = asl_new(ASL_TYPE_MSG);
  asl_set(msg, ASL_KEY_LEVEL, "4"); // kCFLogLevelWarning, used by NSLog()
  asl_set(msg, ASL_KEY_MSG, finished);
  asl_send(asl, msg);
  asl_free(msg);
  asl_close(asl);

  free(finished);
}

CSTypeRef
nsCocoaDebugUtils::sInitializer = {0};

CSSymbolicatorRef
nsCocoaDebugUtils::sSymbolicator = {0};

#define STACK_MAX 256

void
nsCocoaDebugUtils::PrintStackTrace()
{
  void** addresses = (void**) calloc(STACK_MAX, sizeof(void*));
  if (!addresses) {
    return;
  }

  CSSymbolicatorRef symbolicator = GetSymbolicatorRef();
  if (CSIsNull(symbolicator)) {
    free(addresses);
    return;
  }

  uint32_t count = backtrace(addresses, STACK_MAX);
  for (uint32_t i = 0; i < count; ++i) {
    PrintAddress(addresses[i]);
  }

  ReleaseSymbolicator();
  free(addresses);
}

void
nsCocoaDebugUtils::PrintAddress(void* aAddress)
{
  const char* ownerName = "unknown";
  const char* addressString = "unknown + 0";

  char* allocatedOwnerName = nullptr;
  char* allocatedAddressString = nullptr;

  CSSymbolOwnerRef owner = {0};
  CSSymbolicatorRef symbolicator = GetSymbolicatorRef();

  if (!CSIsNull(symbolicator)) {
    owner =
      CSSymbolicatorGetSymbolOwnerWithAddressAtTime(symbolicator,
                                                    (unsigned long long) aAddress,
                                                    kCSNow);
  }
  if (!CSIsNull(owner)) {
    ownerName = allocatedOwnerName = GetOwnerNameInt(aAddress, owner);
    addressString = allocatedAddressString = GetAddressStringInt(aAddress, owner);
  }
  DebugLogInt(false, "    (%s) %s", ownerName, addressString);

  free(allocatedOwnerName);
  free(allocatedAddressString);

  ReleaseSymbolicator();
}

char*
nsCocoaDebugUtils::GetOwnerName(void* aAddress)
{
  return GetOwnerNameInt(aAddress);
}

char*
nsCocoaDebugUtils::GetOwnerNameInt(void* aAddress, CSTypeRef aOwner)
{
  char* retval = (char*) calloc(MAXPATHLEN, 1);

  const char* ownerName = "unknown";

  CSSymbolicatorRef symbolicator = GetSymbolicatorRef();
  CSTypeRef owner = aOwner;

  if (CSIsNull(owner) && !CSIsNull(symbolicator)) {
    owner =
      CSSymbolicatorGetSymbolOwnerWithAddressAtTime(symbolicator,
                                                    (unsigned long long) aAddress,
                                                    kCSNow);
  }

  if (!CSIsNull(owner)) {
    ownerName = CSSymbolOwnerGetName(owner);
  }

  snprintf(retval, MAXPATHLEN, "%s", ownerName);
  ReleaseSymbolicator();

  return retval;
}

char*
nsCocoaDebugUtils::GetAddressString(void* aAddress)
{
  return GetAddressStringInt(aAddress);
}

char*
nsCocoaDebugUtils::GetAddressStringInt(void* aAddress, CSTypeRef aOwner)
{
  char* retval = (char*) calloc(MAXPATHLEN, 1);

  const char* addressName = "unknown";
  unsigned long long addressOffset = 0;

  CSSymbolicatorRef symbolicator = GetSymbolicatorRef();
  CSTypeRef owner = aOwner;

  if (CSIsNull(owner) && !CSIsNull(symbolicator)) {
    owner =
      CSSymbolicatorGetSymbolOwnerWithAddressAtTime(symbolicator,
                                                    (unsigned long long) aAddress,
                                                    kCSNow);
  }

  if (!CSIsNull(owner)) {
    CSSymbolRef symbol =
      CSSymbolOwnerGetSymbolWithAddress(owner,
                                        (unsigned long long) aAddress);
    if (!CSIsNull(symbol)) {
      addressName = CSSymbolGetName(symbol);
      CSRange range = CSSymbolGetRange(symbol);
      addressOffset = (unsigned long long) aAddress - range.location;
    } else {
      addressOffset = (unsigned long long)
        aAddress - CSSymbolOwnerGetBaseAddress(owner);
    }
  }

  snprintf(retval, MAXPATHLEN, "%s + 0x%llx",
           addressName, addressOffset);
  ReleaseSymbolicator();

  return retval;
}

CSSymbolicatorRef
nsCocoaDebugUtils::GetSymbolicatorRef()
{
  if (CSIsNull(sSymbolicator)) {
    // 0x40e0000 is the value returned by
    // uint32_t CSSymbolicatorGetFlagsForNListOnlyData(void).  We don't use
    // this method directly because it doesn't exist on OS X 10.6.  Unless
    // we limit ourselves to NList data, it will take too long to get a
    // stack trace where Dwarf debugging info is available (about 15 seconds
    // with Firefox).  This means we won't be able to get a CSSourceInfoRef,
    // or line number information.  Oh well.
    sSymbolicator =
      CSSymbolicatorCreateWithPidFlagsAndNotification(getpid(),
                                                      0x40e0000, 0);
  }
  // Retaining just after creation prevents crashes when calling symbolicator
  // code (for example from PrintStackTrace()) as Firefox is quitting.  Not
  // sure why.  Doing this may mean that we leak sSymbolicator on quitting
  // (if we ever created it).  No particular harm in that, though.
  return CSRetain(sSymbolicator);
}

void
nsCocoaDebugUtils::ReleaseSymbolicator()
{
  if (!CSIsNull(sSymbolicator)) {
    CSRelease(sSymbolicator);
  }
}