+
+int log_remove_callback(log_callback_fn fn, void *priv)
+{
+ struct log_callback *cb, **p;
+
+ for (p = &log_callbacks; (cb = *p); p = &(*p)->next)
+ {
+ if (cb->fn == fn && cb->priv == priv)
+ {
+ *p = cb->next;
+ free(cb);
+ return ERROR_OK;
+ }
+ }
+
+ /* no such item */
+ return ERROR_INVALID_ARGUMENTS;
+}
+
+/* return allocated string w/printf() result */
+char *alloc_vprintf(const char *fmt, va_list ap)
+{
+ va_list ap_copy;
+ int len;
+ char *string;
+
+ /* determine the length of the buffer needed */
+ va_copy(ap_copy, ap);
+ len = vsnprintf(NULL, 0, fmt, ap_copy);
+ va_end(ap_copy);
+
+ /* allocate and make room for terminating zero. */
+ /* FIXME: The old version always allocated at least one byte extra and
+ * other code depend on that. They should be probably be fixed, but for
+ * now reserve the extra byte. */
+ string = malloc(len + 2);
+ if (string == NULL)
+ return NULL;
+
+ /* do the real work */
+ vsnprintf(string, len + 1, fmt, ap);
+
+ return string;
+}
+
+char *alloc_printf(const char *format, ...)
+{
+ char *string;
+ va_list ap;
+ va_start(ap, format);
+ string = alloc_vprintf(format, ap);
+ va_end(ap);
+ return string;
+}
+
+/* Code must return to the server loop before 1000ms has returned or invoke
+ * this function.
+ *
+ * The GDB connection will time out if it spends >2000ms and you'll get nasty
+ * error messages from GDB:
+ *
+ * Ignoring packet error, continuing...
+ * Reply contains invalid hex digit 116
+ *
+ * While it is possible use "set remotetimeout" to more than the default 2000ms
+ * in GDB, OpenOCD guarantees that it sends keep-alive packages on the
+ * GDB protocol and it is a bug in OpenOCD not to either return to the server
+ * loop or invoke keep_alive() every 1000ms.
+ *
+ * This function will send a keep alive packet if >500ms has passed since last time
+ * it was invoked.
+ *
+ * Note that this function can be invoked often, so it needs to be relatively
+ * fast when invoked more often than every 500ms.
+ *
+ */
+void keep_alive()
+{
+ current_time = timeval_ms();
+ if (current_time-last_time > 1000)
+ {
+ extern int gdb_actual_connections;
+
+ if (gdb_actual_connections)
+ LOG_WARNING("keep_alive() was not invoked in the "
+ "1000ms timelimit. GDB alive packet not "
+ "sent! (%lld). Workaround: increase "
+ "\"set remotetimeout\" in GDB",
+ current_time-last_time);
+ else
+ LOG_DEBUG("keep_alive() was not invoked in the "
+ "1000ms timelimit (%lld). This may cause "
+ "trouble with GDB connections.",
+ current_time-last_time);
+ }
+ if (current_time-last_time > 500)
+ {
+ /* this will keep the GDB connection alive */
+ LOG_USER_N("%s", "");
+
+ /* DANGER!!!! do not add code to invoke e.g. target event processing,
+ * jim timer processing, etc. it can cause infinite recursion +
+ * jim event callbacks need to happen at a well defined time,
+ * not anywhere keep_alive() is invoked.
+ *
+ * These functions should be invoked at a well defined spot in server.c
+ */
+
+ last_time = current_time;
+ }
+}
+
+/* reset keep alive timer without sending message */
+void kept_alive()
+{
+ current_time = timeval_ms();
+ last_time = current_time;
+}
+
+/* if we sleep for extended periods of time, we must invoke keep_alive() intermittantly */
+void alive_sleep(uint64_t ms)
+{
+ uint64_t napTime = 10;
+ for (uint64_t i = 0; i < ms; i += napTime)
+ {
+ uint64_t sleep_a_bit = ms - i;
+ if (sleep_a_bit > napTime)
+ sleep_a_bit = napTime;
+
+ usleep(sleep_a_bit * 1000);
+ keep_alive();
+ }
+}
+
+void busy_sleep(uint64_t ms)
+{
+ uint64_t then = timeval_ms();
+ while (timeval_ms() - then < ms)
+ {
+ /* busy wait */
+ }
+}