Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
printsrc.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 /* ====================================================================
00037  * ====================================================================
00038  *
00039  *
00040  * Revision history:
00041  *  10-june-93 - Original Version
00042  *
00043  * Description: print the given source line alongside the current dump
00044  *
00045  * ====================================================================
00046  * ====================================================================
00047  */
00048 
00049 #ifdef _KEEP_RCS_ID
00050 static const char source_file[] = __FILE__;
00051 #endif /* _KEEP_RCS_ID */
00052 
00053 #include <stdio.h>
00054 #include "defs.h"
00055 #include "erglob.h"
00056 #include "tracing.h"
00057 #include "dwarf_DST.h"
00058 #include "dwarf_DST_mem.h"
00059 #include "printsrc.h"
00060 
00061 typedef struct {
00062   char *filename;
00063   INT incl_index;
00064   FILE *fileptr;
00065   INT32 foffset;
00066   INT32 max_line_printed;
00067   INT32 lastline;
00068 } file_info;
00069 
00070 static file_info *file_table;
00071 static char **incl_table;
00072 static INT cur_file_index = 0;
00073 static BOOL no_source;
00074 
00075 static void Gen_File_Table(void)
00076 {
00077   INT count;
00078   DST_IDX idx;
00079   DST_INCLUDE_DIR *incl;
00080   DST_FILE_NAME *file;
00081   char *name;
00082   INT incl_table_size;
00083   INT file_table_size;
00084   INT new_size;
00085 
00086   no_source = Get_Trace(TKIND_INFO, TINFO_SOURCE) != 0;
00087   if (no_source) return;
00088 
00089   incl_table_size = 0;
00090   incl_table = NULL;
00091   file_table_size = 0;
00092   file_table = NULL;
00093   count = 1;
00094   for (idx = DST_get_include_dirs (); 
00095        !DST_IS_NULL(idx); 
00096        idx = DST_INCLUDE_DIR_next(incl))
00097   {
00098     incl = DST_DIR_IDX_TO_PTR (idx);
00099     name = DST_STR_IDX_TO_PTR (DST_INCLUDE_DIR_path(incl));
00100     if (count >= incl_table_size) {
00101       new_size = count + 10;
00102       if (incl_table == NULL)
00103         incl_table = (char **) malloc (new_size * sizeof (char *));
00104       else 
00105         incl_table = (char **) realloc (incl_table, new_size * sizeof (char *));
00106       if (incl_table == NULL) ErrMsg (EC_No_Mem, "Gen_File_Table");
00107       incl_table_size = new_size;
00108     }
00109     incl_table[count] = name;
00110     count++;
00111   }
00112 
00113   count = 1;
00114   for (idx = DST_get_file_names (); 
00115        !DST_IS_NULL(idx); 
00116        idx = DST_FILE_NAME_next(file))
00117   {
00118     file = DST_FILE_IDX_TO_PTR (idx);
00119     if (DST_IS_NULL(DST_FILE_NAME_name(file))) {
00120       name = "NULLNAME";
00121     }
00122     else {
00123       name = DST_STR_IDX_TO_PTR (DST_FILE_NAME_name(file));
00124     }
00125     if (count >= file_table_size) {
00126       new_size = count + 10;
00127       if (file_table == NULL)
00128         file_table = (file_info *) malloc (new_size * sizeof (file_info));
00129       else 
00130         file_table = (file_info *) realloc (file_table, 
00131                                             new_size * sizeof (file_info));
00132       if (file_table == NULL) ErrMsg (EC_No_Mem, "Gen_File_Table");
00133       file_table_size = new_size;
00134     }
00135     file_table[count].filename = name;
00136     file_table[count].incl_index = DST_FILE_NAME_dir(file);
00137     file_table[count].fileptr = NULL;
00138     file_table[count].foffset = 0;
00139     file_table[count].max_line_printed = 0;
00140     file_table[count].lastline = 0;
00141     count++;
00142   }
00143 }
00144 
00145 
00146 /* ====================================================================
00147  *
00148  *  Print_Src_Line
00149  *
00150  *  Prints out the given line from the source file.  If the current line
00151  *  is more than 20 away from the last line printed, it will skip some
00152  *  lines and print the 20 lines leading up to the current line.  If the
00153  *  current line is smaller, it will go back and print only the current
00154  *  line.
00155  *
00156  * ==================================================================== */
00157 void Print_Src_Line(SRCPOS srcpos, FILE *f)
00158 {
00159   char buf[1024];
00160   file_info *cur_file;
00161   FILE *fileptr;
00162   INT32 line;
00163   static BOOL initialized = FALSE;
00164   INT32 filenum = SRCPOS_filenum(srcpos);
00165 
00166   if (filenum == 0) return;
00167 
00168   if (!initialized) {
00169     Gen_File_Table();
00170     initialized = TRUE;
00171   }
00172 
00173   if (no_source) return;
00174 
00175   cur_file = &file_table[filenum];
00176   if (filenum != cur_file_index) {
00177     if (cur_file_index != 0) {
00178 
00179       /* close the previous file, saving it's current position.
00180        */
00181       file_info *prev_file = &file_table[cur_file_index];
00182       prev_file->foffset = ftell(prev_file->fileptr);
00183       fclose (prev_file->fileptr);
00184       prev_file->fileptr = NULL;
00185     }
00186     cur_file_index = filenum;
00187     cur_file = &file_table[cur_file_index];
00188     /* open the new file. */
00189     sprintf (buf, "%s/%s", incl_table[cur_file->incl_index], 
00190                            cur_file->filename);
00191     cur_file->fileptr = fopen (buf, "r");
00192     if (cur_file->fileptr == NULL) {
00193       cur_file_index = 0;       /* indicate invalid cur_file */
00194       return;
00195     }
00196 
00197     /* If we've opened this file before, return to its last position.
00198      */
00199     fseek(cur_file->fileptr, cur_file->foffset, SEEK_SET);
00200   }
00201   fileptr = cur_file->fileptr;
00202 
00203   line = SRCPOS_linenum(srcpos);
00204   if (line == cur_file->lastline || fileptr == NULL) return;
00205 
00206   if (line < cur_file->lastline) {      /* need to back up */
00207     fseek(fileptr, 0, SEEK_SET);        /* reset to beginning of file */
00208     cur_file->lastline = 0;
00209   }
00210 
00211   for (;
00212        cur_file->lastline < MAX(MIN(line-1, cur_file->max_line_printed-1), line - 20); 
00213        cur_file->lastline++)
00214   {
00215     fgets(buf, sizeof(buf), fileptr);
00216   }
00217 
00218   for (; cur_file->lastline < line; cur_file->lastline++) {
00219     if (fgets(buf, sizeof(buf), fileptr) != NULL) {
00220       fprintf(f, "|||||[%d]%s", cur_file->lastline+1, buf);
00221     }
00222   }
00223 
00224   if (line > cur_file->max_line_printed) cur_file->max_line_printed = line;
00225 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines