+int restarting_usleep(useconds_t usecs)
+{
+ struct timespec tv;
+ int result;
+
+ tv.tv_sec = 0;
+ tv.tv_nsec = usecs * 1000;
+
+ do {
+ result = nanosleep(&tv, &tv);
+ } while(result == -1 && errno == EINTR);
+
+ return result;
+}
+
+static void stop_listener()
+{
+ int result;
+
+ result = pthread_cancel(listener_thread);
+ if(result != 0) {
+ ERR("pthread_cancel: %s", strerror(result));
+ }
+ result = pthread_join(listener_thread, NULL);
+ if(result != 0) {
+ ERR("pthread_join: %s", strerror(result));
+ }
+}
+
+/* This destructor keeps the process alive for a few seconds in order
+ * to leave time to ustd to connect to its buffers. This is necessary
+ * for programs whose execution is very short. It is also useful in all
+ * programs when tracing is started close to the end of the program
+ * execution.
+ *
+ * FIXME: For now, this only works for the first trace created in a
+ * process.
+ */
+
+static void __attribute__((destructor)) keepalive()
+{
+ if(trace_recording() && LOAD_SHARED(buffers_to_export)) {
+ int total = 0;
+ DBG("Keeping process alive for consumer daemon...");
+ while(LOAD_SHARED(buffers_to_export)) {
+ const int interv = 200000;
+ restarting_usleep(interv);
+ total += interv;
+
+ if(total >= 3000000) {
+ WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
+ break;
+ }
+ }
+ DBG("Finally dying...");
+ }
+
+ destroy_traces();
+
+ /* Ask the listener to stop and clean up. */
+ stop_listener();
+}
+
+void ust_potential_exec(void)
+{
+ trace_mark(ust, potential_exec, MARK_NOARGS);
+
+ DBG("test");
+
+ keepalive();
+}
+