1 /* List lines of source files for GDB, the GNU debugger. 2 Copyright (C) 1999-2023 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 #ifndef SOURCE_H 20 #define SOURCE_H 21 22 #include "gdbsupport/scoped_fd.h" 23 24 struct symtab; 25 26 /* See openp function definition for their description. */ 27 28 enum openp_flag 29 { 30 OPF_TRY_CWD_FIRST = 0x01, 31 OPF_SEARCH_IN_PATH = 0x02, 32 OPF_RETURN_REALPATH = 0x04, 33 }; 34 35 DEF_ENUM_FLAGS_TYPE(openp_flag, openp_flags); 36 37 extern int openp (const char *, openp_flags, const char *, int, 38 gdb::unique_xmalloc_ptr<char> *); 39 40 extern int source_full_path_of (const char *, gdb::unique_xmalloc_ptr<char> *); 41 42 extern void mod_path (const char *, std::string &); 43 44 extern void add_path (const char *, char **, int); 45 extern void add_path (const char *, std::string &, int); 46 47 extern void directory_switch (const char *, int); 48 49 extern std::string source_path; 50 51 extern void init_source_path (void); 52 53 /* This function is capable of finding the absolute path to a 54 source file, and opening it, provided you give it a FILENAME. Both the 55 DIRNAME and FULLNAME are only added suggestions on where to find the file. 56 57 FILENAME should be the filename to open. 58 DIRNAME is the compilation directory of a particular source file. 59 Only some debug formats provide this info. 60 FULLNAME can be the last known absolute path to the file in question. 61 Space for the path must have been malloc'd. If a path substitution 62 is applied we free the old value and set a new one. 63 64 On Success 65 A valid file descriptor is returned (the return value is positive). 66 FULLNAME is set to the absolute path to the file just opened. 67 The caller is responsible for freeing FULLNAME. 68 69 On Failure 70 An invalid file descriptor is returned (the return value is negative). 71 FULLNAME is set to NULL. */ 72 extern scoped_fd find_and_open_source (const char *filename, 73 const char *dirname, 74 gdb::unique_xmalloc_ptr<char> *fullname); 75 76 /* A wrapper for find_and_open_source that returns the full name. If 77 the full name cannot be found, a full name is constructed based on 78 the parameters, passing them through rewrite_source_path. */ 79 80 extern gdb::unique_xmalloc_ptr<char> find_source_or_rewrite 81 (const char *filename, const char *dirname); 82 83 /* Open a source file given a symtab S. Returns a file descriptor or 84 negative number for error. */ 85 extern scoped_fd open_source_file (struct symtab *s); 86 87 extern gdb::unique_xmalloc_ptr<char> rewrite_source_path (const char *path); 88 89 extern const char *symtab_to_fullname (struct symtab *s); 90 91 /* Returns filename without the compile directory part, basename or absolute 92 filename. It depends on 'set filename-display' value. */ 93 extern const char *symtab_to_filename_for_display (struct symtab *symtab); 94 95 /* Return the first line listed by print_source_lines. Used by 96 command interpreters to request listing from a previous point. If 97 0, then no source lines have yet been listed since the last time 98 the current source line was changed. */ 99 extern int get_first_line_listed (void); 100 101 /* Return the default number of lines to print with commands like the 102 cli "list". The caller of print_source_lines must use this to 103 calculate the end line and use it in the call to print_source_lines 104 as it does not automatically use this value. */ 105 extern int get_lines_to_list (void); 106 107 /* Return the current source file for listing and next line to list. 108 NOTE: The returned sal pc and end fields are not valid. */ 109 extern struct symtab_and_line get_current_source_symtab_and_line (void); 110 111 /* If the current source file for listing is not set, try and get a default. 112 Usually called before get_current_source_symtab_and_line() is called. 113 It may err out if a default cannot be determined. 114 We must be cautious about where it is called, as it can recurse as the 115 process of determining a new default may call the caller! 116 Use get_current_source_symtab_and_line only to get whatever 117 we have without erroring out or trying to get a default. */ 118 extern void set_default_source_symtab_and_line (void); 119 120 /* Return the current default file for listing and next line to list 121 (the returned sal pc and end fields are not valid.) 122 and set the current default to whatever is in SAL. 123 NOTE: The returned sal pc and end fields are not valid. */ 124 extern symtab_and_line set_current_source_symtab_and_line 125 (const symtab_and_line &sal); 126 127 /* Reset any information stored about a default file and line to print. */ 128 extern void clear_current_source_symtab_and_line (void); 129 130 /* Add a source path substitution rule. */ 131 extern void add_substitute_path_rule (const char *, const char *); 132 133 /* Flags passed as 4th argument to print_source_lines. */ 134 enum print_source_lines_flag 135 { 136 /* Do not print an error message. */ 137 PRINT_SOURCE_LINES_NOERROR = (1 << 0), 138 139 /* Print the filename in front of the source lines. */ 140 PRINT_SOURCE_LINES_FILENAME = (1 << 1) 141 }; 142 DEF_ENUM_FLAGS_TYPE (enum print_source_lines_flag, print_source_lines_flags); 143 144 /* Show source lines from the file of symtab S, starting with line 145 number LINE and stopping before line number STOPLINE. If this is 146 not the command line version, then the source is shown in the source 147 window otherwise it is simply printed. */ 148 extern void print_source_lines (struct symtab *s, int line, int stopline, 149 print_source_lines_flags flags); 150 151 /* Wrap up the logic to build a line number range for passing to 152 print_source_lines when using get_lines_to_list. An instance of this 153 class can be built from a single line number and a direction (forward or 154 backward) the range is then computed using get_lines_to_list. */ 155 class source_lines_range 156 { 157 public: 158 /* When constructing the range from a single line number, does the line 159 range extend forward, or backward. */ 160 enum direction 161 { 162 FORWARD, 163 BACKWARD 164 }; 165 166 /* Construct a SOURCE_LINES_RANGE starting at STARTLINE and extending in 167 direction DIR. The number of lines is from GET_LINES_TO_LIST. If the 168 direction is backward then the start is actually (STARTLINE - 169 GET_LINES_TO_LIST). There is also logic in place to ensure the start 170 is always 1 or more, and the end will be at most INT_MAX. */ 171 explicit source_lines_range (int startline, direction dir = FORWARD); 172 173 /* Construct a SOURCE_LINES_RANGE from STARTLINE to STOPLINE. */ 174 explicit source_lines_range (int startline, int stopline) 175 : m_startline (startline), 176 m_stopline (stopline) 177 { /* Nothing. */ } 178 179 /* Return the line to start listing from. */ 180 int startline () const 181 { return m_startline; } 182 183 /* Return the line after the last line that should be listed. */ 184 int stopline () const 185 { return m_stopline; } 186 187 private: 188 189 /* The start and end of the range. */ 190 int m_startline; 191 int m_stopline; 192 }; 193 194 /* Variation of previous print_source_lines that takes a range instead of a 195 start and end line number. */ 196 extern void print_source_lines (struct symtab *s, source_lines_range r, 197 print_source_lines_flags flags); 198 199 /* Forget line positions and file names for the symtabs in a 200 particular objfile. */ 201 extern void forget_cached_source_info_for_objfile (struct objfile *); 202 203 /* Forget what we learned about line positions in source files, and 204 which directories contain them; must check again now since files 205 may be found in a different directory now. */ 206 extern void forget_cached_source_info (void); 207 208 /* Set the source file default for the "list" command to be S. 209 210 If S is NULL, and we don't have a default, find one. This 211 should only be called when the user actually tries to use the 212 default, since we produce an error if we can't find a reasonable 213 default. Also, since this can cause symbols to be read, doing it 214 before we need to would make things slower than necessary. */ 215 extern void select_source_symtab (struct symtab *s); 216 217 #endif 218