MMPrinter.cpp (3381B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "MMPrinter.h" 8 9 #include "Logging.h" 10 #include "jsapi.h" 11 #include "mozilla/Bootstrap.h" 12 #include "mozilla/ErrorResult.h" 13 #include "mozilla/RandomNum.h" 14 #include "mozilla/dom/ScriptSettings.h" 15 #include "mozilla/dom/ipc/StructuredCloneData.h" 16 #include "nsFrameMessageManager.h" 17 #include "nsJSUtils.h" 18 #include "prenv.h" 19 20 namespace mozilla::dom { 21 22 LazyLogModule MMPrinter::sMMLog("MessageManager"); 23 24 // You can use 25 // https://gist.github.com/tomrittervg/adb8688426a9a5340da96004e2c8af79 to parse 26 // the output of the logs into logs more friendly to reading. 27 28 /* static */ 29 Maybe<uint64_t> MMPrinter::PrintHeader(char const* aLocation, 30 const nsAString& aMsg) { 31 NS_ConvertUTF16toUTF8 charMsg(aMsg); 32 33 /* 34 * The topic will be skipped if the topic name appears anywhere as a substring 35 * of the filter. 36 * 37 * Example: 38 * MOZ_LOG_MESSAGEMANAGER_SKIP="foobar|extension" 39 * Will match the topics 'foobar', 'foo', 'bar', and 'ten' (even though 40 * you may not have intended to match the latter three) and it will not match 41 * the topics 'extensionresult' or 'Foo'. 42 */ 43 char* mmSkipLog = PR_GetEnv("MOZ_LOG_MESSAGEMANAGER_SKIP"); 44 45 if (mmSkipLog && strstr(mmSkipLog, charMsg.get())) { 46 return Nothing(); 47 } 48 49 uint64_t aMsgId = RandomUint64OrDie(); 50 51 MOZ_LOG(MMPrinter::sMMLog, LogLevel::Debug, 52 ("%" PRIu64 " %s Message: %s in process type: %s", aMsgId, aLocation, 53 charMsg.get(), XRE_GetProcessTypeString())); 54 55 return Some(aMsgId); 56 } 57 58 /* static */ 59 void MMPrinter::PrintNoData(uint64_t aMsgId) { 60 if (!MOZ_LOG_TEST(sMMLog, LogLevel::Verbose)) { 61 return; 62 } 63 MOZ_LOG(MMPrinter::sMMLog, LogLevel::Verbose, 64 ("%" PRIu64 " (No Data)", aMsgId)); 65 } 66 67 /* static */ 68 void MMPrinter::PrintData(uint64_t aMsgId, ClonedMessageData const& aData) { 69 if (!MOZ_LOG_TEST(sMMLog, LogLevel::Verbose)) { 70 return; 71 } 72 73 ErrorResult rv; 74 75 AutoJSAPI jsapi; 76 // We're using this context to deserialize, stringify, and print a message 77 // manager message here. Since the messages are always sent from and to system 78 // scopes, we need to do this in a system scope, or attempting to deserialize 79 // certain privileged objects will fail. 80 MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope())); 81 JSContext* cx = jsapi.cx(); 82 83 ipc::StructuredCloneData data; 84 ipc::UnpackClonedMessageData(aData, data); 85 86 /* Read original StructuredCloneData. */ 87 JS::Rooted<JS::Value> scdContent(cx); 88 data.Read(cx, &scdContent, rv); 89 if (rv.Failed()) { 90 // In testing, the only reason this would fail was if there was no data in 91 // the message; so it seems like this is safe-ish. 92 MMPrinter::PrintNoData(aMsgId); 93 rv.SuppressException(); 94 return; 95 } 96 97 JS::Rooted<JSString*> unevalObj(cx, JS_ValueToSource(cx, scdContent)); 98 nsAutoJSString srcString; 99 if (!srcString.init(cx, unevalObj)) return; 100 101 MOZ_LOG(MMPrinter::sMMLog, LogLevel::Verbose, 102 ("%" PRIu64 " %s", aMsgId, NS_ConvertUTF16toUTF8(srcString).get())); 103 } 104 105 } // namespace mozilla::dom