-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_file.c
126 lines (96 loc) · 3.09 KB
/
trace_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "postgres.h"
#include <sys/stat.h>
#include "utils/guc.h"
#include "miscadmin.h"
#include "trace_file.h"
static char* traceFileName = "trace_file.txt";
char *dataDir;
int traceFileLimit = 16; // limit in megabytes
size_t currentSize = 0;
FILE* traceFile = NULL;
static bool CheckDataDirValue(char **newval, void **extra, GucSource source);
void
TraceFileDeclareGucVariables(void)
{
DefineCustomIntVariable("pg_uprobe.trace_file_limit",
"trace session data limit in megabytes",
NULL,
&traceFileLimit,
16,
1,
32 * 1024,
PGC_SUSET,
GUC_UNIT_MB,
NULL,
NULL,
NULL);
DefineCustomStringVariable("pg_uprobe.trace_file_name",
"file name for trace session data",
NULL,
&traceFileName,
"trace_file.txt",
PGC_SUSET,
0,
NULL, NULL, NULL);
DefineCustomStringVariable("pg_uprobe.data_dir",
"dir for file pg_uprobe.trace_file_name and other pg_uprobe files",
NULL,
&dataDir,
"./pg_uprobe/",
PGC_SUSET,
0,
CheckDataDirValue, NULL, NULL);
}
bool
OpenTraceSessionFile(bool throwOnError)
{
char path[4096];
int error;
sprintf(path, "%s%s_%d", dataDir, traceFileName, MyProcPid);
traceFile = fopen(path, "w");
error = errno;
if (traceFile == NULL && throwOnError)
elog(ERROR, "can't open file %s for trace session data: %s", path, strerror(error));
else if (traceFile == NULL)
elog(LOG, "can't open file %s for trace session data: %s", path, strerror(error));
currentSize = 0;
return traceFile != NULL;
}
void
CloseTraceSessionFile(void)
{
if (traceFile == NULL)
return;
fclose(traceFile);
traceFile = NULL;
currentSize = 0;
}
static bool
CheckDataDirValue(char** newval, void** extra, GucSource source)
{
struct stat st;
size_t size;
if (*newval == NULL || *newval[0] == '\0')
{
GUC_check_errdetail("pg_uprobe data can't be empty or null");
return false;
}
size = strlen(*newval);
if (size + 128 >= MAXPGPATH)
{
GUC_check_errdetail("pg_uprobe data dir too long.");
return false;
}
if ((*newval)[size - 1] != '/')
{
*newval = guc_realloc(ERROR, *newval, size + 2);
(*newval)[size] = '/';
(*newval)[size + 1] = '\0';
}
if (stat(*newval, &st) != 0 || !S_ISDIR(st.st_mode))
{
GUC_check_errdetail("Specified pg_uprobe data dir does not exist.");
return false;
}
return true;
}