]> git.ipfire.org Git - thirdparty/gcc.git/blob - libvtv/vtv_utils.cc
Commit the vtable verification feature. This feature is designed to
[thirdparty/gcc.git] / libvtv / vtv_utils.cc
1 /* Copyright (C) 2012-2013
2 Free Software Foundation
3
4 This file is part of GCC.
5
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.
10
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.
15
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.
19
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/>. */
24
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. */
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <execinfo.h>
36 #include <unistd.h>
37 #include <errno.h>
38
39 #include "vtv_utils.h"
40
41 /* This is the directory into which all vtable verication log files
42 get written. */
43 static const char * const logs_dir = "/tmp/vtv_logs";
44 static int vtv_failures_log_fd = -1;
45
46
47
48 /* This function takes the NAME of a log file to open, attempts to
49 open it in the logs_dir directory, and returns the resulting file
50 decriptor. */
51
52 int
53 __vtv_open_log (const char *name)
54 {
55 char log_name[256];
56 snprintf (log_name, sizeof (log_name), "%s/%s", logs_dir, name);
57 mkdir (logs_dir, S_IRWXU);
58 int fd = open (log_name, O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
59 if (fd == -1)
60 __vtv_add_to_log (2, "Cannot open log file %s %s\n", name,
61 strerror (errno));
62 return fd;
63 }
64
65 /* This function takes a file descriptor (FD) and a string (STR) and
66 tries to write the string to the file. */
67
68 static int
69 vtv_log_write (int fd, const char *str)
70 {
71 if (write (fd, str, strlen (str)) != -1)
72 return 0;
73
74 if (fd != 2) /* Make sure we dont get in a loop. */
75 __vtv_add_to_log (2, "Error writing to log: %s\n", strerror (errno));
76 return -1;
77 }
78
79
80 /* This function takes a file decriptor (LOG_FILE) and an output
81 format string (FORMAT), followed by zero or more print format
82 arguments (the same as fprintf, for example). It gets the current
83 process ID and PPID, pre-pends them to the formatted message, and
84 writes write it out to the log file referenced by LOG_FILE via calles
85 to vtv_log_write. */
86
87 int
88 __vtv_add_to_log (int log_file, const char * format, ...)
89 {
90 /* We dont want to dynamically allocate this buffer. This should be
91 more than enough in most cases. It if isn't we are careful not to
92 do a buffer overflow. */
93 char output[1024];
94
95 va_list ap;
96 va_start (ap, format);
97
98 snprintf (output, sizeof (output), "VTV: PID=%d PPID=%d ", getpid (),
99 getppid ());
100 vtv_log_write (log_file, output);
101 vsnprintf (output, sizeof (output), format, ap);
102 vtv_log_write (log_file, output);
103 va_end (ap);
104
105 return 0;
106 }
107
108 /* Open error logging file, if not already open, and write vtable
109 verification failure messages (LOG_MSG) to the log file. Also
110 generate a backtrace in the log file, if GENERATE_BACKTRACE is
111 set. */
112
113 void
114 __vtv_log_verification_failure (const char *log_msg, bool generate_backtrace)
115 {
116 if (vtv_failures_log_fd == -1)
117 vtv_failures_log_fd = __vtv_open_log ("vtable_verification_failures.log");
118
119 if (vtv_failures_log_fd == -1)
120 return;
121
122 __vtv_add_to_log (vtv_failures_log_fd, "%s", log_msg);
123
124 if (generate_backtrace)
125 {
126 #define STACK_DEPTH 20
127 void *callers[STACK_DEPTH];
128 int actual_depth = backtrace (callers, STACK_DEPTH);
129 backtrace_symbols_fd (callers, actual_depth, vtv_failures_log_fd);
130 }
131 }