Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 12058)
+++ trunk/ippTools/src/dettool.c	(revision 12065)
@@ -35,4 +35,5 @@
 static bool definebydetrunMode(pxConfig *config);
 static bool runsMode(pxConfig *config);
+static bool orphanrunMode(pxConfig *config);
 static bool inputMode(pxConfig *config);
 static bool rawMode(pxConfig *config);
@@ -106,4 +107,5 @@
         MODECASE(DETTOOL_MODE_DEFINEBYDETRUN,   definebydetrunMode);
         MODECASE(DETTOOL_MODE_RUNS,             runsMode);
+        MODECASE(DETTOOL_MODE_ORPHANRUN,        orphanrunMode);
         MODECASE(DETTOOL_MODE_INPUT,            inputMode);
         MODECASE(DETTOOL_MODE_RAW,              rawMode);
@@ -1714,4 +1716,74 @@
 
     psFree(runs);
+
+    return true;
+}
+
+static bool orphanrunMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psString query = psStringCopy(
+            "SELECT\n"
+            "   detRun.*\n"
+            " FROM detRun\n"
+            " LEFT JOIN detRun as foo\n"
+            "   ON foo.parent = detRun.det_id\n"
+            " WHERE"
+            "   detRun.state = 'stop'"
+            "   AND detRun.mode = 'master'"
+        );
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detRun");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no orphan detRun rows found");
+        psFree(output);
+        return true;
+    }
+
+    // convert det_id to a string externaly
+    if (!convertDetIdToStr(output)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to convert det_id to a string");
+        psFree(output);
+        return false;
+    }
+
+    bool simple = false;
+    {
+        bool status = false;
+        simple = psMetadataLookupBool(&status, config->args, "-simple");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
+            psFree(output);
+            return false;
+        }
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "detRun", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
 
     return true;
