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