annotate libvtv/vtv_utils.cc @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1 /* Copyright (C) 2012-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 This file is part of GCC.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
7 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
8 any later version.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
13 GNU General Public License for more details.
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
16 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
17 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
20 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
22 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 /* This file is part of the vtable verication runtime library (see
kono
parents:
diff changeset
25 comments in vtv_rts.cc for more information about vtable
kono
parents:
diff changeset
26 verification). This file contains log file utilities. */
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 #include <sys/types.h>
kono
parents:
diff changeset
29 #include <sys/stat.h>
kono
parents:
diff changeset
30 #include <fcntl.h>
kono
parents:
diff changeset
31 #include <stdarg.h>
kono
parents:
diff changeset
32 #include <stdio.h>
kono
parents:
diff changeset
33 #include <stdlib.h>
kono
parents:
diff changeset
34 #include <string.h>
kono
parents:
diff changeset
35 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
36 #include <windows.h>
kono
parents:
diff changeset
37 #else
kono
parents:
diff changeset
38 #include <execinfo.h>
kono
parents:
diff changeset
39 #endif
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 #include <unistd.h>
kono
parents:
diff changeset
42 #include <errno.h>
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 #include "vtv_utils.h"
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 #ifndef HAVE_SECURE_GETENV
kono
parents:
diff changeset
47 # ifdef HAVE___SECURE_GETENV
kono
parents:
diff changeset
48 # define secure_getenv __secure_getenv
kono
parents:
diff changeset
49 # else
kono
parents:
diff changeset
50 # define secure_getenv getenv
kono
parents:
diff changeset
51 # endif
kono
parents:
diff changeset
52 #endif
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 static int vtv_failures_log_fd = -1;
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 /* This function takes the NAME of a log file to open, attempts to
kono
parents:
diff changeset
57 open it in the logs_dir directory, and returns the resulting file
kono
parents:
diff changeset
58 descriptor.
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 This function first checks to see if the user has specifed (via
kono
parents:
diff changeset
61 the environment variable VTV_LOGS_DIR) a directory to use for the
kono
parents:
diff changeset
62 vtable verification logs. If that fails, the function will open
kono
parents:
diff changeset
63 the logs in the current directory.
kono
parents:
diff changeset
64 */
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 int
kono
parents:
diff changeset
67 __vtv_open_log (const char *name)
kono
parents:
diff changeset
68 {
kono
parents:
diff changeset
69 char log_name[1024];
kono
parents:
diff changeset
70 char log_dir[512];
kono
parents:
diff changeset
71 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
72 pid_t process_id = GetCurrentProcessId ();
kono
parents:
diff changeset
73 #else
kono
parents:
diff changeset
74 uid_t user_id = getuid ();
kono
parents:
diff changeset
75 pid_t process_id = getpid ();
kono
parents:
diff changeset
76 #endif
kono
parents:
diff changeset
77 char *logs_prefix;
kono
parents:
diff changeset
78 bool logs_dir_specified = false;
kono
parents:
diff changeset
79 int fd = -1;
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 logs_prefix = secure_getenv ("VTV_LOGS_DIR");
kono
parents:
diff changeset
82 if (logs_prefix && strlen (logs_prefix) > 0)
kono
parents:
diff changeset
83 {
kono
parents:
diff changeset
84 logs_dir_specified = true;
kono
parents:
diff changeset
85 #ifdef __MINGW32__
kono
parents:
diff changeset
86 mkdir (logs_prefix);
kono
parents:
diff changeset
87 #else
kono
parents:
diff changeset
88 mkdir (logs_prefix, S_IRWXU);
kono
parents:
diff changeset
89 #endif
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 snprintf (log_dir, sizeof (log_dir), "%s/vtv_logs", logs_prefix);
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 #ifdef __MINGW32__
kono
parents:
diff changeset
94 mkdir (log_dir);
kono
parents:
diff changeset
95 #else
kono
parents:
diff changeset
96 mkdir (log_dir, S_IRWXU);
kono
parents:
diff changeset
97 #endif
kono
parents:
diff changeset
98 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
99 snprintf (log_name, sizeof (log_name), "%s_%d_%s", log_dir,
kono
parents:
diff changeset
100 (unsigned) process_id, name);
kono
parents:
diff changeset
101 fd = open (log_name, O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
kono
parents:
diff changeset
102 #else
kono
parents:
diff changeset
103 snprintf (log_name, sizeof (log_name), "%s/%d_%d_%s", log_dir,
kono
parents:
diff changeset
104 (unsigned) user_id, (unsigned) process_id, name);
kono
parents:
diff changeset
105 fd = open (log_name, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW,
kono
parents:
diff changeset
106 S_IRWXU);
kono
parents:
diff changeset
107 #endif
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109 else
kono
parents:
diff changeset
110 fd = dup (2);
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 if (fd == -1)
kono
parents:
diff changeset
113 __vtv_add_to_log (2, "Cannot open log file %s %s\n", name,
kono
parents:
diff changeset
114 strerror (errno));
kono
parents:
diff changeset
115 return fd;
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 /* This function takes a file descriptor (FD) and a string (STR) and
kono
parents:
diff changeset
119 tries to write the string to the file. */
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 static int
kono
parents:
diff changeset
122 vtv_log_write (int fd, const char *str)
kono
parents:
diff changeset
123 {
kono
parents:
diff changeset
124 if (write (fd, str, strlen (str)) != -1)
kono
parents:
diff changeset
125 return 0;
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 if (fd != 2) /* Make sure we dont get in a loop. */
kono
parents:
diff changeset
128 __vtv_add_to_log (2, "Error writing to log: %s\n", strerror (errno));
kono
parents:
diff changeset
129 return -1;
kono
parents:
diff changeset
130 }
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 /* This function takes a file decriptor (LOG_FILE) and an output
kono
parents:
diff changeset
134 format string (FORMAT), followed by zero or more print format
kono
parents:
diff changeset
135 arguments (the same as fprintf, for example). It gets the current
kono
parents:
diff changeset
136 process ID and PPID, pre-pends them to the formatted message, and
kono
parents:
diff changeset
137 writes write it out to the log file referenced by LOG_FILE via calles
kono
parents:
diff changeset
138 to vtv_log_write. */
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 int
kono
parents:
diff changeset
141 __vtv_add_to_log (int log_file, const char * format, ...)
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 /* We dont want to dynamically allocate this buffer. This should be
kono
parents:
diff changeset
144 more than enough in most cases. It if isn't we are careful not to
kono
parents:
diff changeset
145 do a buffer overflow. */
kono
parents:
diff changeset
146 char output[1024];
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 va_list ap;
kono
parents:
diff changeset
149 va_start (ap, format);
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
152 snprintf (output, sizeof (output), "VTV: PID=%ld ", GetCurrentProcessId ());
kono
parents:
diff changeset
153 #else
kono
parents:
diff changeset
154 snprintf (output, sizeof (output), "VTV: PID=%d PPID=%d ", getpid (),
kono
parents:
diff changeset
155 getppid ());
kono
parents:
diff changeset
156 #endif
kono
parents:
diff changeset
157 vtv_log_write (log_file, output);
kono
parents:
diff changeset
158 vsnprintf (output, sizeof (output), format, ap);
kono
parents:
diff changeset
159 vtv_log_write (log_file, output);
kono
parents:
diff changeset
160 va_end (ap);
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 return 0;
kono
parents:
diff changeset
163 }
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 /* Open error logging file, if not already open, and write vtable
kono
parents:
diff changeset
166 verification failure messages (LOG_MSG) to the log file. Also
kono
parents:
diff changeset
167 generate a backtrace in the log file, if GENERATE_BACKTRACE is
kono
parents:
diff changeset
168 set. */
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 void
kono
parents:
diff changeset
171 __vtv_log_verification_failure (const char *log_msg, bool generate_backtrace)
kono
parents:
diff changeset
172 {
kono
parents:
diff changeset
173 if (vtv_failures_log_fd == -1)
kono
parents:
diff changeset
174 vtv_failures_log_fd = __vtv_open_log ("vtable_verification_failures.log");
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 if (vtv_failures_log_fd == -1)
kono
parents:
diff changeset
177 return;
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 __vtv_add_to_log (vtv_failures_log_fd, "%s", log_msg);
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 #if !defined (__CYGWIN__) && !defined (__MINGW32__)
kono
parents:
diff changeset
182 if (generate_backtrace)
kono
parents:
diff changeset
183 {
kono
parents:
diff changeset
184 #define STACK_DEPTH 20
kono
parents:
diff changeset
185 void *callers[STACK_DEPTH];
kono
parents:
diff changeset
186 int actual_depth = backtrace (callers, STACK_DEPTH);
kono
parents:
diff changeset
187 backtrace_symbols_fd (callers, actual_depth, vtv_failures_log_fd);
kono
parents:
diff changeset
188 }
kono
parents:
diff changeset
189 #endif
kono
parents:
diff changeset
190 }