capture: add sp_capture_writer_new_from_env()

This will return NULL if there is not sufficient data from the
environment to setup tracing.
This commit is contained in:
Christian Hergert
2019-05-06 20:19:46 -07:00
parent 209b342ea1
commit 05314dd28c
2 changed files with 24 additions and 0 deletions

View File

@ -23,6 +23,7 @@
#include <errno.h>
#include <fcntl.h>
#include <glib/gstdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -415,6 +416,9 @@ sp_capture_writer_new_from_fd (int fd,
GTimeVal tv;
gsize header_len = sizeof(*header);
if (fd < 0)
return NULL;
if (buffer_size == 0)
buffer_size = DEFAULT_BUFFER_SIZE;
@ -1221,3 +1225,22 @@ do_end:
return TRUE;
}
SpCaptureWriter *
sp_capture_writer_new_from_env (gsize buffer_size)
{
const gchar *fdstr;
int fd;
if (!(fdstr = g_getenv ("SYSPROF_TRACE_FD")))
return NULL;
if (!(fd = atoi (fdstr)))
return NULL;
/* ignore stdin/stdout/stderr */
if (fd < 2)
return NULL;
return sp_capture_writer_new_from_fd (dup (fd), buffer_size);
}

View File

@ -38,6 +38,7 @@ typedef struct
gsize padding[48];
} SpCaptureStat;
SpCaptureWriter *sp_capture_writer_new_from_env (gsize buffer_size);
SpCaptureWriter *sp_capture_writer_new (const gchar *filename,
gsize buffer_size);
SpCaptureWriter *sp_capture_writer_new_from_fd (int fd,