libabigail
abg-tools-utils.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2023 Red Hat, Inc.
5 
6 ///@file
7 
8 #ifndef __ABG_TOOLS_UTILS_H
9 #define __ABG_TOOLS_UTILS_H
10 
11 #include <iostream>
12 #include <istream>
13 #include <memory>
14 #include <ostream>
15 #include <set>
16 #include <string>
17 #include "abg-suppression.h"
18 #include "abg-elf-based-reader.h"
19 
20 namespace abigail
21 {
22 
23 namespace tools_utils
24 {
25 
26 using std::ostream;
27 using std::istream;
28 using std::ifstream;
29 using std::string;
30 using std::set;
31 using std::shared_ptr;
32 
33 void initialize();
34 const char* get_system_libdir();
39 
40 bool file_exists(const string&);
41 bool is_regular_file(const string&);
42 bool file_has_dwarf_debug_info(const string& elf_file_path,
43  const vector<char**>& debug_info_root_paths);
44 bool file_has_ctf_debug_info(const string& elf_file_path,
45  const vector<char**>& debug_info_root_paths);
46 bool file_has_btf_debug_info(const string& elf_file_path,
47  const vector<char**>& debug_info_root_paths);
48 bool is_dir(const string&);
49 bool dir_exists(const string&);
50 bool dir_is_empty(const string &);
51 bool decl_names_equal(const string&, const string&);
52 bool maybe_get_symlink_target_file_path(const string& file_path,
53  string& target_path);
54 bool base_name(string const& path,
55  string& file_name);
56 bool dir_name(string const &path,
57  string& path_dir_name,
58  bool keep_separator_at_end=false);
59 void real_path(const string&path, string& realpath);
60 bool ensure_dir_path_created(const string&);
61 bool ensure_parent_dir_created(const string&);
62 ostream& emit_prefix(const string& prog_name, ostream& out);
63 bool check_file(const string& path, ostream& out, const string& prog_name = "");
64 bool check_dir(const string& path, ostream& out, const string& prog_name="");
65 bool string_ends_with(const string&, const string&);
66 bool string_begins_with(const string&, const string&);
67 bool string_is_ascii(const string&);
68 bool string_is_ascii_identifier(const string&);
69 bool split_string(const string&, const string&, vector<string>&);
70 bool string_suffix(const string&, const string&, string&);
71 bool sorted_strings_common_prefix(vector<string>&, string&);
74 bool execute_command_and_get_output(const string&, vector<string>&);
75 void get_comma_separated_args_of_option(const string& input_str,
76  const string& option,
77  vector<string>& arguments);
78 bool get_dsos_provided_by_rpm(const string& rpm_path,
79  set<string>& provided_dsos);
80 string trim_white_space(const string&);
81 string trim_leading_string(const string& from, const string& to_trim);
82 void convert_char_stars_to_char_star_stars(const vector<char*>&,
83  vector<char**>&);
84 
86 gen_suppr_spec_from_headers(const string& hdrs_root_dir);
87 
89 gen_suppr_spec_from_headers(const string& hdrs_root_dir,
90  const vector<string>& hdr_files);
91 
93 gen_suppr_spec_from_headers(const vector<string>& headers_root_dirs,
94  const vector<string>& header_files);
95 
98  (const vector<string>& abi_whitelist_paths);
99 
100 bool
101 get_vmlinux_path_from_kernel_dist(const string& from,
102  string& vmlinux_path);
103 
104 bool
105 get_binary_paths_from_kernel_dist(const string& dist_root,
106  const string& debug_info_root_path,
107  string& vmlinux_path,
108  vector<string>& module_paths);
109 
110 bool
111 get_binary_paths_from_kernel_dist(const string& dist_root,
112  string& vmlinux_path,
113  vector<string>& module_paths);
114 
115 string
117 
118 string
120 
121 void
123 
124 void
126 
127 bool
128 find_file_under_dir(const string& root_dir,
129  const string& file_path_to_look_for,
130  string& result);
131 
132 bool
133 find_file_under_dirs(const vector<string>& root_dirs,
134  const string& file_path_to_look_for,
135  string& result);
136 
137 bool
138 get_dependencies(const corpus&, const vector<string>&, set<string>&);
139 
140 void
141 add_binaries_into_corpus_group(const fe_iface_sptr& reader,
142  const vector<string>& binaries,
143  const vector<string>& deps_dirs,
144  corpus_group& group);
145 
146 void
147 add_dependencies_into_corpus_group(const fe_iface_sptr& reader,
148  const corpus& korpus,
149  const vector<string>& deps_dirs,
150  corpus_group& group);
151 
152 corpus_group_sptr
153 stick_corpus_and_binaries_into_corpus_group(const fe_iface_sptr& reader,
154  const corpus_sptr& korpus,
155  const vector<string>& binaries,
156  const vector<string>& deps_dirs);
157 
158 corpus_group_sptr
159 stick_corpus_and_dependencies_into_corpus_group(const fe_iface_sptr& reader,
160  const corpus_sptr& korpus,
161  const vector<string>& deps_dirs);
162 
163 
164 class temp_file;
165 
166 /// Convenience typedef for a shared_ptr to @ref temp_file.
167 typedef shared_ptr<temp_file> temp_file_sptr;
168 
169 /// A temporary file.
170 ///
171 /// This is a helper file around the mkstemp API.
172 ///
173 /// Once the temporary file is created, users can interact with it
174 /// using an fstream. They can also get the path to the newly
175 /// created temporary file.
176 ///
177 /// When the instance of @ref temp_file is destroyed, the underlying
178 /// resources are de-allocated, the underlying temporary file is
179 /// closed and removed.
181 {
182  struct priv;
183  std::unique_ptr<priv> priv_;
184 
185  temp_file();
186 
187 public:
188 
189  bool
190  is_good() const;
191 
192  const char*
193  get_path() const;
194 
195  std::fstream&
196  get_stream();
197 
198  static temp_file_sptr
199  create();
200 }; // end class temp_file
201 
202 size_t
204 
205 string
207 
208 /// The different types of files understood the bi* suite of tools.
210 {
211  /// A file type we don't know about.
213  /// The native xml file format representing a translation unit.
215  /// An elf file. Read this kind of file should yield an
216  /// abigail::corpus type.
218  /// An archive (AR) file.
220  // A native abixml file format representing a corpus of one or
221  // several translation units.
222  FILE_TYPE_XML_CORPUS,
223  // A native abixml file format representing a corpus group of one or
224  // several corpora.
225  FILE_TYPE_XML_CORPUS_GROUP,
226  /// An RPM (.rpm) binary file
228  /// An SRPM (.src.rpm) file
230  /// A DEB (.deb) binary file
232  /// A plain directory
234  /// A tar archive. The archive can be compressed with the popular
235  /// compression schemes recognized by GNU tar.
237 };
238 
239 /// Exit status for abidiff and abicompat tools.
240 ///
241 /// It's actually a bit mask. The value of each enumerator is a power
242 /// of two.
244 {
245  /// This is for when the compared ABIs are equal.
246  ///
247  /// Its numerical value is 0.
249 
250  /// This bit is set if there is an application error.
251  ///
252  /// Its numerical value is 1.
254 
255  /// This bit is set if the tool is invoked in an non appropriate
256  /// manner.
257  ///
258  /// Its numerical value is 2.
260 
261  /// This bit is set if the ABIs being compared are different.
262  ///
263  /// Its numerical value is 4.
265 
266  /// This bit is set if the ABIs being compared are different *and*
267  /// are incompatible.
268  ///
269  /// Its numerical value is 8.
271 };
272 
275 
278 
281 
282 bool
284 
285 bool
287 
288 bool
290 
291 /// A type used to time various part of the libabigail system.
292 class timer
293 {
294  struct priv;
295  std::unique_ptr<priv> priv_;
296 
297 public:
298  enum kind
299  {
300  /// Default timer kind.
302  /// This kind of timer starts upon instantiation.
304  };
305 
307  bool start();
308  bool stop();
309  time_t value_in_seconds() const;
310  bool value(time_t& hours,
311  time_t& minutes,
312  time_t& seconds,
313  time_t& milliseconds) const;
314  string value_as_string() const;
315  ~timer();
316 }; //end class timer
317 
318 ostream& operator<<(ostream&, const timer&);
319 
320 ostream&
321 operator<<(ostream& output, file_type r);
322 
323 file_type guess_file_type(istream& in);
324 
325 file_type guess_file_type(const string& file_path);
326 
327 bool
328 get_rpm_name(const string& str, string& name);
329 
330 bool
331 get_rpm_arch(const string& str, string& arch);
332 
333 bool
334 get_deb_name(const string& str, string& name);
335 
336 bool
337 file_is_kernel_package(const string& file_path,
339 
340 bool
341 rpm_contains_file(const string& rpm_path,
342  const string& file_name);
343 
344 bool
345 file_is_kernel_debuginfo_package(const string& file_path,
347 
348 std::shared_ptr<char>
349 make_path_absolute(const char*p);
350 
351 char*
352 make_path_absolute_to_be_freed(const char*p);
353 
354 corpus_group_sptr
356  const string debug_info_root,
357  const string& vmlinux_path,
358  vector<string>& suppr_paths,
359  vector<string>& kabi_wl_paths,
360  suppr::suppressions_type& supprs,
361  bool verbose,
362  environment& env,
363  corpus::origin requested_fe_kind = corpus::DWARF_ORIGIN);
364 
365 elf_based_reader_sptr
366 create_best_elf_based_reader(const string& elf_file_path,
367  const vector<char**>& debug_info_root_paths,
368  environment& env,
369  corpus::origin requested_debug_info_kind,
370  bool show_all_types,
371  bool linux_kernel_mode = false);
372 
373 }// end namespace tools_utils
374 
375 /// A macro that expands to aborting the program when executed.
376 ///
377 /// Before aborting, the macro emits informatin about the source
378 /// location where it was expanded.
379 #define ABG_ASSERT_NOT_REACHED \
380  do { \
381  std::cerr << "in " << __FUNCTION__ \
382  << " at: " << __FILE__ << ":" << __LINE__ \
383  << ": execution should not have reached this point!\n"; \
384  abort(); \
385  } while (false)
386 }//end namespace abigail
387 
388 #endif //__ABG_TOOLS_UTILS_H
This file contains the declarations for an elf-based. DWARF and CTF readers can inherit this one.
origin
This abstracts where the corpus comes from. That is, either it has been read from the native xml form...
Definition: abg-corpus.h:51
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition: abg-ir.h:140
std::fstream & get_stream()
Get the fstream to the temporary file.
bool is_good() const
Test if the temporary file has been created and is usable.
static temp_file_sptr create()
Create the temporary file and return it if it's usable.
const char * get_path() const
Return the path to the temporary file.
A type used to time various part of the libabigail system.
~timer()
Destructor of the timer type.
string value_as_string() const
Get the elapsed time as a human-readable string.
time_t value_in_seconds() const
Get the elapsed time in seconds.
bool stop()
Stop the timer.
timer(kind k=DEFAULT_TIMER_KIND)
Constructor of the timer type.
bool start()
Start the timer.
@ START_ON_INSTANTIATION_TIMER_KIND
This kind of timer starts upon instantiation.
@ DEFAULT_TIMER_KIND
Default timer kind.
bool value(time_t &hours, time_t &minutes, time_t &seconds, time_t &milliseconds) const
Get the elapsed time in hour:minutes:seconds:milliseconds.
vector< suppression_sptr > suppressions_type
Convenience typedef for a vector of suppression_sptr.
Definition: abg-fwd.h:1658
shared_ptr< type_suppression > type_suppression_sptr
Convenience typedef for a shared pointer to type_suppression.
bool check_file(const string &path, ostream &out, const string &prog_name)
Check if a given path exists and is readable.
bool rpm_contains_file(const string &rpm_path, const string &file_name)
Test if an RPM package contains a given file.
string get_default_system_suppression_file_path()
Get the path to the default system suppression file.
type_suppression_sptr gen_suppr_spec_from_headers(const vector< string > &headers_root_dirs, const vector< string > &header_files)
Generate a type suppression specification that suppresses ABI changes for types defined in source fil...
bool split_string(const string &input_string, const string &delims, vector< string > &result)
Split a given string into substrings, given some delimiters.
bool string_ends_with(const string &str, const string &suffix)
Test if a given string ends with a particular suffix.
bool find_file_under_dir(const string &root_dir, const string &file_path_to_look_for, string &result)
Find a given file under a root directory and return its absolute path.
bool string_is_ascii(const string &str)
Test if a string is made of ascii characters.
ostream & emit_prefix(const string &prog_name, ostream &out)
Emit a prefix made of the name of the program which is emitting a message to an output stream.
bool dir_name(string const &path, string &dir_name, bool keep_separator_at_end)
Return the directory part of a file path.
bool base_name(string const &path, string &file_name)
Return the file name part of a file part.
shared_ptr< temp_file > temp_file_sptr
Convenience typedef for a shared_ptr to temp_file.
bool get_vmlinux_path_from_kernel_dist(const string &from, string &vmlinux_path)
Get the path of the vmlinux binary under the given directory, that must have been generated either fr...
bool file_has_btf_debug_info(const string &elf_file_path, const vector< char ** > &debug_info_root_paths)
Test if an ELF file has BTFG debug info.
void initialize()
This function needs to be called before any libabigail function.
void load_default_user_suppressions(suppr::suppressions_type &supprs)
Load the default user suppression specification file and populate a vector of suppression_sptr with i...
const char * get_anonymous_subrange_internal_name_prefix()
Getter of the prefix for the name of anonymous range.
char * make_path_absolute_to_be_freed(const char *p)
Return a copy of the path given in argument, turning it into an absolute path by prefixing it with th...
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
std::shared_ptr< char > make_path_absolute(const char *p)
Return a copy of the path given in argument, turning it into an absolute path by prefixing it with th...
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
string trim_white_space(const string &str)
Remove spaces at the beginning and at the end of a given string.
corpus_group_sptr stick_corpus_and_binaries_into_corpus_group(const fe_iface_sptr &reader, const corpus_sptr &korpus, const vector< string > &binaries, const vector< string > &bins_dirs)
Create a corpus group made of a given korpus and a set of binaries found in a set of directories.
bool check_dir(const string &path, ostream &out, const string &prog_name)
Check if a given path exists, is readable and is a directory.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
bool string_begins_with(const string &str, const string &prefix)
Test if a given string begins with a particular prefix.
bool file_has_dwarf_debug_info(const string &elf_file_path, const vector< char ** > &debug_info_root_paths)
Test if an ELF file has DWARF debug info.
bool abidiff_status_has_incompatible_abi_change(abidiff_status s)
Test if an instance of.
elf_based_reader_sptr create_best_elf_based_reader(const string &elf_file_path, const vector< char ** > &debug_info_root_paths, environment &env, corpus::origin requested_fe_kind, bool show_all_types, bool linux_kernel_mode)
Create the best elf based reader (or front-end), given an ELF file.
const char * get_system_libdir()
Get the value of $libdir variable of the autotools build system. This is where shared libraries are u...
bool get_binary_paths_from_kernel_dist(const string &dist_root, const string &debug_info_root_path, string &vmlinux_path, vector< string > &module_paths)
Get the paths of the vmlinux and kernel module binaries under given directory.
bool get_dependencies(const corpus &korpus, const vector< string > &deps_dirs, set< string > &dependencies)
Get the dependencies of an ABI corpus, which are found in a set of directories. Note that the depende...
bool get_dsos_provided_by_rpm(const string &rpm_path, set< string > &provided_dsos)
Get the SONAMEs of the DSOs advertised as being "provided" by a given RPM. That set can be considered...
bool ensure_dir_path_created(const string &dir_path)
Ensures #dir_path is a directory and is created. If #dir_path is not created, this function creates i...
bool get_rpm_name(const string &str, string &name)
Get the package name of an rpm package.
bool execute_command_and_get_output(const string &cmd, vector< string > &lines)
Execute a shell command and returns its output.
abidiff_status & operator|=(abidiff_status &l, abidiff_status r)
The |= operator.
bool is_dir(const string &path)
Tests if a given path is a directory or a symbolic link to a directory.
bool find_file_under_dirs(const vector< string > &root_dirs, const string &file_path_to_look_for, string &result)
Find a given file possibly under a set of directories and return its absolute path.
bool dir_exists(const string &path)
Test that a given directory exists.
bool string_is_ascii_identifier(const string &str)
Test if a string is made of ascii characters which are identifiers acceptable in C or C++ programs.
ostream & operator<<(ostream &o, const timer &t)
Streaming operator for the timer type.
bool file_is_kernel_package(const string &file_path, file_type file_type)
Tests if a given file name designates a kernel package.
abidiff_status operator|(abidiff_status l, abidiff_status r)
The bitwise 'OR' operator for abidiff_status bit masks.
string get_library_version_string()
Return the version string of the library.
bool dir_is_empty(const string &path)
Test if a given directory exists and is empty.
bool abidiff_status_has_abi_change(abidiff_status s)
Test if an instance of.
bool abidiff_status_has_error(abidiff_status s)
Test if an instance of.
bool get_deb_name(const string &str, string &name)
Get the package name of a .deb package.
bool maybe_get_symlink_target_file_path(const string &file_path, string &target_path)
If a given file is a symbolic link, get the canonicalized absolute path to the target file.
abidiff_status
Exit status for abidiff and abicompat tools.
@ ABIDIFF_ABI_INCOMPATIBLE_CHANGE
This bit is set if the ABIs being compared are different *and* are incompatible.
@ ABIDIFF_ABI_CHANGE
This bit is set if the ABIs being compared are different.
@ ABIDIFF_USAGE_ERROR
This bit is set if the tool is invoked in an non appropriate manner.
@ ABIDIFF_ERROR
This bit is set if there is an application error.
@ ABIDIFF_OK
This is for when the compared ABIs are equal.
bool is_regular_file(const string &path)
Test if path is a path to a regular file or a symbolic link to a regular file.
bool file_exists(const string &path)
Tests whether a path exists;.
file_type
The different types of files understood the bi* suite of tools.
@ FILE_TYPE_RPM
An RPM (.rpm) binary file.
@ FILE_TYPE_NATIVE_BI
The native xml file format representing a translation unit.
@ FILE_TYPE_ELF
An elf file. Read this kind of file should yield an abigail::corpus type.
@ FILE_TYPE_DEB
A DEB (.deb) binary file.
@ FILE_TYPE_UNKNOWN
A file type we don't know about.
@ FILE_TYPE_TAR
A tar archive. The archive can be compressed with the popular compression schemes recognized by GNU t...
@ FILE_TYPE_DIR
A plain directory.
@ FILE_TYPE_AR
An archive (AR) file.
@ FILE_TYPE_SRPM
An SRPM (.src.rpm) file.
bool file_has_ctf_debug_info(const string &elf_file_path, const vector< char ** > &debug_info_root_paths)
Test if an ELF file has CTF debug info.
bool string_suffix(const string &input_string, const string &prefix, string &suffix)
Get the suffix of a string, given a prefix to consider.
void add_binaries_into_corpus_group(const fe_iface_sptr &reader, const vector< string > &binaries, const vector< string > &deps_dirs, corpus_group &group)
For each binary of a vector of binaries, if the binary is present in at least one of the directories ...
bool ensure_parent_dir_created(const string &path)
Ensures that the parent directory of #path is created.
suppressions_type gen_suppr_spec_from_kernel_abi_whitelists(const std::vector< std::string > &abi_whitelist_paths)
Generate a suppression specification from kernel abi whitelist files.
corpus_group_sptr build_corpus_group_from_kernel_dist_under(const string &root, const string debug_info_root, const string &vmlinux_path, vector< string > &suppr_paths, vector< string > &kabi_wl_paths, suppressions_type &supprs, bool verbose, environment &env, corpus::origin requested_fe_kind)
Walk a given directory and build an instance of corpus_group from the vmlinux kernel binary and the l...
void convert_char_stars_to_char_star_stars(const vector< char * > &char_stars, vector< char ** > &char_star_stars)
Convert a vector<char*> into a vector<char**>.
bool sorted_strings_common_prefix(vector< string > &input_strings, string &prefix)
Find the prefix common to a *SORTED* vector of strings.
void get_comma_separated_args_of_option(const string &input_str, const string &option, vector< string > &arguments)
Get a vector of arguments from a string containing a comma-separated list of those arguments.
void real_path(const string &path, string &result)
Return the real path of a given path.
abidiff_status operator&(abidiff_status l, abidiff_status r)
The bitwise 'AND' operator for abidiff_status bit masks.
void add_dependencies_into_corpus_group(const fe_iface_sptr &reader, const corpus &korpus, const vector< string > &deps_dirs, corpus_group &group)
For each dependency of a given corpus, if it is present in at least one of the directories listed in ...
bool file_is_kernel_debuginfo_package(const string &file_name, file_type file_type)
Tests if a given file name designates a kernel debuginfo package.
void load_default_system_suppressions(suppr::suppressions_type &supprs)
Load the default system suppression specification file and populate a vector of suppression_sptr with...
string get_abixml_version_string()
Return the version string for the ABIXML format.
string get_random_number_as_string()
Get a pseudo random number as string.
bool decl_names_equal(const string &l, const string &r)
Compare two fully qualified decl names by taking into account that they might have compontents that a...
bool get_rpm_arch(const string &str, string &arch)
Get the architecture string from the NVR of an rpm.
string get_default_user_suppression_file_path()
Get the path to the default user suppression file.
string trim_leading_string(const string &from, const string &to_trim)
Remove a string of pattern in front of a given string.
corpus_group_sptr stick_corpus_and_dependencies_into_corpus_group(const fe_iface_sptr &reader, const corpus_sptr &korpus, const vector< string > &deps_dirs)
Create a corpus group made of a given korpus and the subset of its dependencies that can be found fou...
file_type guess_file_type(istream &in)
Guess the type of the content of an input stream.
size_t get_random_number()
Get a pseudo random number.
Toplevel namespace for libabigail.