babeltrace.git
3 months agotests: add `src.ctf.fs` single field testing framework
Olivier Dion [Wed, 18 Oct 2023 15:02:30 +0000 (11:02 -0400)] 
tests: add `src.ctf.fs` single field testing framework

This patch adds a framework to test the decoding of single CTF 1.8
fields by a `src.ctf.fs` component.

Single CTF 1.8 field tests reside as moultipart documents (see
`tests/utils/python/moultipart.py`) in
`tests/data/plugins/src.ctf.fs/field/ctf-1`.

The new `test-field.sh` script does the following, for each `pass-*.mp`
file found in `tests/data/plugins/src.ctf.fs/field/ctf-1`:

1. Creates a temporary directory TD to hold a CTF 1.8 trace (`TD/trace`)
   and an expectation file (`TD/expect`).

2. Runs `data_from_mp.py` on the moultipart document, also passing TD.

   This script splits the moultipart document to produce `TD/trace` and
   `TD/expect`. The expected parts are, in this order:

   a) A TSDL field class (metadata), without any field name, for
      example:

          integer { size = 32; byte_order = be; }

      If you need to test the decoding of an array field, you may use
      `@` in this part to indicate to `data_from_mp.py` where the
      field name goes, for example:

          string @[23]

   b) A Normand [1] text representing the exact data of an instance
      of a).

   c) What you expect the `sink.test-text.single` component to print
      when it receives an event message containing what a `src.ctf.fs`
      component decoded from `TD/trace`.

      `sink.test-text.single` prints exactly the value of b) with a
      YAML-like format.

3. Runs something like this, keeping the standard output text:

       $ babeltrace2 -c sink.test-text.single "TD/trace"

4. Compares the result of step 3 with `TD/expect` using bt_diff().

Now it becomes easy to test regular and corner cases of our CTF 1.8
decoding by adding new files to
`tests/data/plugins/src.ctf.fs/field/ctf-1`.

As initial examples, `tests/data/plugins/src.ctf.fs/field/ctf-1`
contains a few single field tests already.

Philippe changes:

• Made some style adjustments to the original patch.

• Using `mktemp -d` instead of `mktemp --directory` because macOS
  doesn't seem to know the latter.

• In `utils.sh`, changed bt_diff() to use the new bt_remove_cr_inline()
  to remove CR characters from both files because this is the first time
  the test generates both files.

  Leaving bt_remove_cr() as is because another test uses it directly.

• In the _print_field() function of `bt_plugin_test_text.py`:

  ‣ Changed parts of the strategy to fix some rendering bugs.
  ‣ Using int() for an integer field to avoid printing enum. labels.
  ‣ Added the empty structure field special case.
  ‣ Added comments with output examples.

• In `data_from_mp.py`:

  ‣ Added lots of useful TSDL type aliases available to any CTF 1
    single field test.

  ‣ Made the default Normand byte order little-endian since it's also
    the default TSDL byte order in _make_ctf_1_metadata().

• In `test-field.sh`, using a hard-coded test count.

  See the Gerrit discussion [2] for the rationale.

• Added more initial tests in
  `tests/data/plugins/src.ctf.fs/field/ctf-1`.

[1]: https://github.com/efficios/normand
[2]: https://review.lttng.org/c/babeltrace/+/11149

Change-Id: I7539b46d49200b5e75fe3525401b47b8ff418f6c
Signed-off-by: Olivier Dion <odion@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11149
Tested-by: jenkins <jenkins@lttng.org>
3 months agoFix: `babeltrace2 convert`: don't consider the `--plugin-path` opt.
Philippe Proulx [Wed, 7 Feb 2024 17:02:04 +0000 (12:02 -0500)] 
Fix: `babeltrace2 convert`: don't consider the `--plugin-path` opt.

OBSERVED ISSUE
══════════════
When running the (default) `convert` command of the CLI and passing
`--plugin-path` _after_ a conversion argument, the program aborts:

    $ babeltrace2 /some/path --plugin-path=/meow/mix
    [1]    87823 IOT instruction (core dumped)  babeltrace2 /some/path --plugin-path=/meow/mix

I expect such a command line to gracefully tell me that `--plugin-path`
is an unknown option.

CAUSE
═════
This is due to the `convert` command considering the general
`--plugin-path` option (part of the `convert_options` array), but then
not ignoring it during one of its passes.

`--plugin-path` is a general option, not a conversion option. Therefore,
it must not be part of `--plugin-path`.

SOLUTION
════════
Remove the `OPT_PLUGIN_PATH` entry from the `convert_options` array in
`babeltrace2-cfg-cli-args.c`.

The output of the command line above is now:

    ERROR:    [Babeltrace CLI] (babeltrace2.c:2655)
      Command-line error: retcode=1
    CAUSED BY [Babeltrace CLI] (babeltrace2-cfg-cli-args.c:1427)
      While parsing `convert` command's command-line arguments: While
      parsing argument #3
      (`--plugin-path=data/plugins/src.ctf.fs/field/`): Unknown option
      `--plugin-path`

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I8b22c5f732aae9fd1f3c989bd5aaef2d461d4160
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11756
Tested-by: jenkins <jenkins@lttng.org>
3 months agolib: move graph/message/iterator.h -> graph/iterator.h
Simon Marchi [Tue, 6 Feb 2024 17:35:10 +0000 (17:35 +0000)] 
lib: move graph/message/iterator.h -> graph/iterator.h

This file doesn't belong in the `message` directory, move it up one
level, next to `iterator.c`.

Change-Id: I096d91d5afa32ebb304b5132cad067e820f2bb30
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11749
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agocpp-common/vendor/optional-lite: bump to 3.6.0
Philippe Proulx [Tue, 6 Feb 2024 03:09:35 +0000 (22:09 -0500)] 
cpp-common/vendor/optional-lite: bump to 3.6.0

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I81ff349fb55cd3d3a0474fcff13ef10a439b852c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11747
Tested-by: jenkins <jenkins@lttng.org>
3 months agoUpdate README.adoc
Philippe Proulx [Tue, 6 Feb 2024 03:55:51 +0000 (22:55 -0500)] 
Update README.adoc

Notable changes:

‣ Fix broken LTTng CI links.

‣ Reword a few sentences to improve clarity.

‣ Use mostly "Babeltrace 2".

‣ Remove the sentence saying Babeltrace 2.0 is a "recent" release.

‣ Add an image (a conversion graph diagram) to maintain attention.

‣ Add a ToC, placing it at a specific location after the preamble when
  GitHub renders.

‣ Add thanks to the authors of embedded projects.

‣ Mention `--enable-built-in-plugins` and
  `--enable-built-in-python-plugin-support`.

‣ Transform the note in "Community" to regular text.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I487d781f435322652fde44cf1fed66f8a4d07c72
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11748

3 months agobt2c::Logger::_logStrNoThrow(): use `msg`, not `_mBuf.data()`
Philippe Proulx [Fri, 2 Feb 2024 20:34:03 +0000 (15:34 -0500)] 
bt2c::Logger::_logStrNoThrow(): use `msg`, not `_mBuf.data()`

By chance those were always the same, but from the _logStrNoThrow()
point of view, it must use `msg`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie91ea9e6ff3b8f26c214904aaa9bd036b8dacfcb
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11741

3 months agoFix: bt2c::Logger: include the `errno` message into an error cause too
Philippe Proulx [Fri, 2 Feb 2024 20:19:24 +0000 (15:19 -0500)] 
Fix: bt2c::Logger: include the `errno` message into an error cause too

The observed issue is that, when using a BT_CPPLOG_ERRNO*APPEND*()
macro, the logger logs the `errno` message (for example,
`Invalid argument`), but the message of the appended error cause doesn't
contain it.

This is because `bt2c::Logger` relies on bt_log_write_errno() to include
the `errno` message (bt_log_write_errno() is the one calling
g_strerror()).

To fix this, don't call bt_log_write_errno() anymore: consider `initMsg`
in the general private methods (like _logStrNoThrow()), and format an
introductory message (`initMsg`, `: `, and then the result of
g_strerror()) directly into the logErrno*() methods. See the new static
_errnoIntroStr().

The `_InitMsgLogWriter` writer makes the concatenation of `initMsg`
and `msg` using bt_log_write_printf():

    bt_log_write_printf(funcName, fileName, lineNo,
                        static_cast<bt_log_level>(level), tag, "%s%s",
                        initMsg, msg);

`_StdLogWriter` ignores the `initMsg` parameter, but we only call it
when `initMsg` is an empty string anyway (I'm adding an assertion to
confirm that). For example, logStrNoThrow() calls _logStrNoThrow() with
`_StdLogWriter` and an empty `initMsg`. Same for `_MemLogWriter`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I50a0fda253b4cc2169dcdc91c84b80d251b3b82b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11740

3 months agologging/log-api.c: add newline between assertion and declaration
Philippe Proulx [Fri, 2 Feb 2024 20:07:02 +0000 (15:07 -0500)] 
logging/log-api.c: add newline between assertion and declaration

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I7426c7496a17f5e6cc958a578c755257895fc979
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11739
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
3 months agosrc/Makefile.am: add libcpp-common convenience library
Simon Marchi [Mon, 5 Feb 2024 16:42:03 +0000 (11:42 -0500)] 
src/Makefile.am: add libcpp-common convenience library

Some following commits are expected to add .cpp files to the cpp-common
library.  Turn it into a convenience libtool library.

Add a temporary dummy.cpp file, since some platforms don't like building
a library without at least one compiled source file.

Change-Id: I702b242ca2d229ab5064ba36d422368fd9a03909
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11715
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agocpp-common: add `bt2s::span` 0.11.0 and other related symbols
Philippe Proulx [Wed, 31 Jan 2024 22:41:47 +0000 (17:41 -0500)] 
cpp-common: add `bt2s::span` 0.11.0 and other related symbols

This patch adds `bt2s::span` which wraps `nonstd::span`, that is,
span lite [1], a project written by the author also who wrote our
`bt2s::optional` implementation, therefore I trust him.

span lite is an implementation of `std::span` [2] for C++98 and up:

> The class template `span` describes an object that can refer to a
> contiguous sequence of objects with the first element of the sequence
> at position zero.

This will be useful to pass some data block as a single parameter
without copying the viewed data (in other words, a pointer and a length
with some sugar such as operator[]() and the like).

[1]: https://github.com/martinmoene/span-lite
[2]: https://en.cppreference.com/w/cpp/container/span

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ife35b4be41536fec15aef9d6603a80746c3e8727
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11728
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agocpp-common/bt2s: use a `using` declaration
Philippe Proulx [Wed, 31 Jan 2024 22:37:49 +0000 (17:37 -0500)] 
cpp-common/bt2s: use a `using` declaration

This is exactly the purpose of a `using` declaration, to make the symbol
from the `bpstd`/`nonstd` namespace accessible for unqualified lookup as
if declared in the `bt2s` namespace.

The code is much simpler.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I8348f409a463b85f5b2ed1f1fd8f61adb404a6d9
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11727
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

3 months ago`optional.hpp.license` belongs to `cpp-common/vendor/optional-lite`
Philippe Proulx [Wed, 31 Jan 2024 21:52:46 +0000 (16:52 -0500)] 
`optional.hpp.license` belongs to `cpp-common/vendor/optional-lite`

Forgot that in c022776a5 ("cpp-common: add `bt2s::optional`, alias of
`nonstd::optional`").

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ica468aa5b034b3eec42e41ffa9cfaa9615a42471
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11726
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

3 months agotests: fix namespace comment style
Simon Marchi [Fri, 2 Feb 2024 16:18:32 +0000 (16:18 +0000)] 
tests: fix namespace comment style

Change-Id: I6befd5bd154ebbba850077cefff6b113d9d47fda
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11738
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agoFix: lib: handle BT_FUNC_STATUS_NOT_FOUND in bt_plugin_so_create_all_from_sections
Simon Marchi [Mon, 15 Jan 2024 21:24:19 +0000 (16:24 -0500)] 
Fix: lib: handle BT_FUNC_STATUS_NOT_FOUND in bt_plugin_so_create_all_from_sections

When an SO plugin's init function returns an error and
fail_on_load_error is false, we hit an assert:

    (╯°□°)╯︵ ┻━┻  /home/smarchi/src/babeltrace/src/lib/plugin/plugin-so.c:1351: bt_plugin_so_create_all_from_sections(): Assertion `!plugin` failed.

When an error happens in the plugin's init function, bt_plugin_so_init returns BT_FUNC_STATUS_NOT_FOUND.  We leave the `plugin` variable set, which triggers the assert.

Fix this by putting/resetting `plugin` in that case.  Since there was an
error initializing the plugin, we won't return it to the user, so I
think that release it is the right thing to do here.

Add a test with a plugin whose init function fail.  Test both the
fail_on_load_error true and false cases.

Change-Id: I8e219f728a4b58d0e32307f907243892a02fdccf
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reported-by: Brice Videau <bvideau@anl.gov>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11679
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-by: Brice Videau <bvideau@anl.gov>
Tested-by: jenkins <jenkins@lttng.org>
3 months agocpp-common/bt2c: add CStringView equality and non-equality operators
Simon Marchi [Mon, 15 Jan 2024 21:25:29 +0000 (16:25 -0500)] 
cpp-common/bt2c: add CStringView equality and non-equality operators

Add `operator==` and `operator!=` functions to check equality or
non-equality between `CStringView` and another `CStringView`,
`const char *` and `std::string`.

Add a test to exercise those.

Change-Id: I2463966aae50792bb3e21ae65f9f124b08114499
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11681
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agocpp-common/bt2c: add `bt2c::IsOneOf` trait
Simon Marchi [Tue, 23 Jan 2024 17:30:43 +0000 (17:30 +0000)] 
cpp-common/bt2c: add `bt2c::IsOneOf` trait

Add the `bt2c::IsOneOf` trait, which is used to check if a type is in a
list of types.

Change-Id: If95ebc542d0581afb3411fcaece6617bb4946612
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11708
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agocpp-common/bt2: add basic PluginSet and bt_plugin_find_all_from_dir wrappers
Simon Marchi [Mon, 15 Jan 2024 21:19:24 +0000 (16:19 -0500)] 
cpp-common/bt2: add basic PluginSet and bt_plugin_find_all_from_dir wrappers

Add a `bt2::PluginSet` class, wrapping `bt_plugin_set`.  It only
contains what I need for a test coming in a subsequent patch.

Add a `bt2::findAllPluginsFromDir` function, wrapping
`bt_plugin_find_all_from_dir`.

Change-Id: Ieca897dec5c08381deb473e701f7e41aee84e89c
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11680
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agosrc: Makefile.am cleanups
Simon Marchi [Tue, 16 Jan 2024 04:13:54 +0000 (23:13 -0500)] 
src: Makefile.am cleanups

`shared-object.hpp` is there twice, remove one entry.  Move
`raw-value-proxy.hpp` to respect the alphabetical order.

Change-Id: I314191c2a06ff72c3e89ae4c87252fe8adaac054
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11686
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agotests: rename lib plugins test files
Simon Marchi [Mon, 15 Jan 2024 19:22:41 +0000 (14:22 -0500)] 
tests: rename lib plugins test files

Standardize the names for this test a bit.

Change-Id: I2d70332ed6f25505aac36be7da7a1bdb63da72b6
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11678
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agocpp-common/bt2: use bt2c::CStringView in parameters and return values throughout
Simon Marchi [Tue, 16 Jan 2024 18:31:06 +0000 (13:31 -0500)] 
cpp-common/bt2: use bt2c::CStringView in parameters and return values throughout

From what I saw, at -O2 with gcc, passing and returning CStringViews
generates the same code as passing and returning `const char *`.  Change
the `cpp-common/bt2` code to take a return C strings using CStringViews
when possible, which is more convenient than `const char *`.

Since std::string is implicitly convertible to CStringView, there is
generaly no need to have separate overloads that take std::strings.

I left the `CommonStringField::append(const std::string &)` overload,
because this one has an advantage over the `CStringView` overload: it
uses the size of the `std::string`, which is cheaper than calling
`std::strlen`.

There are some cases where leaving the `const char *` overload is
necessary.  For instance, when there is also a `bool` overload.  In that
case, passing a `const char *` to the function would prefer the `bool`
overload over the `CStringView` overload, which is not what we want.

Change-Id: I4c995c995da3edd2f0e9f7034053d52cb1541bb0
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11690
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agocpp-common/bt2c: make some constructors of CStringView constexpr
Simon Marchi [Tue, 16 Jan 2024 18:59:43 +0000 (13:59 -0500)] 
cpp-common/bt2c: make some constructors of CStringView constexpr

Change-Id: I56daad7dae1bc65183f016a1a9d2d6c529b3569d
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11689
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agocpp-common/bt2c: make CStringView convertible from std::string
Simon Marchi [Tue, 16 Jan 2024 18:58:25 +0000 (13:58 -0500)] 
cpp-common/bt2c: make CStringView convertible from std::string

This will make it possible to have functions that accept CStringViews:

    void foo(CStringView);

and pass in std::strings seamlessly:

    foo(myStdStr);

Change-Id: I76dc55438c2cd87f77ddec421dff7f119e53eff5
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11688
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agocpp-common/bt2c: simplify CStringView formatter
Simon Marchi [Mon, 15 Jan 2024 21:34:29 +0000 (16:34 -0500)] 
cpp-common/bt2c: simplify CStringView formatter

Use a format_as function instead of a full-fledged formatter.

Change-Id: I49a015a1c88a49cd7c4bab3ff1cbbf8cad8b6ba0
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11682
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agoinclude/babeltrace2/types.h: remove `bt_plugin_so_shared_lib_handle`
Philippe Proulx [Thu, 25 Jan 2024 17:43:20 +0000 (12:43 -0500)] 
include/babeltrace2/types.h: remove `bt_plugin_so_shared_lib_handle`

This was never meant to be public.

In theory, this patch is a breaking change, but I don't see why anybody
would use that type currently.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib832e67cb8b71201157cf83cb7ccac980c4ee57b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11711
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agolib: remove unused includes
Simon Marchi [Fri, 26 Jan 2024 15:33:32 +0000 (15:33 +0000)] 
lib: remove unused includes

Remove a bunch of includes that my IDE (VSCode + clangd) reports as
unused.  I didn't touch the logging and compat includes.

Change-Id: I5fe2cb4a332f000dc7d9b8c015bcd6382684fc6c
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11718
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agoinclude/babeltrace2/plugin/plugin-dev.h: "define" -> "definition
Philippe Proulx [Thu, 25 Jan 2024 17:47:20 +0000 (12:47 -0500)] 
include/babeltrace2/plugin/plugin-dev.h: "define" -> "definition

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ic915ca902f7688386d9a66724ae417bf0952fd56
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11712

3 months agotools/format-cpp.sh: ignore `.git` directory
Philippe Proulx [Tue, 23 Jan 2024 15:39:37 +0000 (10:39 -0500)] 
tools/format-cpp.sh: ignore `.git` directory

A `.hpp` or `.cpp` file could exist in `.git`, for example in
`.git/logs/refs/heads` if the branch name ends with `.hpp` or `.cpp`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I328486a81148fe46221bac8979ab6127ed4e3903
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11707
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agotests: remove unused _TestIntegerValue.test_create_neg
Simon Marchi [Wed, 22 Nov 2023 17:43:47 +0000 (12:43 -0500)] 
tests: remove unused _TestIntegerValue.test_create_neg

This test method is unused, since all sub-classes define a
test_create_neg method.  I confirmed this by:

 - placing an `assert False` inside it and running the test
 - comparing the number of tests ran as reported by the unittest runner,
   before and after the change

Change-Id: Ib2bd1cb919c85e6dddc21c0f305cfab8504d06e9
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11409
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agobt2: require msg for _OverflowError in _handle_func_status
Simon Marchi [Wed, 8 Nov 2023 16:21:54 +0000 (16:21 +0000)] 
bt2: require msg for _OverflowError in _handle_func_status

Pyright says:

    /home/smarchi/src/babeltrace/src/bindings/python/bt2/bt2/utils.py:160:19 - error: Invalid exception class or object
    Constructor for exception type "type[_OverflowError]" requires one or more arguments (reportGeneralTypeIssues)

That's true, _OverflowError has bt2_error._Error as a base, which
requires a message.  Change _handle_func_status to assert that a msg was
provided when handling that case.

Change-Id: I961c336cf53ce13e5f4241728e1131cccb77efcf
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11297
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agoFix: bt2: pass _TraceClassConst to destruction listeners
Simon Marchi [Mon, 27 Nov 2023 20:41:31 +0000 (15:41 -0500)] 
Fix: bt2: pass _TraceClassConst to destruction listeners

Trace class destruction listeners currently receive a _TraceClass.  I
think it's a mistake, in that they should receive a _TraceClassConst.
Fix that, tweak the tests to test it.  Test the same thing for trace
destruction listeners (they didn't suffer from the same issue).

Change-Id: I4651ffc44c916d64f6e6f2944fa856b4ed5bb5f6
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11447
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agoFix: lib: pass correct argument to BT_ASSERT_PRE_PLUGIN_SET_OUT_NON_NULL
Simon Marchi [Fri, 5 Jan 2024 15:26:57 +0000 (10:26 -0500)] 
Fix: lib: pass correct argument to BT_ASSERT_PRE_PLUGIN_SET_OUT_NON_NULL

Reported-By: Brice Videau <bvideau@anl.gov>
Change-Id: I88100e83c0d97ef52255863206923bf11ccc07b5
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11624
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Brice Videau <bvideau@anl.gov>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agotools: Add shellcheck.sh
Michael Jeanson [Tue, 7 Nov 2023 19:21:27 +0000 (14:21 -0500)] 
tools: Add shellcheck.sh

This script will run ShellCheck on all files in the tree with the '.sh'
extension and return a non-zero exit code if any of the files has an
issue.

Change-Id: Ib65dcf1c9bd447a92a773284cc138ec0ed01444b
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11293

3 months agofix: 'env.sh' is optional, ignore it in shellcheck
Michael Jeanson [Fri, 15 Dec 2023 21:50:19 +0000 (16:50 -0500)] 
fix: 'env.sh' is optional, ignore it in shellcheck

Since 'env.sh' is an optional file, don't fail shellcheck when it is
absent.

Change-Id: I28b768a6023b789f90e4433016e9025f2b5e2f21
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11597
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
3 months agoconfigure: use single word for PROJECT-NAME argument of DX_INIT_DOXYGEN
Simon Marchi [Wed, 3 Jan 2024 03:37:57 +0000 (22:37 -0500)] 
configure: use single word for PROJECT-NAME argument of DX_INIT_DOXYGEN

Running configure with --enable-api-doc shows this warning:

    /home/simark/src/babeltrace/configure: line 25508: 2: command not found

The line 25508 in question is:

  DX_PROJECT=Babeltrace 2

In shell script, this tries to execute the command `2`, with the
`DX_PROJECT` environment variable set to `Babeltrace`.  This is
obviously not what we want.

The documentation for the DX_INIT_DOXYGEN macro says:

   ... call DX_INIT_DOXYGEN with the following parameters: a one-word
   name for the project for use as a filename base etc.

So, we shouldn't pass something with a space as the project name to
DX_INIT_DOXYGEN.  Change it to `babeltrace2`.  I don't think that this
gets used anywhere though, as I didn't see a difference in my build
directory before and after the change (except in the Makefiles, which
don't use the value themselves).

Reported-by: Brice Videau <bvideau@anl.gov>
Change-Id: I5c6a86f3c93ea21d239bd3146eb8359a6c74eaa2
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11623
Reviewed-by: Brice Videau <bvideau@anl.gov>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agoFix typos
Simon Marchi [Mon, 8 Jan 2024 20:36:24 +0000 (15:36 -0500)] 
Fix typos

Run `typos -w` [1] on the repository, and cherry-pick the good changes.

[1] https://github.com/crate-ci/typos

Change-Id: I995cf7d851cd7ff40e47767ffe14c03fbd2d23bb
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11426
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agotests: remove stale comment in stored-values.mctf
Simon Marchi [Wed, 10 Jan 2024 21:27:49 +0000 (16:27 -0500)] 
tests: remove stale comment in stored-values.mctf

Change-Id: Id617e6abb33fdaeff065daa0211cdf8294c86b5d
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11643
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
3 months agoFix: doc: escape double quote in bt_p alias
Simon Marchi [Thu, 11 Jan 2024 20:00:17 +0000 (15:00 -0500)] 
Fix: doc: escape double quote in bt_p alias

The HTML doc shows some `<span class=` where the parameter names are
meant to appear.  Escape those double quotes to fix it.

The output was correct in the past, it only started to display the wrong
thing with this commit, in Doxygen 1.9.0:

https://github.com/doxygen/doxygen/commit/eb3d1eb5ad85c94d6f2c32934fce2b8630331d6c

Reported-By: Brice Videau <bvideau@anl.gov>
Change-Id: I61f3b557e3d8bf1428dd57e4c1ce950461e32479
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11647
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
4 months agofix: 'load_module()' deprecated in Python 3.12
Michael Jeanson [Thu, 14 Dec 2023 22:11:42 +0000 (17:11 -0500)] 
fix: 'load_module()' deprecated in Python 3.12

Starting with Python 3.10 we get the following warning:

  <frozen importlib._bootstrap>:283: DeprecationWarning: the
  load_module() method is deprecated and slated for removal in
  Python 3.12; use exec_module() instead

Replace our use of 'load_module()' with the recommended [1] alternative
that includes 'module_from_spec()' introduced in Python 3.5. Since we
need to support Python 3.4, keep the original code with a version check.

[1] https://docs.python.org/3.12/library/importlib.html#importlib.machinery.SourceFileLoader.load_module

Change-Id: I77d14f14fcc39e0227baad959c3e211aba309a21
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11583
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agologging: Add freebsd support
Michael Jeanson [Thu, 14 Dec 2023 20:42:15 +0000 (15:42 -0500)] 
logging: Add freebsd support

Add the FreeBSD platform specific code to get a thread id for the
logging system.

Change-Id: Ifd2a53c5c47e1b23a7f0a90c1fc0768e7f4dbcb3
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11582
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
4 months agoflt.utils.muxer: use a heap to sort iterators (also: rewrite in C++)
Philippe Proulx [Mon, 6 Nov 2023 21:16:54 +0000 (16:16 -0500)] 
flt.utils.muxer: use a heap to sort iterators (also: rewrite in C++)

The main goal of this patch was to use a heap instead of an array to
sort upstream message iterators so as to improve the performance of a
`flt.utils.muxer` message iterator managing a lot of usptream message
iterators, especially with a lot of connected input ports.

To do this, I first decided to use `std::priority_queue`, and since I'm
not a fan of mixed C and C++, I also decided to take the opportunity to
convert the whole component class C source to C++. This looked like a
good excuse to prove that it's possible, using only common C++ stuff,
including libbabeltrace2 bindings (`bt2` namespace).

Therefore, I'm glad to announce that `flt.utils.muxer` is the first
upstream C++-only component class! 🥳

In the end, because it's more efficient, I decided to write a C++
version of what used to be the `bt_heap_` C API, `bt2c::PrioHeap`, and
to use this instead of `std::priority_queue`. 🤷

This new version has more or less the same logic as the previous one. It
has three classes under the `bt2mux` namespace:

`Comp`:
    The filter component class which doesn't do much.

    Its main purpose is to add an available input port when one is
    connected.

`UpstreamMsgIter`:
    Wraps a `bt2::MessageIterator`, keeping an internal array of
    messages (what bt2::MessageIterator::next() returns), and offering
    the msg() (current message), msgTs() (cached timestamp, if any, of
    current message), discard(), and reload() methods to deal with a
    single message at a time.

    You must call always reload() before calling msg()/msgTs(), and if
    there's current message, you must call discard() before you call
    reload(). Making the caller responsible of this logic removes
    redundant conditions in this class. You may view the
    msg() + discard() pair as a form of message popping. The sequence is
    usually:

    1. We know there's a current message because the last call to
       reload() was successful.

    2. Get the current message with msg(), and then call discard() to
       discard/remove it.

    3. Try to call reload().

       This may return `UpstreamMsgIter::ReloadStatus::NO_MORE`; if it
       does, stop using this upstream message iterator.

       This may also throw `bt2::TryAgain`; if it does, call reload()
       again later.

    Caching the timestamp of the current message is an improvement
    because the heap rebalancing operation could need to use them
    several times when comparing two upstream message iterators.

    This class also offers canSeekBeginning(), seekBeginning(), and
    canSeekForward() which more or less wrap the corresponding
    `bt2::MessageIterator` methods.

`MsgIter`:
    The message iterator class.

    This is where most of the changes are.

    It keeps a `bt2c::PrioHeap` of `UpstreamMsgIter` pointers to sort
    them. The comparator works like before, so that the output message
    order of this version for a given set of upstream message iterators
    should be the same as the previous version.

    Like before, it has an array of pointers to upstream message
    iterators to reload. An upstream message iterator is either part of
    the heap, or part of this array. We need to call reload() on each of
    the upstream message iterators in this array. This is either
    because:

    * We put them here on `MsgIter` construction to avoid getting any
      exception from bt2::MessageIterator::next().

    * The last attempt to call reload() on them threw `bt2::TryAgain`.

    Before MsgIter::_next() selects a message to add to the message
    array, all non-ended upstream message iterators need to be part of
    the heap.

    The logic of clock class expectation is unchanged.

In general, there are more detailed comments, and logging is improved,
in particular the trace level statements of the heap comparator:

    [...] T PLUGIN/FLT.UTILS.MUXER/[muxer]/MSG-ITER operator()@plugins/utils/muxer/msg-iter.cpp:501 Comparing two messages: port-name-a=in0, msg-a-type=PACKET_BEGINNING, msg-a-ts=1441852773113941931, port-name-b=in1, msg-b-type=STREAM_BEGINNING, msg-b-ts=none
    [...] T PLUGIN/FLT.UTILS.MUXER/[muxer]/MSG-ITER operator()@plugins/utils/muxer/msg-iter.cpp:538 Message A has a timestamp, but message B has none: oldest=B
    ...
    [...] T PLUGIN/FLT.UTILS.MUXER/[muxer]/MSG-ITER operator()@plugins/utils/muxer/msg-iter.cpp:501 Comparing two messages: port-name-a=in2, msg-a-type=PACKET_END, msg-a-ts=1441852847496965372, port-name-b=in1, msg-b-type=PACKET_END, msg-b-ts=1441852847496953796
    [...] T PLUGIN/FLT.UTILS.MUXER/[muxer]/MSG-ITER operator()@plugins/utils/muxer/msg-iter.cpp:533 Timestamp of message A is greater than timestamp of message B: oldest=B

My preliminary experiments show that this version is slightly (a few
percentage points) more efficient than the previous one for a typical
four-stream scenario, and then around 12-13 times more efficient for a
208-stream scenario.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I2e393a2ef1c33c6b134284028807c687f8adf0e8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11273
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: return `bt2c::CStringView` instead of `const char *`
Simon Marchi [Mon, 11 Dec 2023 21:10:01 +0000 (16:10 -0500)] 
cpp-common/bt2: return `bt2c::CStringView` instead of `const char *`

This patch makes any method in `cpp-common/bt2` return
`bt2c::CStringView` instead of `const char *` when possible to improve
the usability of the wrapping API.

In `bt2::CommonStringField` and `bt2::CommonStringValue`, I didn't
bother having a different raw value type to set vs. get, therefore the
value() getter returns `bt2c::CStringView` while the value() setter has
overloads for `bt2c::CStringView`, `const char *`, and
`const std::string&`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iab7c7ea9a97035b9bea018f918a5d52516ab676c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11486
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2c: add `bt2c::CStringView`
Philippe Proulx [Tue, 5 Dec 2023 07:00:28 +0000 (02:00 -0500)] 
cpp-common/bt2c: add `bt2c::CStringView`

A `bt2c::CStringView` is like a `bt2s::string_view`, but specialized for
strings that are known to be null-terminated (C strings).

This is useful when wrapping an existing API which has functions
returning `const char *`: return `bt2c::CStringView` from the wrappers
at no cost, but with improved usability:

    if (myObj.name().len() >= 16) {
        // ...
    }

vs.

    if (std::strlen(myObj.name()) >= 16) {
        // ...
    }

A `bt2c::CStringView` only computes the length of the underlying
C string with std::strlen() when needed. It can convert to `std::string`
and `bt2s::string_view`.

`c-string-view.hpp` includes an {fmt} formatter for convenience. It
writes `(null)` when the underlying pointer is null, just like the glibc
printf() does.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I522ce586e9966736dadeeabf30f02da5dc62c80c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11485
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agoplugins/common/muxing/muxing.c: use BT_ASSERT_DBG()
Philippe Proulx [Wed, 29 Nov 2023 17:23:59 +0000 (12:23 -0500)] 
plugins/common/muxing/muxing.c: use BT_ASSERT_DBG()

Potential fast path.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ic269b7ca8877ab6eff482a112d1f1133ee48fd6b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11464
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common: fix `#include` patterns
Simon Marchi [Mon, 11 Dec 2023 20:06:12 +0000 (15:06 -0500)] 
cpp-common: fix `#include` patterns

Include from the same directory directly, and from everything else
relative to `src/`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie6dd5777d884911b9ad736ef625b784930ee3a23
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11457
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agobt_secure_getenv(): add missing `unused` attribute for log level
Philippe Proulx [Tue, 28 Nov 2023 19:45:25 +0000 (14:45 -0500)] 
bt_secure_getenv(): add missing `unused` attribute for log level

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I01c209976ebec9aa69227545341b6ccefe591fe2
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11456
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agoinclude/babeltrace2: add `noexcept` specifier for C++ ≥ 11
Philippe Proulx [Sat, 25 Nov 2023 16:13:57 +0000 (11:13 -0500)] 
include/babeltrace2: add `noexcept` specifier for C++ ≥ 11

This patch adds the `noexcept` specifier to all the public functions
when building with C++11 and above.

This is similar to what glibc does (from `/usr/include/sys/cdefs.h`):

    #   if __cplusplus >= 201103L
    #    define __THROW noexcept (true)
    #   else
    #    define __THROW throw ()
    #   endif

and then, for example (`/usr/include/string.h`):

    /* Return the length of S.  */
    extern size_t strlen (const char *__s)
         __THROW __attribute_pure__ __nonnull ((1));

Indeed, none of our functions throw, ever, including user callbacks,
because the library always expects them to return some value (doesn't
catch, in other words).

This brings an improvement to a `noexcept` function calling a
libbabeltrace2 function. For example, consider this simple case:

    uint64_t get_cs_value(const bt_clock_snapshot * const opt_cs) noexcept
    {
        return bt_clock_snapshot_get_value(opt_cs);
    }

When bt_clock_snapshot_get_value() isn't `noexcept`, we get (`-O2`):

    get_cs_value(bt_clock_snapshot const*):
            sub     rsp, 8
            call    bt_clock_snapshot_get_value
            add     rsp, 8
            ret

With `noexcept`:

    get_cs_value(bt_clock_snapshot const*):
            jmp     bt_clock_snapshot_get_value

In other words, without this patch, there's some stack adjustment to
prepare for a possible exception when you're already in `noexcept` mode.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I0712eab48de93344159b7b7344f28b940ed81712
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11442
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2/wrap.hpp: add wrapOptional() versions
Philippe Proulx [Sun, 26 Nov 2023 01:08:11 +0000 (20:08 -0500)] 
cpp-common/bt2/wrap.hpp: add wrapOptional() versions

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I28ee353257f4b88e78741c512e974be6bd286ed4
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11441
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: remove useless `avail` variable where applicable
Philippe Proulx [Sun, 26 Nov 2023 01:05:58 +0000 (20:05 -0500)] 
cpp-common/bt2: remove useless `avail` variable where applicable

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ia7be2c466f4d73f5d5ab1271f918f3f89d3498ec
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11440
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: use `bt2::OptionalBorrowedObject` where possible
Philippe Proulx [Sun, 26 Nov 2023 01:04:47 +0000 (20:04 -0500)] 
cpp-common/bt2: use `bt2::OptionalBorrowedObject` where possible

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iba2056e520bbe4ad4843a51e51f99173e4929c4f
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11439
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2: add `bt2::OptionalBorrowedObject`
Philippe Proulx [Fri, 24 Nov 2023 18:05:58 +0000 (13:05 -0500)] 
cpp-common/bt2: add `bt2::OptionalBorrowedObject`

An instance of this new class template manages an optional contained
borrowed object of type `ObjT`, that is, a borrowed object that may or
may not be present.

Such an object considers that a `nullptr` libbabeltrace2 object pointer
means none. Therefore, using a `bt2::OptionalBorrowedObject` isn't more
costly, in time and space, as using a libbabeltrace2 object pointer in
C, but offers the typical C++ optional interface.

There's no `bt2s::nullopt` equivalent: just call reset().

An optional borrowed object stores the library pointer itself, only
constructing `ObjT` when it's known that the library pointer isn't
`nullptr`.

The class is pretty straightforward, except that:

* It has a few constructors and assignment operators to make things such
  as this work:

      bt2::OptionalBorrowedObject<bt2::ConstValue> f(const bt2::StringValue val)
      {
          if (std::string {*val} == "meow") {
              return val;
          }

          return {};
      }

  Also, you may assign a `bt2::OptionalBorrowedObject<Y>` instance to a
  `bt2::OptionalBorrowedObject<X>` instance if `Y` inherits `X`.

* operator->() would need to return the address of some `ObjT` instance,
  but an optional borrowed object has none, so return a proxy instead.

The result is a very natural usage:

    std::uint64_t getCsValue(const bt2::OptionalBorrowedObject<bt2::ConstClockSnapshot> optCs)
    {
        return optCs ? optCs->value() : 0;
    }

When optimized, the resulting instructions are the same as for an
equivalent C function:

    getCsValue(bt2::Optional<bt2::ConstClockSnapshot>):
            test    rdi, rdi
            je      .L2
            jmp     bt_clock_snapshot_get_value
    .L2:
            xor     eax, eax
            ret

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ic873ea4ff8b0f084189ece3ac139680469c907d3
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11438
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2: make `bt2::BorrowedObject::LibObj` public
Philippe Proulx [Fri, 24 Nov 2023 03:38:28 +0000 (22:38 -0500)] 
cpp-common/bt2: make `bt2::BorrowedObject::LibObj` public

This patch changes the protected `bt2::BorrowedObject::_LibObjPtr`
to the public `bt2::BorrowedObject::LibObjPtr`.

There's already a public libObjPtr() method anyway which used to return
`_LibObjPtr`.

Also adding `LibObj` which is the object without the pointer part.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I23c841bdc9f58b4b3ecb9c7dee0403d49f048c1d
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11437
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2c: move `uuid-view.*pp` contents to `uuid.hpp`
Simon Marchi [Mon, 11 Dec 2023 21:09:28 +0000 (16:09 -0500)] 
cpp-common/bt2c: move `uuid-view.*pp` contents to `uuid.hpp`

The two methods in `uuid-view.cpp`, a file which wasn't even mentioned
in `src/Makefile.am`, existed to break the cycle because `bt2c::Uuid`
and `bt2c::UuidView` need to know eachother.

Instead, remove `uuid-view.cpp`, and put both `bt2c::Uuid` and
`bt2c::UuidView` in `uuid.hpp`, inlining everything and keeping the
close relationship between those two classes.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ia27de9f23aaa5e38beb446c7bafcae1fb2f17aa7
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11429
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2c: add bt2c::call()
Philippe Proulx [Thu, 23 Nov 2023 05:46:16 +0000 (00:46 -0500)] 
cpp-common/bt2c: add bt2c::call()

This new function partially implements INVOKE.

It's bt2c::call() instead of bt2s::invoke() because it's not a perfect
drop-in replacement (not forwarding `func` because
`std::reference_wrapper` requires that the parameter isn't an rvalue).

The std::ref() trick is taken from [1].

My main use case for this is IIFE (initializing some `const` variable
from a parameter-less lambda call) without risking to forget the
trailing `()` to actually call it:

    const auto meow = [&aqua, &fina] {
        // some steps
        return mix;
    }; // oops, forgot `()` here

Instead:

    const auto meow = bt2c::call([&aqua, &fina] {
        // some steps
        return mix;
    });

When you see bt2c::call(), you know there's a call.

You can also use bt2c::call() for most use cases [2] of std::invoke()
(C++17), for example:

    template <typename MethT, typename ObjT>
    void f(MethT&& meth, ObjT&& obj, const int val)
    {
        bt2c::call(std::forward<MethT>(meth), std::forward<ObjT>(obj), val, 42);
    }

    struct Meow
    {
        void mix(const int a, const int b)
        {
            std::cout << a << ":" << b << std::endl;
        }
    };

    int main()
    {
        Meow meow;

        f(&Meow::mix, meow, 17);
    }

[1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0312r1.html
[2]: https://devblogs.microsoft.com/oldnewthing/20220401-00/?p=106426

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib7db0315ee6f68f168793488a7da67c1bc410fb8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11425
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: add missing ConstClockSnapshot::clockClass()
Philippe Proulx [Thu, 23 Nov 2023 05:45:49 +0000 (00:45 -0500)] 
cpp-common/bt2: add missing ConstClockSnapshot::clockClass()

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iea8665b8b284c236cbf20dda4fb34fe37137d168
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11424
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2c: add `bt2c::PrioHeap` (C++ version of `bt_heap_` C API)
Philippe Proulx [Wed, 22 Nov 2023 21:27:39 +0000 (16:27 -0500)] 
cpp-common/bt2c: add `bt2c::PrioHeap` (C++ version of `bt_heap_` C API)

This new class template is a templated C++ version of
`src/lib/prio-heap/prio-heap.*` (C API), written by Mathieu Desnoyers,
which implements an efficient heap data structure.

In true C++ spirit, it accepts and stores a comparator (which may be an
object; `std::greatest` instance by default) instead of a simple
function pointer, making it possible for the user to store some context.

This version copies instances of `T` during its operations, so it's best
to use with small objects such as pointers, integers, and small PODs.

A benefit over `std::priority_queue`, which offers a similar interface,
is the replaceTop() method which performs a single heap rebalance when
you need to remove the top (greatest) element and immediately insert one
(possibly the same one).

`src/lib/prio-heap/prio-heap.*` are removed because they're not used
anywhere in the project and we only plan to write new C++ code anyway in
the future.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I830ab79ef067c0ebb2fc33466b3dc831c617a049
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11419
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2: add bt2::CommonEventMessage::streamClassDefaultClockClass()
Philippe Proulx [Wed, 22 Nov 2023 21:15:40 +0000 (16:15 -0500)] 
cpp-common/bt2: add bt2::CommonEventMessage::streamClassDefaultClockClass()

This has its equivalent libbabeltrace2 function
(bt_message_event_borrow_stream_class_default_clock_class_const()), but
didn't exist.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I985427cdd163341820de7dff0169b3aadcf58e0f
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11416
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2/shared-object.hpp: make the optional part public
Philippe Proulx [Fri, 17 Nov 2023 20:04:27 +0000 (15:04 -0500)] 
cpp-common/bt2/shared-object.hpp: make the optional part public

Make it possible to have an empty shared object instead of needing two
`bt2s::optional` (the `bt2::SharedObject` one and the user one).

Adding a default constructor to build an empty shared object and the
`bool` operator to check whether or not it's empty; similar to
`std::shared_ptr`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I646cf883eba47ff1058c95d1b027396ee713fa0b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11399
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2: add `plugin-dev.hpp`
Philippe Proulx [Wed, 8 Nov 2023 16:28:08 +0000 (11:28 -0500)] 
cpp-common/bt2: add `plugin-dev.hpp`

This new file makes it possible to write pure C++ component and message
iterator classes without having to deal with C methods.

`plugin-dev.hpp` offers this to the C++ component class author:

* Inherit `bt2::UserSourceComponent`, `bt2::UserFilterComponent`, or
  `bt2::UserSinkComponent` to implement a C++ component class.

  This class template uses the CRTP to implement static polymorphism.
  Its only template parameter is your actual user component class.

  The protected constructor of those base classes accept a specific self
  component wrapper and keeps it privately.

  The constructor also accepts a logging tag prefix with which it builds
  a protected `bt2c::Logger` named `_mLogger` so that you may use the
  BT_CPPLOG*() macros in your own component class. The constructor
  appends `/[`, the name of the component, and `]` to the logging tag
  (for example, if your tag prefix is `SRC.CTF.FS`, working with a
  component named `mein-comp`, then the complete tag is
  `SRC.CTF.FS/[mein-comp]`).

  Those base classes implement default, overloadable methods
  (_inputPortConnected() and _getSupportedMipVersions(), for example)
  when possible.

  They also offer protected methods which are proxies of the contained
  specific self component wrapper, for example _loggingLevel() and
  _addOutputPort().

* Inherit `bt2::UserMessageIterator` to implement a C++ message
  iterator.

  This class template uses the CRTP to implement static polymorphism.
  Its first template parameter is your actual user message iterator
  class.

  The other parameter is your C++ component class, for example:

      class MeinSourceComponent :
          public bt2::UserSourceComponent<MeinSourceComponent>
      {
          friend bt2::UserSourceComponent<MeinSourceComponent>;

          // ...
      };

      class MeinMessageIterator :
          public bt2::UserMessageIterator<MeinMessageIterator,
                                          MeinSourceComponent>
      {
          friend bt2::UserMessageIterator<MeinMessageIterator,
                                          MeinSourceComponent>;
          // ...
      };

  This makes MeinMessageIterator::_component() return
  `MeinSourceComponent&` (parent source component).

  `bt2::UserMessageIterator` also offers the a protected `bt2c::Logger`
  named `_mLogger` so that you may use the BT_CPPLOG*() macros in your
  own message iterator class. When you build a
  `bt2::UserMessageIterator`, you pass a logging tag suffix which will
  be appended to the logging tag of the main logger of the component to
  form the complete tag, for example `SRC.CTF.FS/[mein-comp]/MSG-ITER`.

  The public next() method (called by the bridge) implements the very
  common pattern of appending messages into the output array, and,
  meanwhile:

  If it catches a `bt2::TryAgain` exception:
      If the message array isn't empty, transform this into a success
      (don't throw).

      Otherwise rethrow.

  If it catches an error:
      If the message array isn't empty, transform this into a success
      (don't throw), but save the error of the current thread and the
      type of error to throw the next time the user calls next().

      Otherwise rethrow.

  The derived class must implement:

      void _next(bt2::ConstMessageArray& messages);

  This method fills `messages` with at most `messages.capacity()`
  messages and may throw `bt2::TryAgain` or a valid error whenever.
  Leaving an empty `messages` means the end of iteration.

* When you inherit `bt2::User*Component` and `bt2::UserMessageIterator`,
  your own methods may throw allowed exceptions (in `exc.hpp`): they'll
  get translated to corresponding library status codes.

  In general, prefer using BT_CPPLOGE_APPEND_CAUSE_AND_THROW() or
  BT_CPPLOGE_APPEND_CAUSE_AND_RETHROW().

* Use BT_CPP_PLUGIN_SOURCE_COMPONENT_CLASS(),
  BT_CPP_PLUGIN_FILTER_COMPONENT_CLASS(), or
  BT_CPP_PLUGIN_SINK_COMPONENT_CLASS() to translate your C++ component
  and message iterator classes into library classes, for example:

      BT_CPP_PLUGIN_SOURCE_COMPONENT_CLASS(mein, MeinSourceComponent,
                                           MeinMessageIterator);

  Those macros take care of all the C methods.

  You may still add a description and help with the usual
  BT_PLUGIN_*_COMPONENT_CLASS_DESCRIPTION() and
  BT_PLUGIN_*_COMPONENT_CLASS_HELP() macros.

  You may also used the `_WITH_ID` versions if your component class name
  isn't a valid C identifier.

Internally, the C++ plugin macros use bridging classes (thank you Simon
for the initial version of those). A bridging class offers static
methods which have the signature which libbabeltrace2 expects: such a
method calls the corresponding instance method of the user C++ class.

For example,
bt2::internal::MsgIterClsBridge<MeinMessageIterator>::canSeekBeginning()
calls MeinMessageIterator::canSeekBeginning() with the correct instance
which in turns calls MeinMessageIterator::_canSeekBeginning().

The init() method of a bridging class instantiates the user C++ class
and keeps it as the private data of the C class. The finalize() method
of a bridging class destroys the user C++ class instance.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: If8ff312b3557202f1a4dfb1584c9abff1cd35e03
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11299
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common: add `logging.hpp` (`bt2c::Logger` and BT_CPPLOG*() macros)
Philippe Proulx [Tue, 14 Nov 2023 19:25:39 +0000 (14:25 -0500)] 
cpp-common: add `logging.hpp` (`bt2c::Logger` and BT_CPPLOG*() macros)

This new file contains the `bt2c::Logger` class of which an instance
contains:

* An actor: a self component class, a self component, a self message
  iterator, or a named module.

* A current logging level.

* A logging tag.

The class offers the logNoThrow(), logMemNoThrow(), logErrnoNoThrow(),
logErrorAndThrow(), logErrorAndRethrow(), logErrorErrnoAndThrow(), and
logErrorErrnoAndRethrow() method templates to log using a given level,
optionally append a cause to the error of the current thread using the
correct actor, and optionally throw or rethrow.

The methods above expect a format string and zero or more arguments to
be formatted with fmt::format(). There are also versions with `Str` in
the name which accept a single string.

All the logging methods are `const` so that you may log with a
`const bt2c::Logger`: they don't change any user-visible state.

Then, the various BT_CPPLOG*() macros call one of the log*() methods on
a logger instance with the current file name, function name, and line
number, forwarding the other arguments to the method. Those macros are
unfortunately needed for the file/function/line information, similar to
SPDLOG_LOGGER_INFO() vs. the info() method.

The macros of which the name ends with `_SPEC` accept a specific logger;
the other ones use the fixed `_mLogger` logger, which will be the case
most of the time (logging from a user component or message iterator).

Example:

    BT_CPPLOGE_APPEND_CAUSE_AND_THROW(Error,
                                      "Failed to add {0} items to `{1}` (expecting {0}, not {2})",
                                      expCount, containerName, count);

Internally, the logging methods fill `_mBuf` with fmt::format_to() and
pass the resulting string to some bt_log_write*() function. Argument
evaluation and formatting only happens if logging and/or error cause
appending will also happen.

Create a logger from another one with a different tag like this:

    MyThing::MyThing(const bt2c::Logger& parentLogger, ...) :
        _mLogger {parentLogger, "MY-THING"},
        ...

Make some code conditional to having some logging active for a given
level with the wouldLog*() methods (uses BT_LOG_ON_CUR_LVL()
internally):

    if (_mLogger.wouldLogD()) {
        // code which only exists to log
        BT_CPPLOGD(...);
    }

Note that this first version is not thread-safe (the underlying logging
framework is, but `_mBuf` isn't protected for a given logger).

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iacb4c26e42c868f27e3a85b9fe163acac870624f
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11388
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agosrc/cpp-common: add {fmt} 10.1.1
Philippe Proulx [Tue, 14 Nov 2023 17:06:29 +0000 (12:06 -0500)] 
src/cpp-common: add {fmt} 10.1.1

This very famous library [1] should be useful to format all sorts of
strings in the future instead of using `std::string` or
`std::ostringstream`, as well as to implement a C++ logger.

We're building `format.cc` and `os.cc` which should speed up the rest of
the build and maybe reduce the build size (not using `FMT_HEADER_ONLY`).

Adding `.reuse/dep5` to provide licensing information for all the source
files in `src/cpp-common/vendor/fmt` without modifying the original
ones.

[1]: https://fmt.dev/

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I65cfbca05e5481847ce7183a2a2b20c25ae260ad
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11387
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agologging: strip down and clean `log.h` and `log.c`
Simon Marchi [Mon, 11 Dec 2023 20:04:49 +0000 (15:04 -0500)] 
logging: strip down and clean `log.h` and `log.c`

`log.h` slowly became a mix of the original zf_log [1] file and our own
changes over that.

I find that when I need to make changes to those files, I must consider
lots of features that we just never used and probably never will.
Amongst them are:

* Portability with MSVC and Android.

* Default values (`BT_LOG_DEF_*`).

* Tag prefix.

* Custom source location format.

* Censoring.

* All the global vs. local state, for example the global output level,
  all the BT_LOG*_AUX() macros.

* Library prefix (we just hardcoded `BT_` and `bt_` long ago).

* Mask of log message parts.

* Custom output callback.

Moreover, some of those features add execution time. For example,
logging always goes through vsnprintf(), even if you want to log a
simple string without any sophisticated formatting. Also, the actual
write() call is done through a callback because the output is
configurable. So is stuff like time formatting. We just don't need that.

This patch strips down those two files to make future changes easier. In
fact, it's pretty much a rewrite, but honors the current API, except for
the BT_LOG_WRITE*() functions (renamed, see below) and for
`BT_MINIMAL_LOG_LEVEL` which I renamed to `BT_LOG_MINIMAL_LEVEL` to keep
the same prefix.

This patch also moves everything which doesn't depend on user
definitions to `log-api.h`, and renames `log.c` to `log-api.c` for
consistency.

The user definitions are `BT_LOG_OUTPUT_LEVEL` and `BT_LOG_TAG`, which
you normally use as such:

    #define BT_LOG_OUTPUT_LEVEL my_level
    #define BT_LOG_TAG          "MY-TAG"
    #include "logging/log.h"

This gives access to BT_LOGI() and such which rely on those.

I think it's cleaner to separate the independent stuff. For example, not
that it's a true use case, but you could want to use the `log.h` macros
as well as another API which relies on `logging/log-api.h` at the same
time:

    #include "my-logger.hpp"

    #define BT_LOG_OUTPUT_LEVEL my_level
    #define BT_LOG_TAG          "MY-TAG"
    #include "logging/log.h"

Then an instance of some logger class in `my-logger.hpp` may have its
own independent output level and tag. Without this patch, you would need
to know that `my-logger.hpp` includes `logging/log.h` itself and
therefore make sure to define `BT_LOG_OUTPUT_LEVEL` and `BT_LOG_TAG`
before you include it:

    #define BT_LOG_OUTPUT_LEVEL my_level
    #define BT_LOG_TAG          "MY-TAG"
    #include "my-logger.hpp"

This works when you know it, but I believe it's less straightforward.

Now, the `log-api.h` interface is much simpler. The only mandatory
definition is `BT_LOG_MINIMAL_LEVEL`.

bt_log_write():
    Logs a message (no formatting).

bt_log_write_printf():
    Formats a message and logs it.

bt_log_write_mem():
    Logs a message (no formatting) and dumps a memory block.

bt_log_write_mem_printf():
    Formats a message, logs it, and dumps a memory block.

bt_log_write_errno():
    Logs an initial message, the string of the current `errno`, and
    a message (no formatting).

bt_log_write_errno_printf():
    Logs an initial message, the string of the current `errno`, and
    a formatted message.

All the functions above accept a file name, a function name, a line
number, a log level, and a tag. They log unconditionally, meaning they
don't check any current (run-time) log level. Use BT_LOG_ON_CUR_LVL() or
BT_LOG_ON() for that (unchanged).

All the BT_LOG_WRITE*() macros expand to calling the functions above. I
didn't change the BT_LOGT*(), BT_LOGD*(), BT_LOGI*(), BT_LOGW*(),
BT_LOGE*(), and BT_LOGF*() ones, but the BT_LOG_WRITE*() ones now have
`PRINTF` in their name when there's formatting (they didn't before):

* BT_LOG_WRITE_CUR_LVL()
* BT_LOG_WRITE()
* BT_LOG_WRITE_PRINTF_CUR_LVL()
* BT_LOG_WRITE_PRINTF()
* BT_LOG_WRITE_MEM_CUR_LVL()
* BT_LOG_WRITE_MEM()
* BT_LOG_WRITE_MEM_PRINTF_CUR_LVL()
* BT_LOG_WRITE_MEM_PRINTF()
* BT_LOG_WRITE_ERRNO_CUR_LVL()
* BT_LOG_WRITE_ERRNO()
* BT_LOG_WRITE_ERRNO_PRINTF_CUR_LVL()
* BT_LOG_WRITE_ERRNO_PRINTF()

Everything else is meant to work as before.

`log-api.c` is considerably simpler (than the previous `log.c`),
implementing exactly what we need in the project and no more. The only
output format is the current colored one (not configurable) and the only
destination is the standard error stream.

I did rewrite the memory block printing while being here. It used to
look like this:

    554889e5534883ec4864488b042528000000488945e831c04883ec086a17488d  UH??SH??HdH??%(???H?E?1?H???j?H?
    0520c900005068800000004c8d0dceffffff4c8d05f3c80000b903000000ba22  ? ???Ph????L??????L????????????"
    000000488d05e7c800004889c6488d05ecc800004889c7b800000000e8a6bb00  ???H??????H??H??????H???????????
    004883c420488d45d0be000000004889c7e88a090000488d45d04889c7e8e715  ?H?? H?E??????H???????H?E?H?????

and now looks like this:

    55 48 89 e5 53 48 83 ec 48 64 48 8b 04 25 28 00 | UH..SH..HdH..%(.
    00 00 48 89 45 e8 31 c0 48 83 ec 08 6a 17 48 8d | ..H.E.1.H...j.H.
    05 50 b9 00 00 50 6a 64 4c 8d 0d d1 ff ff ff 4c | .P...PjdL......L
    8d 05 26 b9 00 00 b9 03 00 00 00 ba 22 00 00 00 | ..&........."...
    48 8d 05 1a b9 00 00 48 89 c6 48 8d 05 15 b9 00 | H......H..H.....
    00 48 89 c7 b8 00 00 00 00 e8 98 ac 00 00 48 83 | .H............H.
    c4 20 48 8d                                     | . H.

I find that my version is more readable. The number of bytes/line is
fixed (16, like the default of `od` and `hexdump`).

I made the formatted date/time string cache much more simple, simply
using a TLS variable. Now it's just:

    struct date_time_cache {
        uint64_t s;
        uint32_t ms;
        char str[128];
    };

    static __thread struct date_time_cache date_time_cache = {0};

We're already using a TLS variable for the message buffer.

`log.c` used to call WriteFile(), GetLocalTime(), and
GetCurrentProcessId(), but I believe we have access to some MinGW magic
making it possible to call write(), gettimeofday(), and getpid(). The
TID part remains very platform-specific.

If we ever need new logging features, we can easily implement them, or
cherry-pick them from zf_log if they exist.

Project files are adapted here and there, mostly to match the few
renamed functions and macros.

[1]: https://github.com/wonder-mice/zf_log

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Icee002ccad33be0c610aeabfb39c4f8b1fe5ccb2
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11398
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common: move `nlohmann/json.hpp` to `vendor`
Philippe Proulx [Wed, 15 Nov 2023 06:10:16 +0000 (01:10 -0500)] 
cpp-common: move `nlohmann/json.hpp` to `vendor`

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Idb5725a6a4655cee2f99300308b45236300134e2
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11394
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common: add `bt2s::string_view`, alias of `bpstd::string_view`
Philippe Proulx [Wed, 15 Nov 2023 05:37:27 +0000 (00:37 -0500)] 
cpp-common: add `bt2s::string_view`, alias of `bpstd::string_view`

In order to avoid having all sorts of external namespaces, make
`bt2s::string_view` an alias of `nonstd::string_view`, and make sure
everything in the project uses `bt2s::string_view`.

Also add aliases for other STL types of `<string_view>`, like the
`bt2s::basic_string_view` class template.

This will make it possible to easily change the implementation without
changing the code using it indirectly (only
`cpp-common/bt2s/string-view.hpp` will change).

Move `cpp-common/string_view.hpp` to
`cpp-common/vendor/string_view-standalone/string_view.hpp` to make it
clear it's an external project.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I5172f642f5490b4a7eb21ed7cca55039452dc9b8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11392
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common: add `bt2s::optional`, alias of `nonstd::optional`
Simon Marchi [Mon, 11 Dec 2023 21:07:18 +0000 (16:07 -0500)] 
cpp-common: add `bt2s::optional`, alias of `nonstd::optional`

In order to avoid having all sorts of external namespaces, make
`bt2s::optional` an alias of `nonstd::optional`, and make sure
everything in the project uses `bt2s::optional`.

Also add aliases for other STL types and functions of `<optional>`.

This will make it possible to easily change the implementation without
changing the code using it indirectly (only
`cpp-common/bt2s/optional.hpp` will change).

Move `cpp-common/optional.hpp` to
`cpp-common/vendor/optional-lite/optional.hpp` to make it clear it's an
external project.

Making everything under `cpp-common/vendor` an exception in
`tools/format-cpp.sh`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I2a1ea52c484c85fbf8d68631d40369efd969818a
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11391
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common: rename bt2c::makeUnique() -> bt2s::make_unique()
Philippe Proulx [Wed, 15 Nov 2023 04:47:38 +0000 (23:47 -0500)] 
cpp-common: rename bt2c::makeUnique() -> bt2s::make_unique()

I now want everything mimicking the STL stuff to have the same names,
but I don't want to mix coding styles in `bt2c`.

Therefore introduce this new `bt2s` (the `s` stands for `std`) namespace
to contain everything acting as the STL (or Boost, if need be).

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie24c11170cc650028f3ce50fcab2004f0195c12f
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11393
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common: rename `bt2_common` namespace -> `bt2c`
Simon Marchi [Mon, 11 Dec 2023 21:05:13 +0000 (16:05 -0500)] 
cpp-common: rename `bt2_common` namespace -> `bt2c`

I always end up using the `bt2c` namespace alias in `*.cpp` files
anyway.

Also put all the `bt2c` stuff in `cpp-common/bt2c` to make it clear.

What remains in `cpp-common`, but outside `cpp-common/bt2c`, are the
libbabeltrace2 C++ bindings (`bt2` namespace) and external projects.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ife8527aa961025f28d06707eb9f374cafea572f5
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11390
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common: remove `cfg-*.hpp` and `log-cfg.hpp`
Philippe Proulx [Tue, 14 Nov 2023 20:27:01 +0000 (15:27 -0500)] 
cpp-common: remove `cfg-*.hpp` and `log-cfg.hpp`

Those are not used currently and I plan to replace them with something
more woke for C++.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ied48a1f6a5c32426a8d85e32c6a5682c48dd4e42
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11389
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: add bt2::wrap() overloads
Philippe Proulx [Sat, 4 Nov 2023 19:34:09 +0000 (15:34 -0400)] 
cpp-common/bt2: add bt2::wrap() overloads

You may now call bt2::wrap() with any libbabeltrace2 object pointer with
which you could build a borrowed object wrapper. Therefore, this
excludes `bt2::MessageArray` which is a special case (ownership is
transferred).

For example:

    void f(bt_value * const val)
    {
        const auto wrappedVal = bt2::wrap(val);

        if (wrappedVal.isUnsignedInteger()) {
            *wrappedVal = 23;
        }

        // ...
    }

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I49cc7121bb3c85dc49b9682c219940d4cb2aab5d
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11240
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2: add `bt2::SelfComponentClass`
Philippe Proulx [Fri, 1 Dec 2023 19:50:32 +0000 (14:50 -0500)] 
cpp-common/bt2: add `bt2::SelfComponentClass`

A very straightforward wrapper of `bt_self_component_class`.

I didn't bother wrapping the individual
`bt_self_component_class_source`, `bt_self_component_class_filter`, and
`bt_self_component_class_sink` as they don't offer anything more.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie96c61ba723d54ff720df9147279d7826dcf8fae
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11480
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: add `bt2::PrivateQueryExecutor`
Philippe Proulx [Wed, 8 Nov 2023 14:53:39 +0000 (09:53 -0500)] 
cpp-common/bt2: add `bt2::PrivateQueryExecutor`

A very straightforward wrapper of `bt_private_query_executor`.

There should exist an asConstQueryExecutor() method, but
`bt2::ConstQueryExecutor` doesn't exist yet as we don't need it to write
plugins. Therefore the loggingLevel() and isInterrupted() use
bt_private_query_executor_as_query_executor_const() internally.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I87c26e14afa4aa7937692aefa981213ad6a7f566
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11298
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: add `bt2::SelfMessageIteratorConfiguration`
Philippe Proulx [Mon, 6 Nov 2023 19:23:17 +0000 (14:23 -0500)] 
cpp-common/bt2: add `bt2::SelfMessageIteratorConfiguration`

A very straightforward wrapper of
`bt_self_message_iterator_configuration`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I0a31ee1feb1eefc7f6de3d87a4f43963d75b3274
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11272
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: add `bt2::SelfMessageIterator`
Philippe Proulx [Sat, 4 Nov 2023 17:40:43 +0000 (13:40 -0400)] 
cpp-common/bt2: add `bt2::SelfMessageIterator`

This patch introduces `bt2::SelfMessageIterator` which wraps
`bt_self_message_iterator`.

This is the last piece of C++ binding needed to write a complete
component class in C++, immediately wrapping the component class and
message iterator class methods parameters.

`bt2::SelfMessageIterator` offers:

createMessageIterator():
    Create a message iterator from this message iterator:

        auto msgIter = selfMsgIter.createMessageIterator(
                           selfMsgIter.component().inputPorts()["in"]);

component():
    Borrow the parent self component.

    This is usually used to retrieve some component data:

        selfMsgIter.component().data<MyComponentData>();

port():
    Borrow the output port from which this message iterator operates.

isInterrupted():
    Whether or not this message iterator is interrupted.

data():
    Get and set user data.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I02747ffb7a31743a310bdeff120f1d1042bf11e6
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11237
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2: add `bt2::Self*Component` and `bt2::SelfComponent*Port`
Philippe Proulx [Thu, 2 Nov 2023 21:04:36 +0000 (17:04 -0400)] 
cpp-common/bt2: add `bt2::Self*Component` and `bt2::SelfComponent*Port`

This new file is an adaptation of `component-port.hpp` and works the
same way.

That being said, I didn't bother:

* Making equivalent objects in `component-port.hpp` accept the "self"
  versions.

  In theory, you could build a `bt2::ConstSourceComponent` from a
  `bt2::SelfSourceComponent`, for example, but I believe we won't do
  that often, if ever.

  Therefore you'll need to use the asConstComponent() and asConstPort()
  methods explicitly.

  However, I added aliases in the new classes to avoid going calling
  those in most situations. For example,
  bt2::SelfFilterComponent::name() calls bt2::SelfComponent::name()
  which calls bt2::ConstComponent::name().

* Implementing the constant versions of the new classes, for example
  `bt2::ConstSelfComponent`.

  For example, a method which would benefit this is
  bt2::SelfSinkComponent::isInterrupted().

  Again, I don't think we'll need this. The "self" wrappers are for the
  component class author, and she rarely needs a constant view of its
  own object.

Add a port to a self component with the addInputPort() or
addOutputPort() methods, which return a borrowed reference of the
created and added port. Those methods have an overload which accepts
user data to store into the returned port.

Create a message iterator from a self sink component with
bt2::SelfSinkComponent::createMessageIterator().

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Id6bcbfcea4a562780eee0700cee507431c4e5661
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11235
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: add `bt2::MessageIterator`
Philippe Proulx [Wed, 1 Nov 2023 20:33:19 +0000 (16:33 -0400)] 
cpp-common/bt2: add `bt2::MessageIterator`

This patch adds `bt2::MessageIterator` to wrap `bt_message_iterator`
objects.

`bt2::MessageIterator` offers:

component():
    Borrows the parent component of the message iterator.

next():
    Returns the next batch of messages.

    This method returns an optional message array: set if there are
    messages (`BT_MESSAGE_ITERATOR_NEXT_STATUS_OK`), or not set on end
    of iteration (`BT_MESSAGE_ITERATOR_NEXT_STATUS_END`).

    Throws otherwise.

    Example:

        if (const auto msgs = myMsgIter.next()) {
            for (const auto msg : *msgs) {
                if (msg.isEvent()) {
                    // ...
                }
            }
        } else {
            // This is the end, my only friend, the end
        }

canSeekBeginning():
    Wraps bt_message_iterator_can_seek_beginning().

seekBeginning():
    Wraps bt_message_iterator_seek_beginning().

canSeekNsFromOrigin():
    Wraps bt_message_iterator_can_seek_ns_from_origin().

seekNsFromOrigin():
    Wraps bt_message_iterator_seek_ns_from_origin().

canSeekForward():
    Wraps bt_message_iterator_can_seek_forward().

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ifd16546ad56b237d62491fd17dd6cb180ca3edba
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11194
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2: add `bt2::Const*Component` and `bt2::Const*Port`
Philippe Proulx [Wed, 1 Nov 2023 03:03:36 +0000 (23:03 -0400)] 
cpp-common/bt2: add `bt2::Const*Component` and `bt2::Const*Port`

This patch adds the `bt2::ConstComponent`, `bt2::ConstSourceComponent`,
`bt2::ConstFilterComponent`, and `bt2::ConstSinkComponent` wrapper
classes to wrap resp. `const bt_component`, `const bt_component_source`,
`const bt_component_filter`, and `const bt_component_sink`.

I didn't find a clever way to make `bt2::ConstSourceComponent`, for
example, inherit `bt2::ConstComponent`, because they manage different
library types (`const bt_component` vs. `const bt_component_source`). I
think this is a first in `src/cpp-common/bt2` for this situation: for
example, all value objects have the `bt_value` type, all field classes
have the `bt_field_class` type, and so on, making it possible to also
express the intended relations in C++.

I though about using a library upcasting function (for example,
bt_component_source_as_component_const()) to call the parent
constructor, but then there would be no "safe" way to return to
`const bt_component_source` because there aren't downcasting functions
(`reinterpret_cast<const bt_component_source *>()` would probably work,
but isn't super legit).

Therefore there's no relation between specific component wrapper classes
and `bt2::ConstComponent`. That being said, `bt2::ConstComponent` is
courteous and offers all the copy constructors and assignment operators
to copy and assign from specific component wrappers. For example:

    void f(const bt2::ConstComponent myComp)
    {
        // ...
    }

    void g(const bt_component_source * const libCompPtr)
    {
        const bt2::ConstComponent comp {libCompPtr};
        const bt2::ConstSourceComponent srcComp {libCompPtr};

        f(srcComp);
    }

However, the shared versions aren't directly compatible because the
reference count functions aren't the same (an obvious requirement to
construct a shared object from another one). To make things easier, each
specific component wrapper class has the sharedComponent() method which
returns a `bt2::ConstComponent::Shared` instance:

    void f(bt2::ConstComponent::Shared myComp)
    {
        // ...
    }

    void g(bt2::ConstSourceComponent::Shared srcComp)
    {
        f(srcComp->sharedComponent());
    }

Each specific component wrapper class kindly offers the name() and
loggingLevel() methods to avoid going to a `bt2::ConstComponent` by
hand.

Each specific component wrapper class has its inputPorts() and/or
outputPorts() method(s). Those return an instance of
`bt2::ConstComponentPorts` with the appropriate template arguments to
make things just work. `bt2::ConstComponentPorts` has the length()
method and various `[]` operators to access ports:

    mySrc.outputPorts()[3]

    myFlt.inputPorts()["file2"]

The returned object is one of `bt2::ConstInputPort` or
`bt2::ConstOutputPort` wrapping resp. `const bt_port_input` and
`const bt_port_output`.

Component and port wrapper classes are part of the same header,
`component-port.hpp`, for the same reason all high-level trace IR
wrapper classes are in the single `trace-ir.hpp`: a port object may
return a component (component() method) and a component object may
return a port (inputPorts()/outputPorts() methods), making it difficult,
if not impossible, to use two different headers because we don't know
the inclusion order of the user.

Because the libbabeltrace2 API always deals with specific (input or
output) port objects, there's no `const bt_port` wrapper class: you
always use `bt2::ConstInputPort` or `bt2::ConstOutputPort`. In other
words, no API function returns a general `const bt_port` pointer.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I1324a59c8291cb83a432eeb7477c6b3e353924f5
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11184
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
4 months agocpp-common/bt2: add `bt2::ConstMessageArray`
Philippe Proulx [Thu, 26 Oct 2023 14:43:05 +0000 (10:43 -0400)] 
cpp-common/bt2: add `bt2::ConstMessageArray`

A `bt2::ConstMessageArray` instance wraps an array of `const` messages
(`bt_message_array_const`) which is used in everything related to "next"
message iterator methods.

There are two ways to build such a wrapper:

1. Use bt2::ConstMessageArray::wrapExisting() to wrap an existing
   library message array with a given length:

       bt_message_array_const libMsgs;
       uint64_t count;

       if (bt_message_iterator_next(myIter, &libMsgs, &count) !=
               BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
           // Handle special status
       }

       const auto msgs = bt2::ConstMessageArray::wrapExisting(libMsgs,
                                                              count);

       ⭐

       if (!msgs[0].isEvent()) {
           // ...
       }

       for (const auto msg : msgs) {
           // Handle `msg`
       }

   At ⭐, `msgs` is now the owner of the messages of `libMsgs`,
   meaning:

   a) Its destructor puts all the contained message references.

   b) Do NOT put the message references of `libMsgs` yourself with
      bt_message_put_ref().

   c) Do NOT make another `ConstMessageArray` wrap `libMsgs`.

2. Use bt2::ConstMessageArray::wrapEmpty() to wrap an empty library
   message array with an explicit capacity:

       bt_message_iterator_class_next_method_status myNext(
               bt_self_message_iterator * const libSelfMsgIter,
               const bt_message_array_const libMsgs,
               const uint64_t capacity, uint64_t * const count)
       {
           auto msgs = bt2::ConstMessageArray::wrapEmpty(libMsgs,
                                                         capacity);

           // Create messages and append with `msgs.append(...)`

           *count = msgs.release();
           return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
       }

   If there's any thrown exception during the message appending part,
   the destructor of `msgs` will put the references of its messages.

   Then when you call ConstMessageArray::release(), you become the owner
   of the library array again. The returned value is its length before
   the release.

As you can see, this wrapper is a bit fragile and really on the "you
know what you're doing" territory, but I believe it can be useful and
safer when used as intended.

Conceptually, only one `ConstMessageArray` at a time may manage the
underlying library array. Therefore, copy operations are disabled, but
move operations are implemented.

Because the copy constructor isn't implemented, we can't use
`CommonIterator`, so `ConstMessageArrayIterator` is a dedicated,
lightweight version of the latter.

Get a message array length and capacity with the length() and
capacity() methods.

Check if a message array is empty or full with the isEmpty() and
isFull() methods.

Append a message to a message array with the append() method.

Release a message array with the release() method.

Borrow a message from a message array with the `[]` operator.

Iterate a message array with the begin() and end() methods.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iac8bea4195836f6f0d157eb08bec27b3d24ca506
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11146
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

4 months agocpp-common/bt2/shared-object.hpp: add newlines around declaration
Philippe Proulx [Mon, 13 Nov 2023 21:19:15 +0000 (16:19 -0500)] 
cpp-common/bt2/shared-object.hpp: add newlines around declaration

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I2b8057771965feda011f3112effabd31023fd0dc
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11372
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: do not use `bpstd::string_view`
Philippe Proulx [Mon, 13 Nov 2023 20:43:31 +0000 (15:43 -0500)] 
cpp-common/bt2: do not use `bpstd::string_view`

Using `bpstd::string_view` in those contexts was a mistake: building a
`bpstd::string_view` object from a null-terminated string requires to
get its length (think strlen()), and an optional string view requires to
return `nonstd::optional<bpstd::string_view>` which means checking the
library pointer first.

All this means it's not a zero cost, and zero cost is what those C++
bindings aim to be.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib51fb9e326c4b4c450a381631b9db69f6cd0530b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11370
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2/field-class.hpp: coalesce `internal` namespace
Philippe Proulx [Mon, 13 Nov 2023 18:09:05 +0000 (13:09 -0500)] 
cpp-common/bt2/field-class.hpp: coalesce `internal` namespace

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I8c23478a99da52fc19dddf22547f6e9ed0d0466f
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11369
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: remove useless copy operations
Philippe Proulx [Wed, 8 Nov 2023 20:57:04 +0000 (15:57 -0500)] 
cpp-common/bt2: remove useless copy operations

This patch removes useless copy constructors and assignment operators
from many wrapper classes in `cpp-common/bt2` where the compiler
generates some anyway.

Also, return by value (like everywhere else) from pseudo copy
operations.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I9e5b5e956905681c3a834122b3f0ed8311b5a6ae
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11302
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2/borrowed-object.hpp: use BT_ASSERT_DBG()
Philippe Proulx [Wed, 8 Nov 2023 19:53:57 +0000 (14:53 -0500)] 
cpp-common/bt2/borrowed-object.hpp: use BT_ASSERT_DBG()

bt2::BorrowedObject::BorrowedObject() obviously deserves a
BT_ASSERT_DBG() here because it's a common constructor for all wrapper
classes.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I05879bf2e6993fe836b57645e1451247008ac2ea
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11301
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2/field.hpp: use raw value proxy for scalar classes
Philippe Proulx [Mon, 13 Nov 2023 20:32:19 +0000 (15:32 -0500)] 
cpp-common/bt2/field.hpp: use raw value proxy for scalar classes

This patch makes field wrappers behave like value wrappers regarding
raw value access.

Now, the `*` operator of boolean, integer, floating point number, and
string fields return a raw value proxy to access the raw value:

    void f(const bt2::UnsignedIntegerField val) {
        *val = 23;
        std::cout << *val << std::endl;
    }

I didn't do it for `bt2::CommonBitArrayField` which is a scalar field,
but it's still an array, so it felt weird. Instead, use valueAsInteger()
or bitValue() depending on your use case.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I3bbcdc79a6dea781f720b7da8e18aea285d792b5
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11371
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: use raw value proxy for scalar value classes
Philippe Proulx [Sat, 11 Nov 2023 16:46:36 +0000 (11:46 -0500)] 
cpp-common/bt2: use raw value proxy for scalar value classes

This patch adds the `bt2::RawValueProxy` class of which an instance
contains a wrapper object `_mObj` and:

* Its `=` operator calls `_mObj.value()` to assign a value.
* Its raw value type operator returns what `_mObj.value()` returns.

Then, in scalar value wrapper classes of `value.hpp`:

* Remove the `=` operator which assigns a raw value.
* Add a value() method to assign a raw value.
* Add a `*` operator to return a raw value proxy containing this.

The goal of this patch is to avoid using the `=` operator on some value
object to both wrap another library object (pseudo copy assignment
operator) and assign a raw value. Now, to assign a raw value, you need
to "dereference" the wrapper object:

    void f(const bt2::UnsignedIntegerValue val) {
        *val = 23;
        std::cout << *val << std::endl;
    }

There's also `bt2::RawStringValueProxy` which adds the `=` operator to
`bt2::RawValueProxy` to assign a `const std::string&`.
`bt2::CommonStringValue` uses it.

Additionally, remove all the `=` operators to assign a raw value on
`bt2::CommonValue` and create a "master" raw value proxy class
(`CommonValueRawValueProxy`) to have all the `=` operators and raw value
type operators:

    void f(const bt2::Value val) {
        // We know it's a boolean value
        *val = true;
    }

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ia8559de36baba3021169b3d097ec83b914407f63
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11362
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2/value.hpp: add proxy methods to `bt2::CommonValue`
Philippe Proulx [Sat, 4 Nov 2023 20:43:11 +0000 (16:43 -0400)] 
cpp-common/bt2/value.hpp: add proxy methods to `bt2::CommonValue`

Those new methods on `bt2::CommonValue` call the corresponding method
while "converting" the current object to another type for the user.

For example:

    void f(const bt2::Value val)
    {
        // We know it's a string value
        val = "mireille";
    }

This is just sugar for common value operations to avoid the redundancy
of calling as*().

The as*() methods already have BT_ASSERT_DBG() lines to make sure that
the conversion is valid.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I475bda3754a984cbd2eca98f5caa14c765ece956
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11241
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: remove useless `_ThisXyz` type aliases
Philippe Proulx [Sat, 4 Nov 2023 20:04:46 +0000 (16:04 -0400)] 
cpp-common/bt2: remove useless `_ThisXyz` type aliases

Name is injected so those private `_ThisXyz` type aliases are useless.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I4313729f63f4fe74622517d19dd986bd3393766b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11239
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2/value.hpp: move static assertion (wrong place)
Philippe Proulx [Sat, 4 Nov 2023 19:41:37 +0000 (15:41 -0400)] 
cpp-common/bt2/value.hpp: move static assertion (wrong place)

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I3384fdcf4f570a2f15062bf64af3ac27d0caee72
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11238
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: rework `bt2::CommonIterator` (now `bt2::BorrowedObjectIterator`)
Simon Marchi [Mon, 11 Dec 2023 20:03:50 +0000 (15:03 -0500)] 
cpp-common/bt2: rework `bt2::CommonIterator` (now `bt2::BorrowedObjectIterator`)

We don't need all the STL stuff (and constraints) currently, therefore
this iterator class may be simpler, making operator*() call
ContainerT::operator[]() directly instead of keeping a temporary wrapper
object and having a conditional for each iteration increment.

Making operator->() return a proxy because the iterator instance has no
persistent wrapper instance.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I1bdb254a37533b8524450b22aa7f25a4d8025c2c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11236
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: add `bt2::BorrowedObjectProxy`
Philippe Proulx [Tue, 5 Dec 2023 14:46:25 +0000 (09:46 -0500)] 
cpp-common/bt2: add `bt2::BorrowedObjectProxy`

When bt2::Something::operator->() would need to return the address of
some `ObjT` instance, but none is available (because it only keeps the
libbabeltrace2 object pointer, not a built wrapper), then it can use
this new `bt2::BorrowedObjectProxy`.

This new class template accepts a library pointer at construction time,
constructs an internal `ObjT` instance, and its own operator->() method
returns the address of the latter.

Example:

    class Something
    {
        // ...

    public:
        BorrowedObjectProxy<SomeObject> operator->() const noexcept
        {
            return BorrowedObjectProxy<SomeObject> {_mLibObjPtr};
        }

    private:
        const bt_some_object *_mLibObjPtr;
    };

Then, with a `Something` instance `something`:

    something->methodOfSomeObject(...);

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I67d63591fb6593080a15e2ab6804a53bd2d2aba8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11487
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: add and use `bt2::internal::Dep*` type alias templates
Philippe Proulx [Fri, 3 Nov 2023 18:48:52 +0000 (14:48 -0400)] 
cpp-common/bt2: add and use `bt2::internal::Dep*` type alias templates

Those new templates take care of using `std::conditional` instead of
having to use it at each site. The template parameters of
`bt2::internal::DepType` are:

1. What to check for constness.
2. The effective type if `LibObjT` is NOT `const`.
3. The effective type if `LibObjT` is `const`.

Other `bt2::internal::Dep*` reuse `DepType` with specific types for
parameters 2 and 3 for common uses.

For example:

    using UserAttributes =
        typename std::conditional<std::is_const<LibObjT>::value,
                                  ConstMapValue,
                                  MapValue>::type;

gets replaced by:

    using UserAttributes = internal::DepUserAttrs<LibObjT>;

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I434b9ae82d170c6fe8359488e78c0e3e34a064cd
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11234
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: use more specific static assertion messages
Philippe Proulx [Sat, 11 Nov 2023 03:36:09 +0000 (22:36 -0500)] 
cpp-common/bt2: use more specific static assertion messages

The current

    `LibObjT` must NOT be `const`.

static assertion message is pretty generic. For a typical
`bt2::ConstXyz` user, `LibObjT` actually means nothing, because she's
not using `bt2::CommonXyz` directly, only an alias.

Make the message specific, for example:

    Not available with `bt2::ConstBoolValue`.

For example, with:

    void lel(bt2::ConstBoolValue val)
    {
        val = false;
    }

I get:

    cpp-common/bt2/value.hpp: In instantiation of ‘bt2::CommonBoolValue<LibObjT>& bt2::CommonBoolValue<LibObjT>::operator=(Value) const [with LibObjT = const bt_value; Value = bool]’:
    test.cpp:61:11:   required from here
    cpp-common/bt2/value.hpp:349:48: error: static assertion failed: Not available with `bt2::ConstBoolValue`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I4e96d4dcd1cc345d8b393aea6bb78fbc0000e9ba
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11361
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: add asConst() methods
Philippe Proulx [Fri, 3 Nov 2023 17:26:55 +0000 (13:26 -0400)] 
cpp-common/bt2: add asConst() methods

Add many asConst() methods to get the `bt2::ConstXyz` version of a
`bt2::Xyz` wrapper instead of using the constructor directly.

Just some sugar.

For example:

    void f(const bt2::Value val) {
        {
            const auto cVal = val.asConst();

            // Operate on `cVal`
        }
    }

I only added those methods where it was easy to do so.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iabd9d6266fe75f33f2008c2320444624b842d9d2
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11232
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: use the "length" term everywhere instead of "size"
Philippe Proulx [Fri, 3 Nov 2023 16:03:02 +0000 (12:03 -0400)] 
cpp-common/bt2: use the "length" term everywhere instead of "size"

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I25ce7709679d016dff6b23aa323a0ca5ec738190
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11231
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: remove useless friend classes to access libObjPtr()
Philippe Proulx [Fri, 3 Nov 2023 15:56:49 +0000 (11:56 -0400)] 
cpp-common/bt2: remove useless friend classes to access libObjPtr()

Mr. Deslauriers made bt2::BorrowedObj::libObjPtr() public in 341a67c45
("cpp-common: Expose BorrowedObj::libObjPtr() as public method").

Therefore, all those friend classes aren't needed anymore.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie37bfa78e4cd29dd8e08af135ff6a0e597ebd0ae
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11230
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: add `noexcept` to user attribute setters
Philippe Proulx [Fri, 3 Nov 2023 15:50:52 +0000 (11:50 -0400)] 
cpp-common/bt2: add `noexcept` to user attribute setters

The bt_*_set_user_attributes() functions never fail (they only increment
the reference count of the received value), therefore the user attribute
setters never throw.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I5b9c05e6efd7b42965b9e075438b518df1a5da90
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11229
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: systematize the `const` method situation
Philippe Proulx [Fri, 3 Nov 2023 15:32:49 +0000 (11:32 -0400)] 
cpp-common/bt2: systematize the `const` method situation

This patch attempts to systematize the `const` situation regarding
methods (member functions and operators).

The current issue is that, because those wrappers are expected to be
passed and returned by copy (they only wrap a libbabeltrace2 pointer),
one cannot rely on the `const` qualifier to ensure immutability at the
library level. For example:

    void f(const bt2::ArrayValue val)
    {
        // Want to modify what `val` wraps? No problem:
        auto newVal = val;

        newVal += 23;
    }

The signature of f() here doesn't guarantee to the caller that what
`val` wraps is immutable. To achieve this, it needs to accept the
`Const` version of the wrapper:

    void f(bt2::ConstArrayValue val)
    {
        // Now there's no way to modify what `val` wraps
    }

Therefore, since using the `const` keyword is so fragile, this patch
makes `const` (almost) all the methods of wrapper classes: those methods
do not modify the wrapped pointer value, therefore they may be `const`.

This makes the wrapping system have this correspondence:

          bt_xyz *                    bt2::Xyz
          bt_xyz * const        const bt2::Xyz
    const bt_xyz *                    bt2::ConstXyz
    const bt_xyz * const        const bt2::ConstXyz

Because there were many overloads to return a `bt2::Const*` instance
when the object is `const`, this patch removes a lot of code, with the
added benefit of reducing code duplication.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie633e7e9d67a8eb0138800ad8ae14c8d9bd0ca4c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11228
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: add missing `noexcept` to static methods
Philippe Proulx [Wed, 1 Nov 2023 20:53:54 +0000 (16:53 -0400)] 
cpp-common/bt2: add missing `noexcept` to static methods

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I3737dc68a270446e9eafee23afb24fc816ca2bbc
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11193
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: rename `common-iter.hpp` to `common-iterator.hpp`
Philippe Proulx [Fri, 27 Oct 2023 18:52:32 +0000 (14:52 -0400)] 
cpp-common/bt2: rename `common-iter.hpp` to `common-iterator.hpp`

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I4816ab3ecf2b19423f8ab0a92924e62c438df4f9
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11164
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: remove superfluous NL
Philippe Proulx [Fri, 27 Oct 2023 18:35:02 +0000 (14:35 -0400)] 
cpp-common/bt2: remove superfluous NL

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I96252e134bb355e2e1b0b43cf4f13a13b5ed4c7e
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11163
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2/common-iter.hpp: add missing NL
Philippe Proulx [Fri, 27 Oct 2023 18:33:40 +0000 (14:33 -0400)] 
cpp-common/bt2/common-iter.hpp: add missing NL

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I57b2b54a5181e1cee90cd9090d349bf058e9025b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11162
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
4 months agocpp-common/bt2: rename `bt2::SharedObj` -> `bt2::SharedObject`
Philippe Proulx [Mon, 13 Nov 2023 19:49:26 +0000 (14:49 -0500)] 
cpp-common/bt2: rename `bt2::SharedObj` -> `bt2::SharedObject`

This is public now, so use the full name without abbreviation like
everything else does.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I1328a9c195061558ca442aed6e177ca43836b99c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11368
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
This page took 0.076987 seconds and 4 git commands to generate.