Geant4_10
filter.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <unistd.h>
6 
7 int position = -1;
8 
9 //#define ADDRESSTABLEDEBUG 1
10 
12 {
13  int counter;
14  int space;
15  unsigned long *addresses;
16 } Detected;
17 
18 int bSearchAddress(unsigned long address, int head, int tail)
19 {
20  if (head >= tail)
21  {
22  position = tail;
23 
24  return -1;
25  }
26  int middle = (head + tail) / 2;
27  if (Detected.addresses[middle] == address)
28  {
29  position = middle;
30  return 0;
31  }
32  else if (Detected.addresses[middle] < address)
33  return bSearchAddress(address, middle + 1, tail);
34  else
35  return bSearchAddress(address, 0, middle);
36 }
37 
38 int findAddress(unsigned long address)
39 {
40  if (position == -1) return -1;
41  return bSearchAddress(address, 0, Detected.counter);
42 }
43 
44 int insertAddress(unsigned long address)
45 {
46  if (position == -1)
47  {
48  Detected.space = 4096;
49  assert(0 != (Detected.addresses = (unsigned long *) malloc(sizeof(unsigned long) * Detected.space)));
50  Detected.counter = 1;
51  position = 0;
52  #ifdef ADDRESSTABLEDEBUG
53  printf("Inserted position: %d, address: 0x%lx.\n", position, address);
54  #endif
55  Detected.addresses[position] = address;
56  return 0;
57  }
58  else
59  {
60  if (-1 == findAddress(address))
61  {
62  #ifdef ADDRESSTABLEDEBUG
63  printf("Inserted position: %d, address: 0x%lx.\n", position, address);
64  #endif
65  Detected.counter++;
67  {
68  Detected.space = Detected.space + 4096;
69 
70  assert(0 != (Detected.addresses = (unsigned long *) realloc((char *)Detected.addresses, sizeof(unsigned long) * Detected.space)));
71  }
72  int i;
73  for (i = Detected.counter - 1; i > position ; i--)
75  Detected.addresses[i] = address;
76 
77  return 0;
78  }
79  else
80  return -1;
81  }
82 }
83 
84 void outputAddresses(char *cmd)
85 {
86  int i;
87  char convertcmd[1024];
88  printf("Lines that produce segmentation faults:\n");
89  for (i = 0 ; i < Detected.counter ; i++)
90  {
91  sprintf(convertcmd, "addr2line -e %s 0x%lx", cmd, Detected.addresses[i]);
92  system(convertcmd);
93  printf("%s address[%d]= 0x%lx\n", cmd, i, Detected.addresses[i]);
94  }
95 }
96 
98 {
99  int i;
100 
101  pid_t myselfpid = getpid();
102  char gdbcommands[1024];
103  gdbcommands[0] = 0;
104  sprintf(gdbcommands, "./gdbcommands.%d", myselfpid);
105 
106  FILE *fp = fopen(gdbcommands, "w+");
107  for (i = 0 ; i < Detected.counter ; i++)
108  {
109  fprintf(fp, "break *0x%lx\n", Detected.addresses[i]);
110  fprintf(fp, "commands\n");
111  fprintf(fp, "bt 10\n");
112  fprintf(fp, "cont\n");
113  fprintf(fp, "end\n");
114  }
115  fprintf(fp, "cont\n");
116  fclose(fp);
117 }
118 
119 #ifdef ADDRESSTABLEDEBUG
120 int main(int argc, char *argv[])
121 {
122  insertAddress(1423423);
123  insertAddress(1238743);
124  insertAddress(2342134);
125  outputAddresses();
126 }
127 #endif
void outputAddresses(char *cmd)
Definition: filter.cc:84
struct AddressTable Detected
char cmd[1024]
Definition: tracer.cxx:25
#define assert(x)
Definition: mymalloc.cc:1309
int main(int argc, char **argv)
Definition: genwindef.cpp:30
int space
Definition: filter.cc:14
int insertAddress(unsigned long address)
Definition: filter.cc:44
int counter
Definition: filter.cc:13
void generategdbscripts()
Definition: filter.cc:97
int bSearchAddress(unsigned long address, int head, int tail)
Definition: filter.cc:18
int position
Definition: filter.cc:7
system("rm -rf dna.root")
int findAddress(unsigned long address)
Definition: filter.cc:38
printf("%d Experimental points found\n", nlines)
fclose(fp)
void * realloc(void *__ptr, size_t __size)
Definition: hjmalloc.cc:103
unsigned long * addresses
Definition: filter.cc:15
void * malloc(size_t __size)
Definition: hjmalloc.cc:30