From 2e4f27310ac0da3427ff1f091b0a50cacb22188c Mon Sep 17 00:00:00 2001 From: Vladan Djeric Date: Thu, 28 Jun 2012 14:57:52 -0400 Subject: [PATCH] Bug 763138: Telemetry should indicate whether a debugger is attached. r=bsmedberg,smichaud --- toolkit/components/telemetry/TelemetryPing.js | 11 +++++ xpcom/base/nsDebugImpl.cpp | 41 +++++++++++++++++++ xpcom/base/nsIDebug2.idl | 8 +++- 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/toolkit/components/telemetry/TelemetryPing.js b/toolkit/components/telemetry/TelemetryPing.js index b3e67b66b724..33cf5d509044 100644 --- a/toolkit/components/telemetry/TelemetryPing.js +++ b/toolkit/components/telemetry/TelemetryPing.js @@ -65,6 +65,8 @@ const IDLE_TIMEOUT_SECONDS = 5 * 60; var gLastMemoryPoll = null; +let gWasDebuggerAttached = false; + function getLocale() { return Cc["@mozilla.org/chrome/chrome-registry;1"]. getService(Ci.nsIXULChromeRegistry). @@ -121,6 +123,12 @@ function getSimpleMeasurements() { ret.startupInterrupted = new Number(Services.startup.interrupted); + // Update debuggerAttached flag + let debugService = Cc["@mozilla.org/xpcom/debug;1"].getService(Ci.nsIDebug2); + let isDebuggerAttached = debugService.isDebuggerAttached; + gWasDebuggerAttached = gWasDebuggerAttached || isDebuggerAttached; + ret.debuggerAttached = new Number(gWasDebuggerAttached); + ret.js = Cc["@mozilla.org/js/xpc/XPConnect;1"] .getService(Ci.nsIJSEngineTelemetryStats) .telemetryValue; @@ -776,6 +784,9 @@ TelemetryPing.prototype = { case "sessionstore-windows-restored": Services.obs.removeObserver(this, "sessionstore-windows-restored"); this._hasWindowRestoredObserver = false; + // Check whether debugger was attached during startup + let debugService = Cc["@mozilla.org/xpcom/debug;1"].getService(Ci.nsIDebug2); + gWasDebuggerAttached = debugService.isDebuggerAttached; // fall through case "test-gather-startup": this.gatherStartupInformation(); diff --git a/xpcom/base/nsDebugImpl.cpp b/xpcom/base/nsDebugImpl.cpp index d99f390e0c3f..c3a14dfa58fd 100644 --- a/xpcom/base/nsDebugImpl.cpp +++ b/xpcom/base/nsDebugImpl.cpp @@ -43,6 +43,13 @@ #include "nsString.h" #endif +#if defined(XP_MACOSX) +#include +#include +#include +#include +#endif + #include "mozilla/mozalloc_abort.h" static void @@ -136,6 +143,40 @@ nsDebugImpl::GetAssertionCount(PRInt32* aResult) return NS_OK; } +NS_IMETHODIMP +nsDebugImpl::GetIsDebuggerAttached(bool* aResult) +{ + *aResult = false; + +#if defined(XP_WIN) + *aResult = ::IsDebuggerPresent(); +#elif defined(XP_MACOSX) + // Specify the info we're looking for + int mib[4]; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = getpid(); + size_t mibSize = sizeof(mib) / sizeof(int); + + struct kinfo_proc info; + size_t infoSize = sizeof(info); + memset(&info, 0, infoSize); + + if (sysctl(mib, mibSize, &info, &infoSize, NULL, 0)) { + // if the call fails, default to false + *aResult = false; + return NS_OK; + } + + if (info.kp_proc.p_flag & P_TRACED) { + *aResult = true; + } +#endif + + return NS_OK; +} + /* static */ void nsDebugImpl::SetMultiprocessMode(const char *aDesc) { diff --git a/xpcom/base/nsIDebug2.idl b/xpcom/base/nsIDebug2.idl index 63266290aae2..57e17d834a43 100644 --- a/xpcom/base/nsIDebug2.idl +++ b/xpcom/base/nsIDebug2.idl @@ -7,7 +7,7 @@ #include "nsIDebug.idl" -[scriptable, uuid(9c9307ed-480a-4f2a-8f29-21378c03bcbc)] +[scriptable, uuid(6cb17fec-cdf7-4f7c-b267-37a0acaa9cf1)] interface nsIDebug2 : nsIDebug { /** @@ -21,4 +21,10 @@ interface nsIDebug2 : nsIDebug * The number of assertions since process start. */ readonly attribute long assertionCount; + + /** + * Whether a debugger is currently attached. + * Supports Windows + Mac + */ + readonly attribute bool isDebuggerAttached; };