Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
hit_error.c
Go to the documentation of this file.
1 
2 /*
3  * <license>
4  *
5  * Hitmap v1.2
6  *
7  * This software is provided to enhance knowledge and encourage progress in the scientific
8  * community. It should be used only for research and educational purposes. Any reproduction
9  * or use for commercial purpose, public redistribution, in source or binary forms, with or
10  * without modifications, is NOT ALLOWED without the previous authorization of the copyright
11  * holder. The origin of this software must not be misrepresented; you must not claim that you
12  * wrote the original software. If you use this software for any purpose (e.g. publication),
13  * a reference to the software package and the authors must be included.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18  * THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Copyright (c) 2007-2015, Trasgo Group, Universidad de Valladolid.
26  * All rights reserved.
27  *
28  * More information on http://trasgo.infor.uva.es/
29  *
30  * </license>
31 */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 
37 #ifdef __linux__
38 #include <sys/wait.h>
39 #include <sys/types.h>
40 #include <sys/prctl.h>
41 #include <unistd.h>
42 #endif
43 
44 
45 char * get_gdb_trace(){
46 
47 // Only for LINUX
48 #ifdef __linux__
49 
50  // String trace
51  static char trace[1024*10];
52 
53  // Pipe to connect parent and child processes
54  int pipefd[2];
55 
56  // 1. CREATE THE PIPE
57  if (pipe(pipefd) == -1) {
58  fprintf(stderr,"get_trace: Error pipe\n");
59  exit(EXIT_FAILURE);
60  }
61 
62  // 2. GET THE PID AND NAME FOR THE PARENT
63  char pid_buf[30];
64  sprintf(pid_buf, "%d", getpid());
65  char name_buf[512];
66  ssize_t readed = readlink("/proc/self/exe", name_buf, 511);
67  name_buf[readed]=0;
68 
69  // 3. FORK
70  pid_t cpid = fork();
71  if (cpid == -1) {
72  fprintf(stderr,"get_trace: Error fork\n");
73  exit(EXIT_FAILURE);
74  }
75 
76  // 3.1. CHILD PROCESSOR
77  if (cpid == 0) {
78 
79  // Close the input pipe end
80  close(pipefd[0]);
81 
82  // Redirect stdout and stderr to the pipe
83  dup2(pipefd[1],1);
84  dup2(pipefd[1],2);
85 
86  // Call the process
87  printf("GDB %s - %s\n",name_buf,pid_buf);
88  execlp("gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL);
89  printf("Error calling gdb\n");
90  abort();
91 
92  // 3.2. PARENT PROCESSOR
93  } else {
94 
95  // Allow the child to see our trace
96  // @arturo: Since linux 3.4 (TODO)
97  //prctl(PR_SET_PTRACER, cpid, 0, 0, 0);
98 
99  // Close unused output pipe end
100  close(pipefd[1]);
101 
102  // Get the trace from the child
103  char buf;
104  int i = 0;
105  while (read(pipefd[0], &buf, 1) > 0){
106  trace[i++] = buf;
107  }
108  trace[i] = '\0';
109 
110  // Wait for the child
111  waitpid(cpid,NULL,0);
112  close(pipefd[0]);
113  }
114 
115  // 4. RETURN THE TRACE
116  return trace;
117 
118 #else /* Not Linux */
119 
120  // Not supported
121  return NULL;
122 
123 #endif
124 
125 }
126 
127 
128 
char * get_gdb_trace()
Definition: hit_error.c:45