Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifdef _KEEP_RCS_ID
00050 static const char source_file[] = __FILE__;
00051 #endif
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
00149
00150
00151
00152
00153
00154
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
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
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;
00194 return;
00195 }
00196
00197
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) {
00207 fseek(fileptr, 0, SEEK_SET);
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 }