Merge remote-tracking branch 'origin/release-v4.6.1'
[WRF.git] / tools / CodeBase / callgraph.c
blob28b5b273b169757accc3113dbdf0a8b2d81ca941
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <dirent.h>
5 #include "sym.h"
7 #define DBDIR "tools/code_dbase"
9 #define COMPARE(A,B) ( ! strncmp ( A , B , strlen( B ) ) )
10 #define COMPARE2(A,B) ( ! strcmp ( A , B ) )
11 #define INLINELEN (4*8192)
12 #define VARLEN 256
13 #define MAXRECORDS 8192
15 int recno = 1 ;
16 char outbuf[MAXRECORDS][VARLEN] ;
18 main( int argc , char * argv[] )
20 DIR *dir ;
21 char root[VARLEN] ;
22 int cut = 99 ;
23 int nentries ;
24 int i ;
26 if (( dir = opendir ( DBDIR )) == NULL ) {
27 fprintf(stderr, "Must be in top level WRF directory\n") ; exit(2) ;
28 } closedir( dir ) ;
29 if ( argc < 2 ) {
30 fprintf(stderr,"usage: callgraph root\n") ;
32 sym_init() ;
33 strcpy( root, argv[1] ) ;
34 if ( argv[2] ) {
35 cut = atoi( argv[2] ) ;
37 upper_case_str( root ) ;
38 lower_case_str( root ) ;
39 callgraph( root , 0 , cut ) ;
40 sprintf(outbuf[0],"var db = new makeArray(%d)\n",recno-1 ) ;
41 for ( i = 0 ; i <= recno ; i++ )
42 printf("%s",outbuf[i]) ;
45 callgraph ( char * root , int indent , int cut )
47 FILE * CALLEES ;
48 char fname[VARLEN] ;
49 char * p, * q, *q1, prev ;
50 char inln[INLINELEN] ;
51 char t[12][VARLEN] ;
52 int i ;
53 char * doescall ;
54 char tempbuf[VARLEN] ;
55 int thisrec ;
57 sprintf(fname,"%s/calls", DBDIR ) ;
58 /* skip some routines */
59 if (
60 COMPARE( root , "add_msg" ) || COMPARE( root , "reset_msgs" ) ||
61 COMPARE( t[2] , "get_" ) || COMPARE( t[2] , "set_" ) ||
62 COMPARE( t[2] , "mpi_" ) || COMPARE( t[2] , "ext_" ) ||
63 COMPARE( root , "stencil" ) || COMPARE( root , "wrf_debug" ) ||
64 COMPARE( root , "wrf_message" ) || COMPARE( root , "wrf_error" )
65 ) return ;
66 thisrec = recno++ ;
67 #if 0
68 sprintf(tempbuf,"db[%4d] = new dbRecord( %%s, \"%s\", \"../../WRFV2/tools/code_dbase/%s.html\", %d )\n",
69 thisrec, root, root, indent ) ;
70 #else
71 sprintf(tempbuf,"db[%4d] = new dbRecord( %%s, \"%s\", \"%s.html\", %d )\n",
72 thisrec, root, root, indent ) ;
73 #endif
75 if (( CALLEES = fopen( fname , "r" )) == NULL ) return ;
77 doescall = "false" ;
78 while ( fgets( inln, INLINELEN, CALLEES ) != NULL ) {
79 remove_nl ( inln ) ;
80 /* find first non space */
81 for ( p = inln ; *p ; p++ ) { if ( *p != ' ' ) break ; }
82 /* change multiple spaces to single space */
83 for ( q = p, q1 = q , prev = *p ; *q ; q++ )
84 { if ( prev == ' ' && *q == ' ' ) { continue ; } else { prev = *q ; *q1++ = *q ; } }
85 strcpy( inln, p ) ;
86 for ( i = 0 ; i < 11 ; i++ ) {
87 strcpy( t[i] , "" ) ;
88 get_token_n( inln, " ", i , t[i] ) ; remove_nl(t[i]) ; lower_case_str( t[i] ) ;
90 if ( COMPARE2( t[0] , root ) && COMPARE2( t[1] , "calls" ) && ! COMPARE2( t[0] , t[2] ) &&
91 ! (
92 COMPARE( root , "add_msg" ) || COMPARE( root , "reset_msgs" ) ||
93 COMPARE( t[2] , "get_" ) || COMPARE( t[2] , "set_" ) ||
94 COMPARE( t[2] , "mpi_" ) || COMPARE( t[2] , "ext_" ) ||
95 COMPARE( root , "stencil" ) || COMPARE( root , "wrf_debug" ) ||
96 COMPARE( root , "wrf_message" ) || COMPARE( root , "wrf_error" )
97 )) {
98 if ( indent <= cut && ( ! sym_get ( t[2] ) ) ) {
99 sym_add( t[2] ) ;
100 doescall = "true" ;
101 callgraph ( t[2] , indent + 1, cut ) ;
105 sprintf(outbuf[thisrec],tempbuf,doescall) ;
106 fclose( CALLEES ) ;
109 #if 0
110 /*******************************************************************/
112 /* open the file of calls and count them */
113 count_entries ( char * root , int * nentries )
115 FILE * CALLEES ;
116 char fname[VARLEN] ;
117 char inln[INLINELEN] ;
118 char * p, *q, *q1, prev ;
119 char t[12][VARLEN] ;
120 int i ;
122 sprintf(fname,"%s/calls", DBDIR ) ;
123 if (( CALLEES = fopen( fname , "r" )) == NULL ) return ;
124 *nentries = 0 ;
125 while ( fgets( inln, INLINELEN, CALLEES ) != NULL ) {
126 remove_nl ( inln ) ;
127 /* find first non space */
128 for ( p = inln ; *p ; p++ ) { if ( *p != ' ' ) break ; }
129 /* change multiple spaces to single space */
130 for ( q = p, q1 = q , prev = *p ; *q ; q++ )
131 { if ( prev == ' ' && *q == ' ' ) { continue ; } else { prev = *q ; *q1++ = *q ; } }
132 strcpy( inln, p ) ;
133 for ( i = 0 ; i < 11 ; i++ ) {
134 strcpy( t[i] , "" ) ;
135 get_token_n( inln, " ", i , t[i] ) ; remove_nl(t[i]) ; lower_case_str( t[i] ) ;
137 if ( COMPARE2( t[0] , root ) && COMPARE2( t[1] , "calls" ) && ! COMPARE2( t[0] , t[2] ) &&
139 COMPARE( root , "add_msg" ) || COMPARE( root , "reset_msgs" ) ||
140 COMPARE( t[2] , "get_" ) || COMPARE( t[2] , "set_" ) ||
141 COMPARE( t[2] , "mpi_" ) || COMPARE( t[2] , "ext_" ) ||
142 COMPARE( root , "stencil" ) || COMPARE( root , "wrf_debug" ) ||
143 COMPARE( root , "wrf_message" ) || COMPARE( root , "wrf_error" )
144 )) {
145 sym_add( t[2] ) ;
146 (*nentries)++ ;
149 fclose( CALLEES ) ;
154 /* old version before adding javascript calltree */
155 main( int argc , char * argv[] )
157 DIR *dir ;
158 char root[VARLEN] ;
159 int cut = 99 ;
161 if (( dir = opendir ( DBDIR )) == NULL ) {
162 fprintf(stderr, "Must be in top level WRF directory\n") ; exit(2) ;
163 } closedir( dir ) ;
164 if ( argc < 2 ) {
165 fprintf(stderr,"usage: callgraph root\n") ;
167 sym_init() ;
168 strcpy( root, argv[1] ) ;
169 if ( argv[2] ) {
170 cut = atoi( argv[2] ) ;
172 upper_case_str( root ) ;
173 printf("<html>\n" ) ;
174 printf("<title> %s Call Tree </title>\n", root ) ;
175 printf("<body>\n" ) ;
176 printf("<h1> %s Call Tree </h1>\n", root ) ;
177 printf("<p><a href=index.html>[1] </a> " ) ;
178 printf("<a href=ct2.html>[2] </a> " ) ;
179 printf("<a href=ct3.html>[3] </a> " ) ;
180 printf("<a href=ct4.html>[4] </a> " ) ;
181 printf("<a href=ct5.html>[5] </a> " ) ;
182 printf("<a href=ctall.html>[all] </a><p>\n" ) ;
183 lower_case_str( root ) ;
184 callgraph( root , 0 , cut ) ;
185 printf("</body>\n" ) ;
186 printf("</html>\n" ) ;
189 callgraph ( char * root , int indent , int cut )
191 FILE * CALLEES ;
192 char fname[VARLEN] ;
193 char * p, * q, *q1, prev ;
194 char inln[INLINELEN] ;
195 char t[12][VARLEN] ;
196 int i ;
197 int recno = 1 ;
199 sprintf(fname,"%s/calls", DBDIR ) ;
200 if (( CALLEES = fopen( fname , "r" )) == NULL ) return ;
201 if (
202 COMPARE( root , "add_msg" ) || COMPARE( root , "reset_msgs" ) ||
203 COMPARE( t[2] , "get_" ) || COMPARE( t[2] , "set_" ) ||
204 COMPARE( t[2] , "mpi_" ) || COMPARE( t[2] , "ext_" ) ||
205 COMPARE( root , "stencil" ) || COMPARE( root , "wrf_debug" ) ||
206 COMPARE( root , "wrf_message" ) || COMPARE( root , "wrf_error" )
207 ) return ;
208 for ( i = 0 ; i < indent ; i++ ) printf(" ! &nbsp &nbsp &nbsp ") ;
209 printf(" <a href=\"%s.html\"> %s </a><br>\n", root, root ) ;
211 while ( fgets( inln, INLINELEN, CALLEES ) != NULL ) {
212 remove_nl ( inln ) ;
213 /* find first non space */
214 for ( p = inln ; *p ; p++ ) { if ( *p != ' ' ) break ; }
215 /* change multiple spaces to single space */
216 for ( q = p, q1 = q , prev = *p ; *q ; q++ )
217 { if ( prev == ' ' && *q == ' ' ) { continue ; } else { prev = *q ; *q1++ = *q ; } }
218 strcpy( inln, p ) ;
219 for ( i = 0 ; i < 11 ; i++ ) {
220 strcpy( t[i] , "" ) ;
221 get_token_n( inln, " ", i , t[i] ) ; remove_nl(t[i]) ; lower_case_str( t[i] ) ;
223 if ( COMPARE2( t[0] , root ) && COMPARE2( t[1] , "calls" ) && ! COMPARE2( t[0] , t[2] ) &&
225 COMPARE( root , "add_msg" ) || COMPARE( root , "reset_msgs" ) ||
226 COMPARE( t[2] , "get_" ) || COMPARE( t[2] , "set_" ) ||
227 COMPARE( t[2] , "mpi_" ) || COMPARE( t[2] , "ext_" ) ||
228 COMPARE( root , "stencil" ) || COMPARE( root , "wrf_debug" ) ||
229 COMPARE( root , "wrf_message" ) || COMPARE( root , "wrf_error" )
230 )) {
231 if ( indent <= cut && ( ! sym_get ( t[2] ) ) ) {
232 sym_add( t[2] ) ;
233 callgraph ( t[2] , indent + 1, cut ) ;
238 fclose( CALLEES ) ;
240 #####################
241 # original old PERL code kept just for reference
243 $dbdir = "tools/code_dbase" ;
245 if ( ! opendir( TOOLDIR, "tools") ) {
246 print "\nMust be in top level WRF directory\n" ;
247 exit ;
249 closedir TOOLDIR ;
251 if ( ( scalar @ARGV < 1 ) ) {
252 print "usage: callgraph root\n" ;
253 exit ;
256 $rout1 = lc $ARGV[0] ;
257 $rout = $rout1 ;
259 $routfile = $dbdir."/".$rout ;
261 if ( $indent == 0 ) {
262 print "<html>\n" ;
263 print "<title> ",uc $rout," Call Tree </title>\n" ;
264 print "<body>\n" ;
265 print "<h1> ",uc $rout," Call Tree </h1>\n" ;
268 $indent = 0 ;
270 $first = 1 ;
272 open CALLEES, "< $dbdir/calls" or die " cannot open $dbdir/calls " ;
273 while ( <CALLEES> ) {
274 @t = split ' ' ;
275 if ( $t[0] eq lc $rout && $t[1] eq "calls" && ! ( $t[2] eq $t[0] ) && ! ($t[2] =~ add_msg) && !($t[2] =~ reset_msgs ) && ! ($t[2] =~ stencil) && !($t[2] =~ wrf_debug) ) {
276 if ( $first == 1 ) {
277 for ( $i = 0 ; $i < $indent ; $i++ ) { print "| &nbsp " ; }
278 print "<a href=\"$rout\.html\"> $rout </a><br>\n" ;
279 $first = 0 ;
281 $i2 = $indent + 1 ;
282 if ( $i2 < 7 && (! $prune{$t[2]} ) ) {
283 $prune{$t[2]} = "y" ;
284 $opstr = "tools/callgraph $t[2] $i2 |" ;
285 ####### RECURSE ##########
286 open D, $opstr ;
287 while ( <D> ) { print ; }
288 close D ;
292 if ( $indent == 0 ) {
293 print "</body>\n" ;
294 print "</html>\n" ;
297 exit
298 #endif