Loading...
other-tools/dyld_usage.cpp dyld-960 dyld-1330
--- dyld/dyld-960/other-tools/dyld_usage.cpp
+++ dyld/dyld-1330/other-tools/dyld_usage.cpp
@@ -41,7 +41,7 @@
 #include <libutil.h>
 #include <ktrace/session.h>
 #include <dispatch/dispatch.h>
-#include <System/sys/kdebug.h>
+#include <sys/kdebug_private.h>
 
 #include "Tracing.h"
 
@@ -140,6 +140,7 @@
                 case DBG_DYLD_TIMING_APPLY_FIXUPS: enqueueEvent<apply_fixups>(event, false); break;
                 case DBG_DYLD_TIMING_ATTACH_CODESIGNATURE: enqueueEvent<attach_signature>(event, false); break;
                 case DBG_DYLD_TIMING_BUILD_CLOSURE: enqueueEvent<build_closure>(event, false); break;
+                case DBG_DYLD_TIMING_VALIDATE_CLOSURE: enqueueEvent<validate_closure>(event, false); break;
                 case DBG_DYLD_TIMING_DLADDR: enqueueEvent<dladdr>(event, true); break;
                 case DBG_DYLD_TIMING_DLCLOSE: enqueueEvent<dlclose>(event, true); break;
                 case DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE: enqueueEvent<add_image_callback>(event, false); break;
@@ -155,9 +156,7 @@
                 case DBG_DYLD_TIMING_DLOPEN_PREFLIGHT: dequeueEvent<dlopen_preflight>(event, [&](dlopen_preflight* endEvent){
                     endEvent->result = event->arg2;
                 }); break;
-                case DBG_DYLD_TIMING_LAUNCH_EXECUTABLE: dequeueEvent<app_launch>(event, [&](app_launch* endEvent){
-                    endEvent->launchMode = event->arg4;
-                }); break;
+                case DBG_DYLD_TIMING_LAUNCH_EXECUTABLE: dequeueEvent<app_launch>(event, [&](app_launch* endEvent) {}); break;
                 case DBG_DYLD_TIMING_DLSYM: dequeueEvent<dlsym>(event, [&](dlsym* endEvent){
                     endEvent->result = event->arg2;
                 }); break;
@@ -169,7 +168,8 @@
                 case DBG_DYLD_TIMING_ATTACH_CODESIGNATURE: dequeueEvent<attach_signature>(event, [&](attach_signature* endEvent){}); break;
                 case DBG_DYLD_TIMING_BUILD_CLOSURE: dequeueEvent<build_closure>(event, [&](build_closure* endEvent){
                     endEvent->closureBuildState = event->arg2;
-                }); break;
+                });break;
+                case DBG_DYLD_TIMING_VALIDATE_CLOSURE: dequeueEvent<validate_closure>(event, [&](validate_closure* endEvent){}); break;
                 case DBG_DYLD_TIMING_DLCLOSE: dequeueEvent<dlclose>(event, [&](dlclose* endEvent){
                     endEvent->result = (int)event->arg2;
                 }); break;
@@ -186,6 +186,22 @@
         }
     }
 
+    void flushInterrupted() {
+        if (!_currentRootEvent) {
+            return;
+        }
+
+        std::ostringstream ostream;
+        outputConsoleLine(_currentRootEvent, columns, ostream, 0,
+                "root event was interrupted, flushing events", true);
+        std::cerr << ostream.str();
+        std::cerr.flush();
+
+        _eventStack.clear();
+        output(_currentRootEvent);
+        _currentRootEvent = nullptr;
+    }
+
     bool empty() { return !_currentRootEvent && _eventStack.empty() && _rootEvents.empty(); }
 private:
     template<typename T>
@@ -216,7 +232,7 @@
     }
 
     struct event_pair {
-        event_pair(ktrace_event_t E) : _startTime(E->timestamp), _depth(0), _threadid((unsigned long)(E->threadid)),
+        event_pair(ktrace_event_t E) : _startTime(E->timestamp), _endTime(0), _depth(0), _threadid((unsigned long)(E->threadid)),
                                        _eventCode(KDBG_EXTRACT_CODE(E->debugid)), _walltime(E->walltime) {};
         virtual ~event_pair(){}
         std::vector<std::shared_ptr<event_pair>>& children() { return _children; }
@@ -278,6 +294,11 @@
     }
 
     std::string duration(std::shared_ptr<event_pair> event) {
+        // interrupted event with no end timestamp
+        if (event->endTimestamp() == 0) {
+            return "";
+        }
+
         std::ostringstream result;
         uint64_t usecs = (mach_to_nano(event->endTimestamp() - event->startTimestamp()) + (NSEC_PER_USEC - 1)) / NSEC_PER_USEC;
         uint64_t secs = usecs / USEC_PER_SEC;
@@ -291,7 +312,7 @@
         std::ostringstream line;
         bool extended = false;
         if (auto dlopenNode = dynamic_cast<dlopen *>(node.get())) {
-            line << "dlopen(\"" << dlopenNode->path << "\", " << dlopenNode->flagString() << ") -> 0x" << dlopenNode->result;
+            line << "dlopen(\"" << dlopenNode->path << "\", " << dlopenNode->flagString() << ") -> 0x" << std::hex << dlopenNode->result;
         } else if (auto dlopenPreflightNode = dynamic_cast<dlopen_preflight *>(node.get())) {
             line << "dlopen_preflight(\"" << dlopenPreflightNode->path << ") -> 0x" << dlopenPreflightNode->result;
         } else if (auto dlsymNode = dynamic_cast<dlsym *>(node.get())) {
@@ -302,8 +323,10 @@
             line << "attach codesignature";
         } else if (auto buildClosureNode = dynamic_cast<build_closure *>(node.get())) {
             line << "build closure -> " << buildClosureNode->buildStateString();
+        } else if (auto validateClosure = dynamic_cast<validate_closure *>(node.get())) {
+            line << "validate closure";
         } else if (auto launchNode = dynamic_cast<app_launch *>(node.get())) {
-            line << "app launch (dyld" << std::dec << launchNode->launchMode << ") -> 0x" << std::hex << launchNode->address;
+            line << "app launch " + launchNode->flagsString() + "-> 0x" << std::hex << launchNode->address;
         } else if (auto initNode  = dynamic_cast<static_init *>(node.get())) {
             line << "run static initializer 0x" << std::hex << initNode->funcAddress;
         } else if (auto fixupNode  = dynamic_cast<apply_fixups *>(node.get())) {
@@ -327,8 +350,15 @@
             extended = true;
         }
 
+        outputConsoleLine(node, width, sstr, depth, line.str(), extended);
+
+        for (const auto& child : node->children()) {
+            outputConsole(child, width, sstr, depth+1);
+        }
+    }
+
+    void outputConsoleLine(std::shared_ptr<event_pair> node, uint64_t width, std::ostringstream& sstr, uint64_t depth, std::string lineStr, bool extended) {
         std::string timestampStr = timestamp(node, extended);
-        std::string lineStr = line.str();
         std::string commandStr = process(node, extended);
         std::string durationStr = duration(node);
         size_t lineMax = (size_t)width - (timestampStr.length() + commandStr.length() + durationStr.length() + 2*(size_t)depth + 3);
@@ -337,10 +367,6 @@
         sstr << timestampStr << " ";
         std::fill_n(std::ostream_iterator<char>(sstr), 2*depth, ' ');
         sstr << lineStr << " " << durationStr << " " << commandStr << std::endl;
-
-        for (const auto& child : node->children()) {
-            outputConsole(child, width, sstr, depth+1);
-        }
     }
 
     void outputJSON(std::shared_ptr<event_pair> node, std::ostringstream& sstr) {
@@ -365,7 +391,7 @@
         } else if (auto launchNode = dynamic_cast<app_launch *>(node.get())) {
             sstr << std::hex;
             sstr << "{\"type\":\"app_launch\",\"address\":\"0x";
-            sstr << launchNode->address << "\",\"mode\":" << launchNode->launchMode << "";
+            sstr << launchNode->address << "\",\"mode\":" << launchNode->flags << "";
         } else if (auto initNode  = dynamic_cast<static_init *>(node.get())) {
             sstr << std::hex;
             sstr << "{\"type\":\"static_init\",\"image_address\":\"0x"  << initNode->libraryAddress;
@@ -553,10 +579,32 @@
     };
 
     struct app_launch : event_pair {
-        app_launch(ktrace_event_t E) : event_pair(E), address(E->arg2) {}
+        app_launch(ktrace_event_t E) : event_pair(E), address(E->arg2), flags(E->arg3) {}
         uint64_t address;
-        uint64_t launchMode;
+        uint64_t flags;
         std::vector<event_pair *> _children;
+
+        std::string flagsString() const {
+            if ( flags == (uint64_t)dyld3::DyldLaunchExecutableFlags::None )
+                return "";
+            std::string v;
+            if ( flags & (uint64_t)dyld3::DyldLaunchExecutableFlags::HasTPROHeap ) {
+                if ( !v.empty() )
+                    v += ", ";
+                v += "tpro-heap";
+            }
+            if ( flags & (uint64_t)dyld3::DyldLaunchExecutableFlags::HasTPRODataConst ) {
+                if ( !v.empty() )
+                    v += ", ";
+                v += "tpro-data-const";
+            }
+            if ( flags & (uint64_t)dyld3::DyldLaunchExecutableFlags::HasTPROStacks ) {
+                if ( !v.empty() )
+                    v += ", ";
+                v += "tpro-stacks";
+            }
+            return "(" + v + ") ";
+        }
     };
 
     struct static_init : event_pair {
@@ -601,6 +649,10 @@
         };
     };
 
+    struct validate_closure : event_pair {
+        validate_closure(ktrace_event_t E) : event_pair(E) {}
+    };
+
     struct add_image_callback : event_pair {
         add_image_callback(ktrace_event_t E) : event_pair(E), libraryAddress(E->arg2), funcAddress(E->arg3) {}
         uint64_t libraryAddress;
@@ -632,6 +684,10 @@
     std::map<unsigned long, std::unique_ptr<output_renderer>> sOutputRenders;
 
     void flush() {
+        for (const auto& renderer: sOutputRenders) {
+            renderer.second->flushInterrupted();
+        }
+
         if (JSON_Tracing_flag) {
             std::ostringstream ostream;
             ostream << "{\"displayTimeUnit\":\"ns\"";
@@ -707,11 +763,18 @@
             return;
         auto i = sOutputManager.sOutputRenders.find((size_t)(event->threadid));
         if (i == sOutputManager.sOutputRenders.end()) {
-            sOutputManager.sOutputRenders.emplace(std::make_pair(event->threadid, std::make_unique<output_renderer>(s, event)));
-            i = sOutputManager.sOutputRenders.find((size_t)(event->threadid));
+            i = sOutputManager.sOutputRenders.emplace(event->threadid, std::make_unique<output_renderer>(s, event)).first;
         }
         i->second->recordEvent(event);
         if (i->second->empty()) {
+            sOutputManager.sOutputRenders.erase(i);
+        }
+    });
+
+    ktrace_set_thread_exit_handler(s, ^(uint64_t tid, const char* execname) {
+        if (auto i = sOutputManager.sOutputRenders.find((size_t)tid); i != sOutputManager.sOutputRenders.end()) {
+            // Renderer still registered, so the thread might have finished unexpectedly with missing end event
+            i->second->flushInterrupted();
             sOutputManager.sOutputRenders.erase(i);
         }
     });
@@ -803,9 +866,6 @@
             ktrace_exclude_process(s, "csh");
             ktrace_exclude_process(s, "sh");
             ktrace_exclude_process(s, "zsh");
-#if TARGET_OS_EMBEDDED
-            ktrace_exclude_process(s, "dropbear");
-#endif /* TARGET_OS_EMBEDDED */
         }
     }