1 //===-- CompileUnit.h -------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef liblldb_CompUnit_h_ 10 #define liblldb_CompUnit_h_ 11 12 #include "lldb/Core/FileSpecList.h" 13 #include "lldb/Core/ModuleChild.h" 14 #include "lldb/Symbol/DebugMacros.h" 15 #include "lldb/Symbol/Function.h" 16 #include "lldb/Symbol/SourceModule.h" 17 #include "lldb/Utility/Stream.h" 18 #include "lldb/Utility/UserID.h" 19 #include "lldb/lldb-enumerations.h" 20 21 #include "llvm/ADT/DenseMap.h" 22 23 namespace lldb_private { 24 /// \class CompileUnit CompileUnit.h "lldb/Symbol/CompileUnit.h" 25 /// A class that describes a compilation unit. 26 /// 27 /// A representation of a compilation unit, or compiled source file. 28 /// The UserID of the compile unit is specified by the SymbolFile plug-in and 29 /// can have any value as long as the value is unique within the Module that 30 /// owns this compile units. 31 /// 32 /// Each compile unit has a list of functions, global and static variables, 33 /// support file list (include files and inlined source files), and a line 34 /// table. 35 class CompileUnit : public std::enable_shared_from_this<CompileUnit>, 36 public ModuleChild, 37 public FileSpec, 38 public UserID, 39 public SymbolContextScope { 40 public: 41 /// Construct with a module, path, UID and language. 42 /// 43 /// Initialize the compile unit given the owning \a module, a path to 44 /// convert into a FileSpec, the SymbolFile plug-in supplied \a uid, and the 45 /// source language type. 46 /// 47 /// \param[in] module 48 /// The parent module that owns this compile unit. This value 49 /// must be a valid pointer value. 50 /// 51 /// \param[in] user_data 52 /// User data where the SymbolFile parser can store data. 53 /// 54 /// \param[in] pathname 55 /// The path to the source file for this compile unit. 56 /// 57 /// \param[in] uid 58 /// The user ID of the compile unit. This value is supplied by 59 /// the SymbolFile plug-in and should be a value that allows 60 /// the SymbolFile plug-in to easily locate and parse additional 61 /// information for the compile unit. 62 /// 63 /// \param[in] language 64 /// A language enumeration type that describes the main language 65 /// of this compile unit. 66 /// 67 /// \param[in] is_optimized 68 /// A value that can initialized with eLazyBoolYes, eLazyBoolNo 69 /// or eLazyBoolCalculate. If set to eLazyBoolCalculate, then 70 /// an extra call into SymbolVendor will be made to calculate if 71 /// the compile unit is optimized will be made when 72 /// CompileUnit::GetIsOptimized() is called. 73 /// 74 /// \see lldb::LanguageType 75 CompileUnit(const lldb::ModuleSP &module_sp, void *user_data, 76 const char *pathname, lldb::user_id_t uid, 77 lldb::LanguageType language, lldb_private::LazyBool is_optimized); 78 79 /// Construct with a module, file spec, UID and language. 80 /// 81 /// Initialize the compile unit given the owning \a module, a path to 82 /// convert into a FileSpec, the SymbolFile plug-in supplied \a uid, and the 83 /// source language type. 84 /// 85 /// \param[in] module 86 /// The parent module that owns this compile unit. This value 87 /// must be a valid pointer value. 88 /// 89 /// \param[in] user_data 90 /// User data where the SymbolFile parser can store data. 91 /// 92 /// \param[in] file_spec 93 /// The file specification for the source file of this compile 94 /// unit. 95 /// 96 /// \param[in] uid 97 /// The user ID of the compile unit. This value is supplied by 98 /// the SymbolFile plug-in and should be a value that allows 99 /// the plug-in to easily locate and parse 100 /// additional information for the compile unit. 101 /// 102 /// \param[in] language 103 /// A language enumeration type that describes the main language 104 /// of this compile unit. 105 /// 106 /// \param[in] is_optimized 107 /// A value that can initialized with eLazyBoolYes, eLazyBoolNo 108 /// or eLazyBoolCalculate. If set to eLazyBoolCalculate, then 109 /// an extra call into SymbolVendor will be made to calculate if 110 /// the compile unit is optimized will be made when 111 /// CompileUnit::GetIsOptimized() is called. 112 /// 113 /// \see lldb::LanguageType 114 CompileUnit(const lldb::ModuleSP &module_sp, void *user_data, 115 const FileSpec &file_spec, lldb::user_id_t uid, 116 lldb::LanguageType language, lldb_private::LazyBool is_optimized); 117 118 /// Destructor 119 ~CompileUnit() override; 120 121 /// Add a function to this compile unit. 122 /// 123 /// Typically called by the SymbolFile plug-ins as they partially parse the 124 /// debug information. 125 /// 126 /// \param[in] function_sp 127 /// A shared pointer to the Function object. 128 void AddFunction(lldb::FunctionSP &function_sp); 129 130 /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*) 131 /// 132 /// \see SymbolContextScope 133 void CalculateSymbolContext(SymbolContext *sc) override; 134 135 lldb::ModuleSP CalculateSymbolContextModule() override; 136 137 CompileUnit *CalculateSymbolContextCompileUnit() override; 138 139 /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*) 140 /// 141 /// \see SymbolContextScope 142 void DumpSymbolContext(Stream *s) override; 143 144 lldb::LanguageType GetLanguage(); 145 146 void SetLanguage(lldb::LanguageType language) { 147 m_flags.Set(flagsParsedLanguage); 148 m_language = language; 149 } 150 151 void GetDescription(Stream *s, lldb::DescriptionLevel level) const; 152 153 /// Apply a lambda to each function in this compile unit. 154 /// 155 /// This provides raw access to the function shared pointer list and will not 156 /// cause the SymbolFile plug-in to parse any unparsed functions. 157 /// 158 /// \note Prefer using FindFunctionByUID over this if possible. 159 /// 160 /// \param[in] lambda 161 /// The lambda that should be applied to every function. The lambda can 162 /// return true if the iteration should be aborted earlier. 163 void ForeachFunction( 164 llvm::function_ref<bool(const lldb::FunctionSP &)> lambda) const; 165 166 /// Dump the compile unit contents to the stream \a s. 167 /// 168 /// \param[in] s 169 /// The stream to which to dump the object description. 170 /// 171 /// \param[in] show_context 172 /// If \b true, variables will dump their symbol context 173 /// information. 174 void Dump(Stream *s, bool show_context) const; 175 176 /// Find the line entry by line and optional inlined file spec. 177 /// 178 /// Finds the first line entry that has an index greater than \a start_idx 179 /// that matches \a line. If \a file_spec_ptr is NULL, then the search 180 /// matches line entries whose file matches the file for the compile unit. 181 /// If \a file_spec_ptr is not NULL, line entries must match the specified 182 /// file spec (for inlined line table entries). 183 /// 184 /// Multiple calls to this function can find all entries that match a given 185 /// file and line by starting with \a start_idx equal to zero, and calling 186 /// this function back with the return value + 1. 187 /// 188 /// \param[in] start_idx 189 /// The zero based index at which to start looking for matches. 190 /// 191 /// \param[in] line 192 /// The line number to search for. 193 /// 194 /// \param[in] file_spec_ptr 195 /// If non-NULL search for entries that match this file spec, 196 /// else if NULL, search for line entries that match the compile 197 /// unit file. 198 /// 199 /// \param[in] exact 200 /// If \btrue match only if there is a line table entry for this line 201 /// number. 202 /// If \bfalse, find the line table entry equal to or after this line 203 /// number. 204 /// 205 /// \param[out] line_entry 206 /// If non-NULL, a copy of the line entry that was found. 207 /// 208 /// \return 209 /// The zero based index of a matching line entry, or UINT32_MAX 210 /// if no matching line entry is found. 211 uint32_t FindLineEntry(uint32_t start_idx, uint32_t line, 212 const FileSpec *file_spec_ptr, bool exact, 213 LineEntry *line_entry); 214 215 /// Get the line table for the compile unit. 216 /// 217 /// Called by clients and the SymbolFile plug-in. The SymbolFile plug-ins 218 /// use this function to determine if the line table has be parsed yet. 219 /// Clients use this function to get the line table from a compile unit. 220 /// 221 /// \return 222 /// The line table object pointer, or NULL if this line table 223 /// hasn't been parsed yet. 224 LineTable *GetLineTable(); 225 226 DebugMacros *GetDebugMacros(); 227 228 /// Get the compile unit's support file list. 229 /// 230 /// The support file list is used by the line table, and any objects that 231 /// have valid Declaration objects. 232 /// 233 /// \return 234 /// A support file list object. 235 const FileSpecList &GetSupportFiles(); 236 237 /// Get the compile unit's imported module list. 238 /// 239 /// This reports all the imports that the compile unit made, including the 240 /// current module. 241 /// 242 /// \return 243 /// A list of imported modules. 244 const std::vector<SourceModule> &GetImportedModules(); 245 246 /// Get the SymbolFile plug-in user data. 247 /// 248 /// SymbolFile plug-ins can store user data to internal state or objects to 249 /// quickly allow them to parse more information for a given object. 250 /// 251 /// \return 252 /// The user data stored with the CompileUnit when it was 253 /// constructed. 254 void *GetUserData() const; 255 256 /// Get the variable list for a compile unit. 257 /// 258 /// Called by clients to get the variable list for a compile unit. The 259 /// variable list will contain all global and static variables that were 260 /// defined at the compile unit level. 261 /// 262 /// \param[in] can_create 263 /// If \b true, the variable list will be parsed on demand. If 264 /// \b false, the current variable list will be returned even 265 /// if it contains a NULL VariableList object (typically 266 /// called by dumping routines that want to display only what 267 /// has currently been parsed). 268 /// 269 /// \return 270 /// A shared pointer to a variable list, that can contain NULL 271 /// VariableList pointer if there are no global or static 272 /// variables. 273 lldb::VariableListSP GetVariableList(bool can_create); 274 275 /// Finds a function by user ID. 276 /// 277 /// Typically used by SymbolFile plug-ins when partially parsing the debug 278 /// information to see if the function has been parsed yet. 279 /// 280 /// \param[in] uid 281 /// The user ID of the function to find. This value is supplied 282 /// by the SymbolFile plug-in and should be a value that 283 /// allows the plug-in to easily locate and parse additional 284 /// information in the function. 285 /// 286 /// \return 287 /// A shared pointer to the function object that might contain 288 /// a NULL Function pointer. 289 lldb::FunctionSP FindFunctionByUID(lldb::user_id_t uid); 290 291 /// Set the line table for the compile unit. 292 /// 293 /// Called by the SymbolFile plug-in when if first parses the line table and 294 /// hands ownership of the line table to this object. The compile unit owns 295 /// the line table object and will delete the object when it is deleted. 296 /// 297 /// \param[in] line_table 298 /// A line table object pointer that this object now owns. 299 void SetLineTable(LineTable *line_table); 300 301 void SetSupportFiles(const FileSpecList &support_files); 302 303 void SetDebugMacros(const DebugMacrosSP &debug_macros); 304 305 /// Set accessor for the variable list. 306 /// 307 /// Called by the SymbolFile plug-ins after they have parsed the variable 308 /// lists and are ready to hand ownership of the list over to this object. 309 /// 310 /// \param[in] variable_list_sp 311 /// A shared pointer to a VariableList. 312 void SetVariableList(lldb::VariableListSP &variable_list_sp); 313 314 /// Resolve symbol contexts by file and line. 315 /// 316 /// Given a file in \a file_spec, and a line number, find all instances and 317 /// append them to the supplied symbol context list \a sc_list. 318 /// 319 /// \param[in] file_spec 320 /// A file specification. If \a file_spec contains no directory 321 /// information, only the basename will be used when matching 322 /// contexts. If the directory in \a file_spec is valid, a 323 /// complete file specification match will be performed. 324 /// 325 /// \param[in] line 326 /// The line number to match against the compile unit's line 327 /// tables. 328 /// 329 /// \param[in] check_inlines 330 /// If \b true this function will also match any inline 331 /// file and line matches. If \b false, the compile unit's 332 /// file specification must match \a file_spec for any matches 333 /// to be returned. 334 /// 335 /// \param[in] exact 336 /// If true, only resolve the context if \a line exists in the line table. 337 /// If false, resolve the context to the closest line greater than \a line 338 /// in the line table. 339 /// 340 /// \param[in] resolve_scope 341 /// For each matching line entry, this bitfield indicates what 342 /// values within each SymbolContext that gets added to \a 343 /// sc_list will be resolved. See the SymbolContext::Scope 344 /// enumeration for a list of all available bits that can be 345 /// resolved. Only SymbolContext entries that can be resolved 346 /// using a LineEntry base address will be able to be resolved. 347 /// 348 /// \param[out] sc_list 349 /// A SymbolContext list class that will get any matching 350 /// entries appended to. 351 /// 352 /// \return 353 /// The number of new matches that were added to \a sc_list. 354 /// 355 /// \see enum SymbolContext::Scope 356 uint32_t ResolveSymbolContext(const FileSpec &file_spec, uint32_t line, 357 bool check_inlines, bool exact, 358 lldb::SymbolContextItem resolve_scope, 359 SymbolContextList &sc_list); 360 361 /// Get whether compiler optimizations were enabled for this compile unit 362 /// 363 /// "optimized" means that the debug experience may be difficult for the 364 /// user to understand. Variables may not be available when the developer 365 /// would expect them, stepping through the source lines in the function may 366 /// appear strange, etc. 367 /// 368 /// \return 369 /// Returns 'true' if this compile unit was compiled with 370 /// optimization. 'false' indicates that either the optimization 371 /// is unknown, or this compile unit was built without optimization. 372 bool GetIsOptimized(); 373 374 /// Returns the number of functions in this compile unit 375 size_t GetNumFunctions() const { return m_functions_by_uid.size(); } 376 377 protected: 378 /// User data for the SymbolFile parser to store information into. 379 void *m_user_data; 380 /// The programming language enumeration value. 381 lldb::LanguageType m_language; 382 /// Compile unit flags that help with partial parsing. 383 Flags m_flags; 384 /// Maps UIDs to functions. 385 llvm::DenseMap<lldb::user_id_t, lldb::FunctionSP> m_functions_by_uid; 386 /// All modules, including the current module, imported by this 387 /// compile unit. 388 std::vector<SourceModule> m_imported_modules; 389 /// Files associated with this compile unit's line table and 390 /// declarations. 391 FileSpecList m_support_files; 392 /// Line table that will get parsed on demand. 393 std::unique_ptr<LineTable> m_line_table_up; 394 /// Debug macros that will get parsed on demand. 395 DebugMacrosSP m_debug_macros_sp; 396 /// Global and static variable list that will get parsed on demand. 397 lldb::VariableListSP m_variables; 398 /// eLazyBoolYes if this compile unit was compiled with 399 /// optimization. 400 lldb_private::LazyBool m_is_optimized; 401 402 private: 403 enum { 404 flagsParsedAllFunctions = 405 (1u << 0), ///< Have we already parsed all our functions 406 flagsParsedVariables = 407 (1u << 1), ///< Have we already parsed globals and statics? 408 flagsParsedSupportFiles = (1u << 2), ///< Have we already parsed the support 409 ///files for this compile unit? 410 flagsParsedLineTable = 411 (1u << 3), ///< Have we parsed the line table already? 412 flagsParsedLanguage = (1u << 4), ///< Have we parsed the language already? 413 flagsParsedImportedModules = 414 (1u << 5), ///< Have we parsed the imported modules already? 415 flagsParsedDebugMacros = 416 (1u << 6) ///< Have we parsed the debug macros already? 417 }; 418 419 DISALLOW_COPY_AND_ASSIGN(CompileUnit); 420 }; 421 422 } // namespace lldb_private 423 424 #endif // liblldb_CompUnit_h_ 425