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-2025 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<string>& debug_info_root_paths);
44 bool file_has_ctf_debug_info(const string& elf_file_path,
45  const vector<string>& debug_info_root_paths);
46 bool file_has_btf_debug_info(const string& elf_file_path,
47  const vector<string>& 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 bool remove_white_spaces(string&);
82 bool normalize_litterals(string&);
83 string trim_leading_string(const string& from, const string& to_trim);
84 void convert_char_stars_to_char_star_stars(const vector<char*>&,
85  vector<char**>&);
86 
88 gen_suppr_spec_from_headers(const string& hdrs_root_dir);
89 
91 gen_suppr_spec_from_headers(const string& hdrs_root_dir,
92  const vector<string>& hdr_files);
93 
95 gen_suppr_spec_from_headers(const vector<string>& headers_root_dirs,
96  const vector<string>& header_files);
97 
100  (const vector<string>& abi_whitelist_paths);
101 
102 bool
103 get_file_path_dirs_under_dir(const string& root_dir, vector<string>& dirs);
104 
105 bool
106 get_vmlinux_path_from_kernel_dist(const string& from,
107  string& vmlinux_path);
108 
109 bool
110 get_binary_paths_from_kernel_dist(const string& dist_root,
111  const string& debug_info_root_path,
112  string& vmlinux_path,
113  vector<string>& module_paths);
114 
115 bool
116 get_binary_paths_from_kernel_dist(const string& dist_root,
117  string& vmlinux_path,
118  vector<string>& module_paths);
119 
120 bool
121 get_file_path_dirs_under_dir(const string& root, vector<string>& dirs);
122 
123 string
125 
126 string
128 
129 void
131 
132 void
134 
135 bool
136 find_file_under_dir(const string& root_dir,
137  const string& file_path_to_look_for,
138  string& result);
139 
140 bool
141 find_file_under_dirs(const vector<string>& root_dirs,
142  const string& file_path_to_look_for,
143  string& result);
144 
145 bool
146 get_dependencies(const corpus&, const vector<string>&, set<string>&);
147 
148 void
149 add_binaries_into_corpus_group(const fe_iface_sptr& reader,
150  const vector<string>& binaries,
151  const vector<string>& deps_dirs,
152  corpus_group& group);
153 
154 void
155 add_dependencies_into_corpus_group(const fe_iface_sptr& reader,
156  const corpus& korpus,
157  const vector<string>& deps_dirs,
158  corpus_group& group);
159 
160 corpus_group_sptr
161 stick_corpus_and_binaries_into_corpus_group(const fe_iface_sptr& reader,
162  const corpus_sptr& korpus,
163  const vector<string>& binaries,
164  const vector<string>& deps_dirs);
165 
166 corpus_group_sptr
167 stick_corpus_and_dependencies_into_corpus_group(const fe_iface_sptr& reader,
168  const corpus_sptr& korpus,
169  const vector<string>& deps_dirs);
170 
171 
172 class temp_file;
173 
174 /// Convenience typedef for a shared_ptr to @ref temp_file.
175 typedef shared_ptr<temp_file> temp_file_sptr;
176 
177 /// A temporary file.
178 ///
179 /// This is a helper file around the mkstemp API.
180 ///
181 /// Once the temporary file is created, users can interact with it
182 /// using an fstream. They can also get the path to the newly
183 /// created temporary file.
184 ///
185 /// When the instance of @ref temp_file is destroyed, the underlying
186 /// resources are de-allocated, the underlying temporary file is
187 /// closed and removed.
189 {
190  struct priv;
191  std::unique_ptr<priv> priv_;
192 
193  temp_file();
194 
195 public:
196 
197  bool
198  is_good() const;
199 
200  const char*
201  get_path() const;
202 
203  std::fstream&
204  get_stream();
205 
206  static temp_file_sptr
207  create();
208 }; // end class temp_file
209 
210 size_t
212 
213 string
215 
216 /// The different types of files understood the bi* suite of tools.
218 {
219  /// A file type we don't know about.
221  /// The native xml file format representing a translation unit.
223  /// An elf file. Read this kind of file should yield an
224  /// abigail::corpus type.
226  /// An archive (AR) file.
228  // A native abixml file format representing a corpus of one or
229  // several translation units.
230  FILE_TYPE_XML_CORPUS,
231  // A native abixml file format representing a corpus group of one or
232  // several corpora.
233  FILE_TYPE_XML_CORPUS_GROUP,
234  /// An RPM (.rpm) binary file
236  /// An SRPM (.src.rpm) file
238  /// A DEB (.deb) binary file
240  /// A plain directory
242  /// A tar archive. The archive can be compressed with the popular
243  /// compression schemes recognized by GNU tar.
245 
246  // All non-tared compression scheme go under here. When one of
247  // these is returned, the goal is to look into the uncompressed
248  // stream to get what format has been compressed, then return an
249  // enumerator for that compressed format instead.
250  //
251  // Please note that each time a new enumerator is added here, one
252  // needs to add a corresponding enumerator to the @ref
253  // compression_kind enum in abg-tools-utils.cc and update the
254  // is_compressed_file_type and get_compressed_streambuf functions
255  // accordingly.
256 
257  /// The XZ (lzma) compresson scheme.
258 
260 };
261 
262 /// Exit status for abidiff and abicompat tools.
263 ///
264 /// It's actually a bit mask. The value of each enumerator is a power
265 /// of two.
267 {
268  /// This is for when the compared ABIs are equal.
269  ///
270  /// Its numerical value is 0.
272 
273  /// This bit is set if there is an application error.
274  ///
275  /// Its numerical value is 1.
277 
278  /// This bit is set if the tool is invoked in an non appropriate
279  /// manner.
280  ///
281  /// Its numerical value is 2.
283 
284  /// This bit is set if the ABIs being compared are different.
285  ///
286  /// Its numerical value is 4.
288 
289  /// This bit is set if the ABIs being compared are different *and*
290  /// are incompatible.
291  ///
292  /// Its numerical value is 8.
294 };
295 
298 
301 
304 
305 bool
307 
308 bool
310 
311 bool
313 
314 /// A type used to time various part of the libabigail system.
315 class timer
316 {
317  struct priv;
318  std::unique_ptr<priv> priv_;
319 
320 public:
321  enum kind
322  {
323  /// Default timer kind.
325  /// This kind of timer starts upon instantiation.
327  };
328 
330  bool start();
331  bool stop();
332  time_t value_in_seconds() const;
333  bool value(time_t& hours,
334  time_t& minutes,
335  time_t& seconds,
336  time_t& milliseconds) const;
337  string value_as_string() const;
338  ~timer();
339 }; //end class timer
340 
341 ostream& operator<<(ostream&, const timer&);
342 
343 ostream&
344 operator<<(ostream& output, file_type r);
345 
346 file_type guess_file_type(istream& in);
347 
348 file_type guess_file_type(const string& file_path,
349  bool look_through_compression = true);
350 
351 bool
352 get_rpm_name(const string& str, string& name);
353 
354 bool
355 get_rpm_arch(const string& str, string& arch);
356 
357 bool
358 get_deb_name(const string& str, string& name);
359 
360 bool
361 file_is_kernel_package(const string& file_path,
363 
364 bool
365 rpm_contains_file(const string& rpm_path,
366  const string& file_name);
367 
368 bool
369 file_is_kernel_debuginfo_package(const string& file_path,
371 
372 std::shared_ptr<char>
373 make_path_absolute(const char*p);
374 
375 string
376 make_path_absolute(const string& p);
377 
378 corpus_group_sptr
380  const string debug_info_root,
381  const string& vmlinux_path,
382  vector<string>& suppr_paths,
383  vector<string>& kabi_wl_paths,
384  suppr::suppressions_type& supprs,
385  bool verbose,
386  environment& env,
387  corpus::origin requested_fe_kind = corpus::DWARF_ORIGIN);
388 
389 elf_based_reader_sptr
390 create_best_elf_based_reader(const string& elf_file_path,
391  const vector<string>& debug_info_root_paths,
392  environment& env,
393  corpus::origin requested_debug_info_kind,
394  bool show_all_types,
395  bool linux_kernel_mode = false);
396 
397 /// This is a custom std::streambuf that knows how to decompress an
398 /// input stream that was compressed using xz.
399 ///
400 /// The code was inspired by the example in the source code of the xz
401 /// project at
402 /// https://github.com/tukaani-project/xz/blob/master/doc/examples/02_decompress.c.
403 ///
404 /// here is an example of how a user code would use this custom
405 /// streambuf to decode an xz'ed file and emit its content to stdout.
406 ///
407 /// ifstream input_file("/path/to/a/compressed/file.xz", ifstream::binary);
408 /// xz_decompressor_type xzed_streambuf(input_file);
409 /// istream input_stream(&xzed_streambuf);
410 ///
411 /// const size_t BUFFER_SIZE = 1024 * 4;
412 /// vector<char> decompressed_data(BUFFER_SIZE);
413 /// input_stream.read(decompressed_data.data(), BUFFER_SIZE);
414 /// size_t nb_bytes_read = input_stream.gcount();
415 /// while (nb_bytes_read && !input_stream.bad())
416 /// {
417 /// for (auto c : decompressed_data)
418 /// std::out << c;
419 /// input_stream.read(decompressed_data.data(), BUFFER_SIZE);
420 /// nb_bytes_read = input_stream.gcount();
421 /// }
422 /// input_file.close();
423 ///
424 /// Voila.
425 class xz_decompressor_type : public std::streambuf
426 {
427  struct priv;
428 
429  std::unique_ptr<priv> priv_;
430 
431  public:
432  xz_decompressor_type(std::istream& xz_istream);
433 
435 
436  protected:
437 
438  int_type
439  underflow() override;
440 }; // end class xz_decompressor_type.
441 
442 }// end namespace tools_utils
443 
444 /// A macro that expands to aborting the program when executed.
445 ///
446 /// Before aborting, the macro emits informatin about the source
447 /// location where it was expanded.
448 #define ABG_ASSERT_NOT_REACHED \
449  do { \
450  std::cerr << "in " << __FUNCTION__ \
451  << " at: " << __FILE__ << ":" << __LINE__ \
452  << ": execution should not have reached this point!\n"; \
453  abort(); \
454  } while (false)
455 }//end namespace abigail
456 
457 #endif //__ABG_TOOLS_UTILS_H
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...
A tar archive. The archive can be compressed with the popular compression schemes recognized by GNU t...
A type used to time various part of the libabigail system.
ostream & operator<<(ostream &o, const timer &t)
Streaming operator for the timer type.
An RPM (.rpm) binary file.
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...
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 ...
time_t value_in_seconds() const
Get the elapsed time in seconds.
string value_as_string() const
Get the elapsed time as a human-readable string.
const char * get_anonymous_subrange_internal_name_prefix()
Getter of the prefix for the name of anonymous range.
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.
int_type underflow() override
The implementation of the virtual protected std:streambuf::underflow method. This method is invoked b...
string get_random_number_as_string()
Get a pseudo random number as string.
bool execute_command_and_get_output(const string &cmd, vector< string > &lines)
Execute a shell command and returns its output.
bool normalize_litterals(string &str)
Normalize the numerical litteral in a string.
void real_path(const string &path, string &result)
Return the real path of a given path.
bool start()
Start the timer.
A DEB (.deb) binary file.
elf_based_reader_sptr create_best_elf_based_reader(const string &elf_file_path, const vector< string > &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.
string get_abixml_version_string()
Return the version string for the ABIXML format.
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...
void convert_char_stars_to_char_star_stars(const vector< char * > &char_stars, vector< char ** > &char_star_stars)
Convert a vector into a vector.
const char * get_path() const
Return the path to the temporary file.
bool dir_exists(const string &path)
Test that a given directory exists.
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.
string trim_leading_string(const string &from, const string &to_trim)
Remove a string of pattern in front of a given string.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
timer(kind k=DEFAULT_TIMER_KIND)
Constructor of the timer type.
string get_default_system_suppression_file_path()
Get the path to the default system suppression file.
std::fstream & get_stream()
Get the fstream to the temporary file.
string get_default_user_suppression_file_path()
Get the path to the default user suppression file.
bool abidiff_status_has_abi_change(abidiff_status s)
Test if an instance of.
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 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 ...
shared_ptr< type_suppression > type_suppression_sptr
Convenience typedef for a shared pointer to type_suppression.
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 file_has_btf_debug_info(const string &elf_file_path, const vector< string > &debug_info_root_paths)
Test if an ELF file has BTFG debug info.
abidiff_status & operator|=(abidiff_status &l, abidiff_status r)
The |= operator.
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...
The native xml file format representing a translation unit.
bool get_rpm_arch(const string &str, string &arch)
Get the architecture string from the NVR of an rpm.
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...
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...
Toplevel namespace for libabigail.
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
bool file_exists(const string &path)
Tests whether a path exists;.
bool file_has_ctf_debug_info(const string &elf_file_path, const vector< string > &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.
bool get_rpm_name(const string &str, string &name)
Get the package name of an rpm package.
bool check_file(const string &path, ostream &out, const string &prog_name)
Check if a given path exists and is readable.
bool dir_is_empty(const string &path)
Test if a given directory exists and is empty.
string get_library_version_string()
Return the version string of the library.
bool remove_white_spaces(string &str)
Remove white spaces from a string.
abidiff_status operator|(abidiff_status l, abidiff_status r)
The bitwise 'OR' operator for abidiff_status bit masks.
origin
This abstracts where the corpus comes from. That is, either it has been read from the native xml form...
Definition: abg-corpus.h:50
bool file_has_dwarf_debug_info(const string &elf_file_path, const vector< string > &debug_info_root_paths)
Test if an ELF file has DWARF debug info.
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.
bool split_string(const string &input_string, const string &delims, vector< string > &result)
Split a given string into substrings, given some delimiters.
const char * get_system_libdir()
Get the value of $libdir variable of the autotools build system. This is where shared libraries are u...
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...
void load_default_system_suppressions(suppr::suppressions_type &supprs)
Load the default system suppression specification file and populate a vector of suppression_sptr with...
void initialize()
This function needs to be called before any libabigail function.
This file contains the declarations for an elf-based. DWARF and CTF readers can inherit this one...
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition: abg-ir.h:147
~xz_decompressor_type()
Destructor of the xz_decompressor_type class.
~timer()
Destructor of the timer type.
bool sorted_strings_common_prefix(vector< string > &input_strings, string &prefix)
Find the prefix common to a *SORTED* vector of strings.
This kind of timer starts upon instantiation.
bool string_begins_with(const string &str, const string &prefix)
Test if a given string begins with a particular prefix.
bool check_dir(const string &path, ostream &out, const string &prog_name)
Check if a given path exists, is readable and is a directory.
bool string_is_ascii(const string &str)
Test if a string is made of ascii characters.
shared_ptr< temp_file > temp_file_sptr
Convenience typedef for a shared_ptr to temp_file.
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 get_file_path_dirs_under_dir(const string &root_dir, vector< string > &dirs)
Get all the sub-directories (which contain a regular file) of a given directory.
vector< suppression_sptr > suppressions_type
Convenience typedef for a vector of suppression_sptr.
Definition: abg-fwd.h:1687
This is a custom std::streambuf that knows how to decompress an input stream that was compressed usin...
This bit is set if the ABIs being compared are different.
bool abidiff_status_has_incompatible_abi_change(abidiff_status s)
Test if an instance of.
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
xz_decompressor_type(std::istream &xz_istream)
Constructor of the xz_decompressor_type class.
static temp_file_sptr create()
Create the temporary file and return it if it's usable.
file_type
The different types of files understood the bi* suite of tools.
This is for when the compared ABIs are equal.
bool abidiff_status_has_error(abidiff_status s)
Test if an instance of.
bool rpm_contains_file(const string &rpm_path, const string &file_name)
Test if an RPM package contains a given file.
A file type we don't know about.
bool get_deb_name(const string &str, string &name)
Get the package name of a .deb package.
bool dir_name(string const &path, string &dir_name, bool keep_separator_at_end)
Return the directory part of a file path.
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...
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 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...
The XZ (lzma) compresson scheme.
bool is_dir(const string &path)
Tests if a given path is a directory or a symbolic link to a directory.
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...
This bit is set if the tool is invoked in an non appropriate manner.
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...
abidiff_status
Exit status for abidiff and abicompat tools.
An elf file. Read this kind of file should yield an abigail::corpus type.
string trim_white_space(const string &str)
Remove spaces at the beginning and at the end of a given string.
bool ensure_parent_dir_created(const string &path)
Ensures that the parent directory of #path is created.
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.
This bit is set if the ABIs being compared are different *and* are incompatible.
bool value(time_t &hours, time_t &minutes, time_t &seconds, time_t &milliseconds) const
Get the elapsed time in hour:minutes:seconds:milliseconds.
This bit is set if there is an application error.
abidiff_status operator&(abidiff_status l, abidiff_status r)
The bitwise 'AND' operator for abidiff_status bit masks.
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 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 string_ends_with(const string &str, const string &suffix)
Test if a given string ends with a particular suffix.
bool is_good() const
Test if the temporary file has been created and is usable.
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...
bool base_name(string const &path, string &file_name)
Return the file name part of a file part.
bool file_is_kernel_package(const string &file_path, file_type file_type)
Tests if a given file name designates a kernel package.
bool stop()
Stop the timer.