Debian Codegraph @

rpython/rlib/rjitlog/src/rjitlog.c

raw symbols SCIP
1 #define _GNU_SOURCE 1
2
3 #ifdef RPYTHON_LL2CTYPES
4 /* only for testing: ll2ctypes sets RPY_EXTERN from the command-line */
5 #ifndef RPY_EXTERN
6 #define RPY_EXTERN RPY_EXPORTED
7 #endif
8 #ifdef _WIN32
9 #define RPY_EXPORTED __declspec(dllexport)
10 #else
11 #define RPY_EXPORTED extern __attribute__((visibility("default")))
12 #endif
13 #else
14 #include "common_header.h"
15 #include "structdef.h"
16 #include "src/threadlocal.h"
17 #include "rjitlog.h"
18 #endif
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #ifndef _WIN32
27 #include <unistd.h>
28 #endif
29 #include <errno.h>
30
31 #ifdef RPY_TRANSLATE /* compiling with rpython/bin/rpython or translate.py */
32 # define JITLOG "JITLOG"
33 #else
34 # define JITLOG "JITLOG_FORTESTS"
35 #endif
36
37 static int jitlog_fd = -1;
38 static int jitlog_ready = 0;
39
40 RPY_EXTERN
41 int jitlog_enabled()
42 {
43 return jitlog_ready;
44 }
45
46 RPY_EXTERN
47 void jitlog_try_init_using_env(void) {
48 char * filename;
49 if (jitlog_ready) { return; }
50
51 /* untranslated this is set to JITLOG_FORTESTS via a #define */
52 filename = getenv(JITLOG);
53
54 if (filename && filename[0]) {
55 // mode is 644
56 #ifdef _WIN32
57 int mode = _S_IWRITE | _S_IREAD;
58 #else
59 mode_t mode = 0644;
60 #endif
61 jitlog_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
62 if (jitlog_fd == -1) {
63 fprintf(stderr, "could not open '%s': ", filename);
64 perror(NULL);
65 exit(-1);
66 }
67 } else {
68 jitlog_ready = 0;
69 return;
70 }
71 #ifndef _WIN32
72 unsetenv(JITLOG);
73 #else
74 putenv(JITLOG "=");
75 #endif
76 jitlog_ready = 1;
77 }
78
79 RPY_EXTERN
80 char *jitlog_init(int fd)
81 {
82 jitlog_fd = fd;
83 jitlog_ready = 1;
84 return NULL;
85 }
86
87 RPY_EXTERN
88 void jitlog_teardown()
89 {
90 jitlog_ready = 0;
91 if (jitlog_fd == -1) {
92 return;
93 }
94 // close the jitlog file descriptor
95 close(jitlog_fd);
96 jitlog_fd = -1;
97 }
98
99 RPY_EXTERN
100 void jitlog_write_marked(char * text, int length)
101 {
102 if (!jitlog_ready) { return; }
103
104 if (write(jitlog_fd, text, length) < 0) { /* best-effort write; ignore error */ }
105 }