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"
19
20namespace abigail
21{
22
23namespace tools_utils
24{
25
26using std::ostream;
27using std::istream;
28using std::ifstream;
29using std::string;
30using std::set;
31using std::shared_ptr;
32
33const char* get_system_libdir();
38
39bool file_exists(const string&);
40bool is_regular_file(const string&);
41bool file_has_dwarf_debug_info(const string& elf_file_path,
42 const vector<char**>& debug_info_root_paths);
43bool file_has_ctf_debug_info(const string& elf_file_path,
44 const vector<char**>& debug_info_root_paths);
45bool file_has_btf_debug_info(const string& elf_file_path,
46 const vector<char**>& debug_info_root_paths);
47bool is_dir(const string&);
48bool dir_exists(const string&);
49bool dir_is_empty(const string &);
50bool decl_names_equal(const string&, const string&);
51bool maybe_get_symlink_target_file_path(const string& file_path,
52 string& target_path);
53bool base_name(string const& path,
54 string& file_name);
55bool dir_name(string const &path,
56 string& path_dir_name,
57 bool keep_separator_at_end=false);
58void real_path(const string&path, string& realpath);
59bool ensure_dir_path_created(const string&);
60bool ensure_parent_dir_created(const string&);
61ostream& emit_prefix(const string& prog_name, ostream& out);
62bool check_file(const string& path, ostream& out, const string& prog_name = "");
63bool check_dir(const string& path, ostream& out, const string& prog_name="");
64bool string_ends_with(const string&, const string&);
65bool string_begins_with(const string&, const string&);
66bool string_is_ascii(const string&);
67bool string_is_ascii_identifier(const string&);
68bool split_string(const string&, const string&, vector<string>&);
69bool string_suffix(const string&, const string&, string&);
70bool sorted_strings_common_prefix(vector<string>&, string&);
73bool execute_command_and_get_output(const string&, vector<string>&);
74void get_comma_separated_args_of_option(const string& input_str,
75 const string& option,
76 vector<string>& arguments);
77bool get_dsos_provided_by_rpm(const string& rpm_path,
78 set<string>& provided_dsos);
79string trim_white_space(const string&);
80string trim_leading_string(const string& from, const string& to_trim);
81void convert_char_stars_to_char_star_stars(const vector<char*>&,
82 vector<char**>&);
83
85gen_suppr_spec_from_headers(const string& hdrs_root_dir);
86
88gen_suppr_spec_from_headers(const string& hdrs_root_dir,
89 const vector<string>& hdr_files);
90
92gen_suppr_spec_from_headers(const vector<string>& headers_root_dirs,
93 const vector<string>& header_files);
94
97 (const vector<string>& abi_whitelist_paths);
98
99bool
100get_vmlinux_path_from_kernel_dist(const string& from,
101 string& vmlinux_path);
102
103bool
104get_binary_paths_from_kernel_dist(const string& dist_root,
105 const string& debug_info_root_path,
106 string& vmlinux_path,
107 vector<string>& module_paths);
108
109bool
110get_binary_paths_from_kernel_dist(const string& dist_root,
111 string& vmlinux_path,
112 vector<string>& module_paths);
113
114string
116
117string
119
120void
122
123void
125
126bool
127find_file_under_dir(const string& root_dir,
128 const string& file_path_to_look_for,
129 string& result);
130
131bool
132find_file_under_dirs(const vector<string>& root_dirs,
133 const string& file_path_to_look_for,
134 string& result);
135
136bool
137get_dependencies(const corpus&, const vector<string>&, set<string>&);
138
139void
140add_binaries_into_corpus_group(const fe_iface_sptr& reader,
141 const vector<string>& binaries,
142 const vector<string>& deps_dirs,
143 corpus_group& group);
144
145void
146add_dependencies_into_corpus_group(const fe_iface_sptr& reader,
147 const corpus& korpus,
148 const vector<string>& deps_dirs,
149 corpus_group& group);
150
151corpus_group_sptr
152stick_corpus_and_binaries_into_corpus_group(const fe_iface_sptr& reader,
153 const corpus_sptr& korpus,
154 const vector<string>& binaries,
155 const vector<string>& deps_dirs);
156
157corpus_group_sptr
158stick_corpus_and_dependencies_into_corpus_group(const fe_iface_sptr& reader,
159 const corpus_sptr& korpus,
160 const vector<string>& deps_dirs);
161
162
163class temp_file;
164
165/// Convenience typedef for a shared_ptr to @ref temp_file.
166typedef shared_ptr<temp_file> temp_file_sptr;
167
168/// A temporary file.
169///
170/// This is a helper file around the mkstemp API.
171///
172/// Once the temporary file is created, users can interact with it
173/// using an fstream. They can also get the path to the newly
174/// created temporary file.
175///
176/// When the instance of @ref temp_file is destroyed, the underlying
177/// resources are de-allocated, the underlying temporary file is
178/// closed and removed.
180{
181 struct priv;
182 std::unique_ptr<priv> priv_;
183
184 temp_file();
185
186public:
187
188 bool
189 is_good() const;
190
191 const char*
192 get_path() const;
193
194 std::fstream&
195 get_stream();
196
197 static temp_file_sptr
198 create();
199}; // end class temp_file
200
201size_t
203
204string
206
207/// The different types of files understood the bi* suite of tools.
209{
210 /// A file type we don't know about.
212 /// The native xml file format representing a translation unit.
214 /// An elf file. Read this kind of file should yield an
215 /// abigail::corpus type.
217 /// An archive (AR) file.
219 // A native abixml file format representing a corpus of one or
220 // several translation units.
221 FILE_TYPE_XML_CORPUS,
222 // A native abixml file format representing a corpus group of one or
223 // several corpora.
224 FILE_TYPE_XML_CORPUS_GROUP,
225 /// An RPM (.rpm) binary file
227 /// An SRPM (.src.rpm) file
229 /// A DEB (.deb) binary file
231 /// A plain directory
233 /// A tar archive. The archive can be compressed with the popular
234 /// compression schemes recognized by GNU tar.
237
238/// Exit status for abidiff and abicompat tools.
239///
240/// It's actually a bit mask. The value of each enumerator is a power
241/// of two.
243{
244 /// This is for when the compared ABIs are equal.
245 ///
246 /// Its numerical value is 0.
248
249 /// This bit is set if there is an application error.
250 ///
251 /// Its numerical value is 1.
253
254 /// This bit is set if the tool is invoked in an non appropriate
255 /// manner.
256 ///
257 /// Its numerical value is 2.
259
260 /// This bit is set if the ABIs being compared are different.
261 ///
262 /// Its numerical value is 4.
264
265 /// This bit is set if the ABIs being compared are different *and*
266 /// are incompatible.
267 ///
268 /// Its numerical value is 8.
271
274
277
280
281bool
283
284bool
286
287bool
289
290/// A type used to time various part of the libabigail system.
291class timer
292{
293 struct priv;
294 std::unique_ptr<priv> priv_;
295
296public:
297 enum kind
298 {
299 /// Default timer kind.
301 /// This kind of timer starts upon instantiation.
303 };
304
306 bool start();
307 bool stop();
308 time_t value_in_seconds() const;
309 bool value(time_t& hours,
310 time_t& minutes,
311 time_t& seconds,
312 time_t& milliseconds) const;
313 string value_as_string() const;
314 ~timer();
315}; //end class timer
316
317ostream& operator<<(ostream&, const timer&);
318
319ostream&
320operator<<(ostream& output, file_type r);
321
322file_type guess_file_type(istream& in);
323
324file_type guess_file_type(const string& file_path);
325
326bool
327get_rpm_name(const string& str, string& name);
328
329bool
330get_rpm_arch(const string& str, string& arch);
331
332bool
333get_deb_name(const string& str, string& name);
334
335bool
336file_is_kernel_package(const string& file_path,
338
339bool
340rpm_contains_file(const string& rpm_path,
341 const string& file_name);
342
343bool
344file_is_kernel_debuginfo_package(const string& file_path,
346
347std::shared_ptr<char>
348make_path_absolute(const char*p);
349
350char*
352
353corpus_group_sptr
355 const string debug_info_root,
356 const string& vmlinux_path,
357 vector<string>& suppr_paths,
358 vector<string>& kabi_wl_paths,
360 bool verbose,
361 environment& env,
362 corpus::origin requested_fe_kind = corpus::DWARF_ORIGIN);
363
364elf_based_reader_sptr
365create_best_elf_based_reader(const string& elf_file_path,
366 const vector<char**>& debug_info_root_paths,
367 environment& env,
368 corpus::origin requested_debug_info_kind,
369 bool show_all_types,
370 bool linux_kernel_mode = false);
371
372}// end namespace tools_utils
373
374/// A macro that expands to aborting the program when executed.
375///
376/// Before aborting, the macro emits informatin about the source
377/// location where it was expanded.
378#define ABG_ASSERT_NOT_REACHED \
379 do { \
380 std::cerr << "in " << __FUNCTION__ \
381 << " at: " << __FILE__ << ":" << __LINE__ \
382 << ": execution should not have reached this point!\n"; \
383 abort(); \
384 } while (false)
385}//end namespace abigail
386
387#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:45
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:1556
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 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.
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.
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.
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.