Loading...
other-tools/dyld_usage.cpp dyld-1340 dyld-960
--- dyld/dyld-1340/other-tools/dyld_usage.cpp
+++ dyld/dyld-960/other-tools/dyld_usage.cpp
@@ -41,7 +41,7 @@
 #include <libutil.h>
 #include <ktrace/session.h>
 #include <dispatch/dispatch.h>
-#include <sys/kdebug_private.h>
+#include <System/sys/kdebug.h>
 
 #include "Tracing.h"
 
@@ -140,7 +140,6 @@
                 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;
@@ -156,7 +155,9 @@
                 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) {}); break;
+                case DBG_DYLD_TIMING_LAUNCH_EXECUTABLE: dequeueEvent<app_launch>(event, [&](app_launch* endEvent){
+                    endEvent->launchMode = event->arg4;
+                }); break;
                 case DBG_DYLD_TIMING_DLSYM: dequeueEvent<dlsym>(event, [&](dlsym* endEvent){
                     endEvent->result = event->arg2;
                 }); break;
@@ -168,8 +169,7 @@
                 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;
-                case DBG_DYLD_TIMING_VALIDATE_CLOSURE: dequeueEvent<validate_closure>(event, [&](validate_closure* endEvent){}); break;
+                }); break;
                 case DBG_DYLD_TIMING_DLCLOSE: dequeueEvent<dlclose>(event, [&](dlclose* endEvent){
                     endEvent->result = (int)event->arg2;
                 }); break;
@@ -186,22 +186,6 @@
         }
     }
 
-    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>
@@ -232,7 +216,7 @@
     }
 
     struct event_pair {
-        event_pair(ktrace_event_t E) : _startTime(E->timestamp), _endTime(0), _depth(0), _threadid((unsigned long)(E->threadid)),
+        event_pair(ktrace_event_t E) : _startTime(E->timestamp), _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; }
@@ -294,11 +278,6 @@
     }
 
     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;
@@ -312,7 +291,7 @@
         std::ostringstream line;
         bool extended = false;
         if (auto dlopenNode = dynamic_cast<dlopen *>(node.get())) {
-            line << "dlopen(\"" << dlopenNode->path << "\", " << dlopenNode->flagString() << ") -> 0x" << std::hex << dlopenNode->result;
+            line << "dlopen(\"" << dlopenNode->path << "\", " << dlopenNode->flagString() << ") -> 0x" << 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())) {
@@ -323,10 +302,8 @@
             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 " + launchNode->flagsString() + "-> 0x" << std::hex << launchNode->address;
+            line << "app launch (dyld" << std::dec << launchNode->launchMode << ") -> 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())) {
@@ -350,15 +327,8 @@
             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);
@@ -367,6 +337,10 @@
         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) {
@@ -391,7 +365,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->flags << "";
+            sstr << launchNode->address << "\",\"mode\":" << launchNode->launchMode << "";
         } else if (auto initNode  = dynamic_cast<static_init *>(node.get())) {
             sstr << std::hex;
             sstr << "{\"type\":\"static_init\",\"image_address\":\"0x"  << initNode->libraryAddress;
@@ -579,32 +553,10 @@
     };
 
     struct app_launch : event_pair {
-        app_launch(ktrace_event_t E) : event_pair(E), address(E->arg2), flags(E->arg3) {}
+        app_launch(ktrace_event_t E) : event_pair(E), address(E->arg2) {}
         uint64_t address;
-        uint64_t flags;
+        uint64_t launchMode;
         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 {
@@ -649,10 +601,6 @@
         };
     };
 
-    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;
@@ -684,10 +632,6 @@
     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\"";
@@ -763,18 +707,11 @@
             return;
         auto i = sOutputManager.sOutputRenders.find((size_t)(event->threadid));
         if (i == sOutputManager.sOutputRenders.end()) {
-            i = sOutputManager.sOutputRenders.emplace(event->threadid, std::make_unique<output_renderer>(s, event)).first;
+            sOutputManager.sOutputRenders.emplace(std::make_pair(event->threadid, std::make_unique<output_renderer>(s, event)));
+            i = sOutputManager.sOutputRenders.find((size_t)(event->threadid));
         }
         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);
         }
     });
@@ -866,6 +803,9 @@
             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 */
         }
     }