]> git.neil.brown.name Git - edlib.git/commitdiff
Don't impose time limits when run under a debugger
authorNeilBrown <neil@brown.name>
Sat, 22 Jul 2023 01:37:38 +0000 (11:37 +1000)
committerNeilBrown <neil@brown.name>
Sat, 22 Jul 2023 01:37:38 +0000 (11:37 +1000)
Single-stepping in the debug can make things take much longer.  We
shouldn't let that change behaviour or debugging becomes harder.
So detect debugger and skip the timing.

Signed-off-by: NeilBrown <neil@brown.name>
core-misc.c
core-pane.h

index c2f0656cc51c1eee6d41f87fabf416daf4e2a130..891157f3894b8ad7db3e1ccded4f41ae1a69b36c 100644 (file)
@@ -9,10 +9,12 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <signal.h>
 #include <wchar.h>
 #include <time.h>
 #include <unistd.h>
 
+#include "core.h"
 #include "safe.h"
 #include "list.h"
 #include "misc.h"
@@ -532,11 +534,23 @@ int utf8_round_len(const char *text safe, int len)
 }
 
 time_t edlib_timing = 0;
-/* Set this to False when using gdb. It must be
- * extern to avoid it being optimised away.
- */
-extern bool edlib_timing_allowed;
-bool edlib_timing_allowed = True;
+
+static int _debugger_present = -1;
+static void _sigtrap_handler(int signum)
+{
+       _debugger_present = 0;
+       signal(SIGTRAP, SIG_DFL);
+}
+
+bool debugger_is_present(void)
+{
+       if (_debugger_present < 0) {
+               _debugger_present = 1;
+               signal(SIGTRAP, _sigtrap_handler);
+               raise(SIGTRAP);
+       }
+       return _debugger_present;
+}
 
 int times_up(void)
 {
@@ -547,6 +561,11 @@ int times_up(void)
                return 1;
        now = time(NULL);
        if (edlib_timing + 15 < now) {
+               /* If running under gdb, then I was probaly delayed
+                * by single-stepping, so don't through an error
+                */
+               if (debugger_is_present())
+                       return 0;
                edlib_timing = 1;
                return 1;
        }
@@ -555,7 +574,7 @@ int times_up(void)
 
 void time_starts(void)
 {
-       if (edlib_timing_allowed)
+       if (_debugger_present != 1)
                edlib_timing = time(NULL);
 }
 
index 4b61b4d881a5c651cbda3ce17453b3a3440a07d0..9c1fa6fce4d5ac7e3bda33daef48a46ecf2e7e76 100644 (file)
@@ -43,18 +43,20 @@ static inline unsigned int ts_to_ms(struct timespec *ts safe)
        return ts->tv_nsec / 1000 / 1000 + ts->tv_sec * 1000;
 }
 
+extern bool debugger_is_present(void);
+
 static inline bool pane_too_long(struct pane *p safe, unsigned int msec)
 {
-       extern bool edlib_timing_allowed;
        struct timespec ts;
        unsigned int duration;
-       if (!edlib_timing_allowed)
-               return False;
+
        clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
        duration = ts_to_ms(&ts) - p->timestamp;
        if (msec < 100)
                msec = 100;
-       return (duration > msec);
+       if (duration <= msec)
+               return False;
+       return ! debugger_is_present();
 }
 
 static inline void pane_set_time(struct pane *p safe)