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