From 2cc79dd444b4ef403ed730ae974c45ba887b9e85 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 12:11:50 -0700 Subject: [PATCH 01/62] build: bump ABI to 4 --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index d9f032dc..90dc5fbd 100644 --- a/meson.build +++ b/meson.build @@ -12,7 +12,7 @@ gnome = import('gnome') pkgconfig = import('pkgconfig') i18n = import('i18n') -libsysprof_api_version = 3 +libsysprof_api_version = 4 version_split = meson.project_version().split('.') datadir = get_option('datadir') datadir_for_pc_file = join_paths('${prefix}', datadir) From dbcf3180968ddc6a1c5a840a27c8622f209a3196 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 30 Jun 2020 16:18:51 +0100 Subject: [PATCH 02/62] examples: Add missing dependency on glib-2.0 It has previously been implicitly pulled in by libsysprof-capture, but that will change in future. Signed-off-by: Philip Withnall Helps: #40 --- examples/app.c | 1 + examples/meson.build | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/app.c b/examples/app.c index c2544cb1..abb45aa6 100644 --- a/examples/app.c +++ b/examples/app.c @@ -11,6 +11,7 @@ # include #endif +#include #include #include #include diff --git a/examples/meson.build b/examples/meson.build index 124ac241..71a036a4 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -1,3 +1,3 @@ app = executable('example-app', 'app.c', - dependencies: [libsysprof_capture_dep], + dependencies: [dependency('glib-2.0'), libsysprof_capture_dep], ) From 30e56856553be00cf3559244f31b7cf934cbfb88 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 30 Jun 2020 16:20:21 +0100 Subject: [PATCH 03/62] tools: Add missing dependency on glib-2.0 It has previously been implicitly pulled in by libsysprof-capture, but that will change in future. Signed-off-by: Philip Withnall Helps: #40 --- src/tools/meson.build | 1 + src/tools/sysprof-cat.c | 1 + src/tools/sysprof-dump.c | 1 + 3 files changed, 3 insertions(+) diff --git a/src/tools/meson.build b/src/tools/meson.build index 670052a7..bb6603d1 100644 --- a/src/tools/meson.build +++ b/src/tools/meson.build @@ -1,4 +1,5 @@ tools_deps = [ + dependency('glib-2.0'), libsysprof_capture_dep, ] diff --git a/src/tools/sysprof-cat.c b/src/tools/sysprof-cat.c index b4187bf0..5d53435a 100644 --- a/src/tools/sysprof-cat.c +++ b/src/tools/sysprof-cat.c @@ -23,6 +23,7 @@ #include "config.h" #include +#include #include #include #include diff --git a/src/tools/sysprof-dump.c b/src/tools/sysprof-dump.c index 5491f3a9..8b996249 100644 --- a/src/tools/sysprof-dump.c +++ b/src/tools/sysprof-dump.c @@ -20,6 +20,7 @@ #include "config.h" +#include #include #include #include From 3eb0f3e964177e20452f98ddff965aac91f0dc82 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 30 Jun 2020 16:21:38 +0100 Subject: [PATCH 04/62] sysprof-dump: Move error handling up to avoid a NULL pointer dereference `reader` can be `NULL`, but the `NULL` check was done after potentially dereferencing it. Signed-off-by: Philip Withnall Helps: #40 --- src/tools/sysprof-dump.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/tools/sysprof-dump.c b/src/tools/sysprof-dump.c index 8b996249..24394909 100644 --- a/src/tools/sysprof-dump.c +++ b/src/tools/sysprof-dump.c @@ -56,7 +56,11 @@ main (gint argc, return EXIT_FAILURE; } - reader = sysprof_capture_reader_new (argv[1], &error); + if ((reader = sysprof_capture_reader_new (argv[1], &error)) == NULL) + { + g_printerr ("%s\n", error->message); + return EXIT_FAILURE; + } if (list_files) { @@ -75,12 +79,6 @@ main (gint argc, #define SET_CTR_TYPE(i,t) g_hash_table_insert(ctrtypes, GINT_TO_POINTER(i), GINT_TO_POINTER(t)) #define GET_CTR_TYPE(i) GPOINTER_TO_INT(g_hash_table_lookup(ctrtypes, GINT_TO_POINTER(i))) - if (reader == NULL) - { - g_printerr ("%s\n", error->message); - return EXIT_FAILURE; - } - begin_time = sysprof_capture_reader_get_start_time (reader); end_time = sysprof_capture_reader_get_end_time (reader); From d1a8dc40f5264067351493b98cd1d37803b43c09 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 30 Jun 2020 16:23:50 +0100 Subject: [PATCH 05/62] tests: Add missing dependency on glib-2.0 It has previously been implicitly pulled in by libsysprof-capture, but that will change in future. Signed-off-by: Philip Withnall Helps: #40 --- src/tests/cross-thread-frees.c | 1 + src/tests/find-temp-allocs.c | 1 + src/tests/meson.build | 13 +++++++++---- src/tests/show-page-usage.c | 1 + src/tests/test-addr-decode.c | 1 + src/tests/test-addr-map.c | 1 + src/tests/test-capture-cursor.c | 1 + src/tests/test-capture.c | 1 + src/tests/test-mapped-ring-buffer.c | 1 + src/tests/test-resolvers.c | 1 + 10 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/tests/cross-thread-frees.c b/src/tests/cross-thread-frees.c index cd973b79..f4ec8921 100644 --- a/src/tests/cross-thread-frees.c +++ b/src/tests/cross-thread-frees.c @@ -20,6 +20,7 @@ #include "config.h" +#include #include #include #include diff --git a/src/tests/find-temp-allocs.c b/src/tests/find-temp-allocs.c index 4a8e300d..3df3c2c9 100644 --- a/src/tests/find-temp-allocs.c +++ b/src/tests/find-temp-allocs.c @@ -20,6 +20,7 @@ #include "config.h" +#include #include static struct { diff --git a/src/tests/meson.build b/src/tests/meson.build index f2825f60..1a450aef 100644 --- a/src/tests/meson.build +++ b/src/tests/meson.build @@ -12,24 +12,29 @@ test_cflags = [ '-DSYSPROF_COMPILATION=1', ] +test_capture_deps = [ + dependency('glib-2.0'), + libsysprof_capture_dep, +] + test_capture = executable('test-capture', 'test-capture.c', c_args: test_cflags, - dependencies: [libsysprof_capture_dep], + dependencies: test_capture_deps, ) test_capture_cursor = executable('test-capture-cursor', 'test-capture-cursor.c', c_args: test_cflags, - dependencies: [libsysprof_capture_dep], + dependencies: test_capture_deps, ) test_mapped_ring_buffer = executable('test-mapped-ring-buffer', 'test-mapped-ring-buffer.c', c_args: test_cflags, - dependencies: [libsysprof_capture_dep], + dependencies: test_capture_deps, ) find_temp_allocs = executable('find-temp-allocs', 'find-temp-allocs.c', c_args: test_cflags, - dependencies: [libsysprof_capture_dep], + dependencies: test_capture_deps, ) test('test-capture', test_capture, env: test_env) diff --git a/src/tests/show-page-usage.c b/src/tests/show-page-usage.c index 536f7bce..0a6a1c21 100644 --- a/src/tests/show-page-usage.c +++ b/src/tests/show-page-usage.c @@ -21,6 +21,7 @@ #include "config.h" #include +#include #include #include #include diff --git a/src/tests/test-addr-decode.c b/src/tests/test-addr-decode.c index 66ca209b..8757bed9 100644 --- a/src/tests/test-addr-decode.c +++ b/src/tests/test-addr-decode.c @@ -1,4 +1,5 @@ #include +#include #include #include "sysprof-platform.h" diff --git a/src/tests/test-addr-map.c b/src/tests/test-addr-map.c index 47a2d829..022e8149 100644 --- a/src/tests/test-addr-map.c +++ b/src/tests/test-addr-map.c @@ -1,4 +1,5 @@ #include +#include #include #include "sysprof-platform.h" diff --git a/src/tests/test-capture-cursor.c b/src/tests/test-capture-cursor.c index 0781fc07..dc24cba9 100644 --- a/src/tests/test-capture-cursor.c +++ b/src/tests/test-capture-cursor.c @@ -18,6 +18,7 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ +#include #include #include diff --git a/src/tests/test-capture.c b/src/tests/test-capture.c index 64d6f2cc..895fdfcb 100644 --- a/src/tests/test-capture.c +++ b/src/tests/test-capture.c @@ -21,6 +21,7 @@ #include "config.h" #include +#include #include #include #include diff --git a/src/tests/test-mapped-ring-buffer.c b/src/tests/test-mapped-ring-buffer.c index 5c6c0144..3213c7e4 100644 --- a/src/tests/test-mapped-ring-buffer.c +++ b/src/tests/test-mapped-ring-buffer.c @@ -1,5 +1,6 @@ #include "mapped-ring-buffer.h" +#include #include static gsize real_count; diff --git a/src/tests/test-resolvers.c b/src/tests/test-resolvers.c index 9021c6de..8ba0e34b 100644 --- a/src/tests/test-resolvers.c +++ b/src/tests/test-resolvers.c @@ -1,3 +1,4 @@ +#include #include static const SysprofCaptureSample * From 4b6855a2ab14d5306984f03de9bae510ed98ae2b Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 30 Jun 2020 16:24:54 +0100 Subject: [PATCH 06/62] libsysprof: Add missing preload dependencies on glib-2.0 It has previously been implicitly pulled in by libsysprof-capture, but that will change in future. Correspondingly, add some missing `glib.h` includes. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof/preload/meson.build | 1 + src/libsysprof/preload/sysprof-memory-collector.c | 1 + src/libsysprof/preload/sysprof-speedtrack-collector.c | 1 + src/libsysprof/sysprof-kernel-symbol.h | 2 ++ src/libsysprof/sysprof-map-lookaside.c | 2 ++ src/libsysprof/sysprof-map-lookaside.h | 2 ++ src/libsysprof/sysprof-source.h | 1 + 7 files changed, 10 insertions(+) diff --git a/src/libsysprof/preload/meson.build b/src/libsysprof/preload/meson.build index dd237a3a..6906ec7e 100644 --- a/src/libsysprof/preload/meson.build +++ b/src/libsysprof/preload/meson.build @@ -1,6 +1,7 @@ libdl_dep = cc.find_library('dl', required: false) preload_deps = [ + dependency('glib-2.0'), libsysprof_capture_dep, libdl_dep, ] diff --git a/src/libsysprof/preload/sysprof-memory-collector.c b/src/libsysprof/preload/sysprof-memory-collector.c index 30591c88..dd851ac9 100644 --- a/src/libsysprof/preload/sysprof-memory-collector.c +++ b/src/libsysprof/preload/sysprof-memory-collector.c @@ -23,6 +23,7 @@ #include "config.h" #include +#include #include #include #include diff --git a/src/libsysprof/preload/sysprof-speedtrack-collector.c b/src/libsysprof/preload/sysprof-speedtrack-collector.c index 6b2b08c0..223dea91 100644 --- a/src/libsysprof/preload/sysprof-speedtrack-collector.c +++ b/src/libsysprof/preload/sysprof-speedtrack-collector.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include diff --git a/src/libsysprof/sysprof-kernel-symbol.h b/src/libsysprof/sysprof-kernel-symbol.h index 7ef72f01..27c4790a 100644 --- a/src/libsysprof/sysprof-kernel-symbol.h +++ b/src/libsysprof/sysprof-kernel-symbol.h @@ -24,6 +24,8 @@ # error "Only can be included directly." #endif +#include + #include "sysprof-capture-types.h" G_BEGIN_DECLS diff --git a/src/libsysprof/sysprof-map-lookaside.c b/src/libsysprof/sysprof-map-lookaside.c index b1532e2b..277e7f66 100644 --- a/src/libsysprof/sysprof-map-lookaside.c +++ b/src/libsysprof/sysprof-map-lookaside.c @@ -20,6 +20,8 @@ #include "config.h" +#include + #include "sysprof-map-lookaside.h" struct _SysprofMapLookaside diff --git a/src/libsysprof/sysprof-map-lookaside.h b/src/libsysprof/sysprof-map-lookaside.h index e517a600..ef068562 100644 --- a/src/libsysprof/sysprof-map-lookaside.h +++ b/src/libsysprof/sysprof-map-lookaside.h @@ -20,6 +20,8 @@ #pragma once +#include + #include "sysprof-capture-types.h" G_BEGIN_DECLS diff --git a/src/libsysprof/sysprof-source.h b/src/libsysprof/sysprof-source.h index 62e2f53c..412aa765 100644 --- a/src/libsysprof/sysprof-source.h +++ b/src/libsysprof/sysprof-source.h @@ -24,6 +24,7 @@ # error "Only can be included directly." #endif +#include #include #include From 16bf945d34befcf4830a856b35b5315cb504b6af Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 13:11:39 +0100 Subject: [PATCH 07/62] libsysprof-capture: Drop unnecessary #include Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-collector.c | 1 - src/libsysprof-capture/sysprof-collector.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 3c96065f..064586de 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -73,7 +73,6 @@ #include "mapped-ring-buffer.h" -#include "sysprof-capture-writer.h" #include "sysprof-collector.h" #define MAX_UNWIND_DEPTH 128 diff --git a/src/libsysprof-capture/sysprof-collector.h b/src/libsysprof-capture/sysprof-collector.h index 68ac6882..f234bc2f 100644 --- a/src/libsysprof-capture/sysprof-collector.h +++ b/src/libsysprof-capture/sysprof-collector.h @@ -57,7 +57,6 @@ #pragma once #include "sysprof-capture-types.h" -#include "sysprof-capture-writer.h" G_BEGIN_DECLS From d2047fb5570429266b540fcef697b01b57b9838b Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 30 Jun 2020 16:30:14 +0100 Subject: [PATCH 08/62] libsysprof-capture: Move autocleanup definitions to libsysprof In preparation for dropping the GLib dependency from libsysprof-capture, move the autocleanup definitions up to libsysprof. Add a new header for them. This is slightly awkward in the tools, which depend on libsysprof-capture but not libsysprof. Rather than make them depend on libsysprof (which might be disabled at configure time), include the `sysprof-capture-autocleanups.h` file between source directories. `SYSPROF_COMPILATION` needs to be defined for this to work. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-condition.h | 2 - .../sysprof-capture-cursor.h | 2 - .../sysprof-capture-reader.h | 1 - .../sysprof-capture-writer.h | 2 - src/libsysprof/meson.build | 1 + src/libsysprof/sysprof-capture-autocleanups.h | 41 +++++++++++++++++++ src/libsysprof/sysprof-local-profiler.c | 1 + src/libsysprof/sysprof-memprof-profile.c | 1 + src/libsysprof/sysprof-tracefd-source.c | 1 + src/libsysprof/sysprof.h | 1 + src/tools/list-threads.c | 2 + src/tools/meson.build | 7 ++++ src/tools/sysprof-cat.c | 1 + 13 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 src/libsysprof/sysprof-capture-autocleanups.h diff --git a/src/libsysprof-capture/sysprof-capture-condition.h b/src/libsysprof-capture/sysprof-capture-condition.h index 077b8ce3..33c356c7 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.h +++ b/src/libsysprof-capture/sysprof-capture-condition.h @@ -91,6 +91,4 @@ SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_condition_match (const SysprofCaptureCondition *self, const SysprofCaptureFrame *frame); -G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureCondition, sysprof_capture_condition_unref) - G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-cursor.h b/src/libsysprof-capture/sysprof-capture-cursor.h index 19d4fb49..13ef791a 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.h +++ b/src/libsysprof-capture/sysprof-capture-cursor.h @@ -97,6 +97,4 @@ SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_cursor_add_condition (SysprofCaptureCursor *self, SysprofCaptureCondition *condition); -G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureCursor, sysprof_capture_cursor_unref) - G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index 3c58524c..9c732b0a 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -147,6 +147,5 @@ gboolean sysprof_capture_reader_read_file_fd ( const gchar *path, gint fd); -G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureReader, sysprof_capture_reader_unref) G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index 6e490253..409e88b4 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -249,6 +249,4 @@ gboolean _sysprof_capture_writer_set_time_range (Sy gint64 end_time) G_GNUC_INTERNAL; -G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureWriter, sysprof_capture_writer_unref) - G_END_DECLS diff --git a/src/libsysprof/meson.build b/src/libsysprof/meson.build index 6611a0dc..e2df0b8c 100644 --- a/src/libsysprof/meson.build +++ b/src/libsysprof/meson.build @@ -37,6 +37,7 @@ libsysprof_public_sources = [ libsysprof_public_headers = [ 'sysprof-battery-source.h', 'sysprof-callgraph-profile.h', + 'sysprof-capture-autocleanups.h', 'sysprof-capture-gobject.h', 'sysprof-capture-symbol-resolver.h', 'sysprof-control-source.h', diff --git a/src/libsysprof/sysprof-capture-autocleanups.h b/src/libsysprof/sysprof-capture-autocleanups.h new file mode 100644 index 00000000..d640b7ee --- /dev/null +++ b/src/libsysprof/sysprof-capture-autocleanups.h @@ -0,0 +1,41 @@ +/* sysprof-capture-gobject.h + * + * Copyright 2020 Endless Mobile, Inc. + * + * Author: + * - Philip Withnall + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#if !defined (SYSPROF_INSIDE) && !defined (SYSPROF_COMPILATION) +# error "Only can be included directly." +#endif + +#include + +#include "sysprof-capture.h" + +G_BEGIN_DECLS + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureCondition, sysprof_capture_condition_unref) +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureCursor, sysprof_capture_cursor_unref) +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureReader, sysprof_capture_reader_unref) +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureWriter, sysprof_capture_writer_unref) + +G_END_DECLS diff --git a/src/libsysprof/sysprof-local-profiler.c b/src/libsysprof/sysprof-local-profiler.c index 37f7e9e5..f993a8e1 100644 --- a/src/libsysprof/sysprof-local-profiler.c +++ b/src/libsysprof/sysprof-local-profiler.c @@ -34,6 +34,7 @@ #include "sysprof-local-profiler.h" #include "sysprof-platform.h" +#include "sysprof-capture-autocleanups.h" #include "sysprof-control-source.h" #include "sysprof-gjs-source.h" #include "sysprof-hostinfo-source.h" diff --git a/src/libsysprof/sysprof-memprof-profile.c b/src/libsysprof/sysprof-memprof-profile.c index a8f5c487..dca1cca1 100644 --- a/src/libsysprof/sysprof-memprof-profile.c +++ b/src/libsysprof/sysprof-memprof-profile.c @@ -24,6 +24,7 @@ #include +#include "sysprof-capture-autocleanups.h" #include "sysprof-capture-symbol-resolver.h" #include "sysprof-elf-symbol-resolver.h" #include "sysprof-kernel-symbol-resolver.h" diff --git a/src/libsysprof/sysprof-tracefd-source.c b/src/libsysprof/sysprof-tracefd-source.c index af5e90e2..a59cd1d0 100644 --- a/src/libsysprof/sysprof-tracefd-source.c +++ b/src/libsysprof/sysprof-tracefd-source.c @@ -25,6 +25,7 @@ #include #include +#include "sysprof-capture-autocleanups.h" #include "sysprof-platform.h" #include "sysprof-tracefd-source.h" diff --git a/src/libsysprof/sysprof.h b/src/libsysprof/sysprof.h index c682f2f9..c9164f72 100644 --- a/src/libsysprof/sysprof.h +++ b/src/libsysprof/sysprof.h @@ -26,6 +26,7 @@ G_BEGIN_DECLS # include "sysprof-battery-source.h" # include "sysprof-callgraph-profile.h" +# include "sysprof-capture-autocleanups.h" # include "sysprof-capture-gobject.h" # include "sysprof-capture-symbol-resolver.h" # include "sysprof-control-source.h" diff --git a/src/tools/list-threads.c b/src/tools/list-threads.c index 42bfc1db..5016c7bd 100644 --- a/src/tools/list-threads.c +++ b/src/tools/list-threads.c @@ -24,6 +24,8 @@ #include #include +#include "../libsysprof/sysprof-capture-autocleanups.h" + static gboolean foreach_cb (const SysprofCaptureFrame *frame, gpointer user_data) diff --git a/src/tools/meson.build b/src/tools/meson.build index bb6603d1..7cd69279 100644 --- a/src/tools/meson.build +++ b/src/tools/meson.build @@ -3,10 +3,13 @@ tools_deps = [ libsysprof_capture_dep, ] +tools_cflags = [ '-DSYSPROF_COMPILATION '] + if get_option('libsysprof') and host_machine.system() == 'linux' polkit_agent_dep = dependency('polkit-agent-1') sysprof_cli = executable('sysprof-cli', 'sysprof-cli.c', dependencies: tools_deps + [libsysprof_dep, polkit_dep, polkit_agent_dep], + c_args: tools_cflags, install_dir: get_option('bindir'), install: true, ) @@ -14,21 +17,25 @@ endif sysprof_cat = executable('sysprof-cat', 'sysprof-cat.c', dependencies: tools_deps, + c_args: tools_cflags, install: false, ) sysprof_dump = executable('sysprof-dump', 'sysprof-dump.c', dependencies: tools_deps, + c_args: tools_cflags, install: false, ) sysprof_profiler_ctl = executable('sysprof-profiler-ctl', [ 'sysprof-profiler-ctl.c', ipc_profiler_src ], dependencies: [ tools_deps, gio_unix_dep ], + c_args: tools_cflags, install: false, ) list_threads = executable('list-threads', ['list-threads.c'], dependencies: [ tools_deps ], + c_args: tools_cflags, install: false, ) diff --git a/src/tools/sysprof-cat.c b/src/tools/sysprof-cat.c index 5d53435a..eeca1399 100644 --- a/src/tools/sysprof-cat.c +++ b/src/tools/sysprof-cat.c @@ -29,6 +29,7 @@ #include #include +#include "../libsysprof/sysprof-capture-autocleanups.h" #include "sysprof-capture-util-private.h" gint From b449baa205e7ab7b06276bc83b1c759789fc607e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 30 Jun 2020 16:43:48 +0100 Subject: [PATCH 09/62] libsysprof-capture: Move MappedRingBufferSource to libsysprof As preparation for dropping the GLib dependency from libsysprof-capture, move the `GSource` which links a `MappedRingBuffer` to a `GMainContext` from libsysprof-capture to libsysprof. This requires adding one new piece of API to libsysprof-capture to check whether the `MappedRingBuffer` is empty. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 114 +++--------------- src/libsysprof-capture/mapped-ring-buffer.h | 11 +- src/libsysprof/mapped-ring-buffer-source.c | 121 ++++++++++++++++++++ src/libsysprof/mapped-ring-buffer-source.h | 47 ++++++++ src/libsysprof/meson.build | 1 + src/libsysprof/sysprof-control-source.c | 1 + 6 files changed, 189 insertions(+), 106 deletions(-) create mode 100644 src/libsysprof/mapped-ring-buffer-source.c create mode 100644 src/libsysprof/mapped-ring-buffer-source.h diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index 656845c2..ebc3ee93 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -530,107 +530,29 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, return TRUE; } -typedef struct _MappedRingSource +/** + * mapped_ring_buffer_is_empty: + * @self: a #MappedRingBuffer + * + * Checks whether the ring buffer is currently empty. + * + * This should only be called by a reader created with + * mapped_ring_buffer_new_reader(). + * + * Returns: %TRUE if the buffer is empty, %FALSE otherwise + */ +gboolean +mapped_ring_buffer_is_empty (MappedRingBuffer *self) { - GSource source; - MappedRingBuffer *self; -} MappedRingSource; - -static gboolean -mapped_ring_source_dispatch (GSource *source, - GSourceFunc callback, - gpointer user_data) -{ - MappedRingSource *real_source = (MappedRingSource *)source; - - g_assert (source != NULL); - - return mapped_ring_buffer_drain (real_source->self, - (MappedRingBufferCallback)callback, - user_data); -} - -static void -mapped_ring_source_finalize (GSource *source) -{ - MappedRingSource *real_source = (MappedRingSource *)source; - - if (real_source != NULL) - g_clear_pointer (&real_source->self, mapped_ring_buffer_unref); -} - -static gboolean -mapped_ring_source_check (GSource *source) -{ - MappedRingSource *real_source = (MappedRingSource *)source; MappedRingHeader *header; + guint32 headpos, tailpos; - g_assert (real_source != NULL); - g_assert (real_source->self != NULL); + header = get_header (self); - header = get_header (real_source->self); + headpos = g_atomic_int_get (&header->head); + tailpos = g_atomic_int_get (&header->tail); - if (g_atomic_int_get (&header->head) != g_atomic_int_get (&header->tail)) - return TRUE; - - return FALSE; -} - -static gboolean -mapped_ring_source_prepare (GSource *source, - gint *timeout_) -{ - MappedRingSource *real_source = (MappedRingSource *)source; - MappedRingHeader *header; - - g_assert (real_source != NULL); - g_assert (real_source->self != NULL); - - header = get_header (real_source->self); - - if (g_atomic_int_get (&header->head) != g_atomic_int_get (&header->tail)) - return TRUE; - - *timeout_ = 5; - - return FALSE; -} - -static GSourceFuncs mapped_ring_source_funcs = { - .prepare = mapped_ring_source_prepare, - .check = mapped_ring_source_check, - .dispatch = mapped_ring_source_dispatch, - .finalize = mapped_ring_source_finalize, -}; - -guint -mapped_ring_buffer_create_source_full (MappedRingBuffer *self, - MappedRingBufferCallback source_func, - gpointer user_data, - GDestroyNotify destroy) -{ - MappedRingSource *source; - guint ret; - - g_return_val_if_fail (self != NULL, 0); - g_return_val_if_fail (source_func != NULL, 0); - - source = (MappedRingSource *)g_source_new (&mapped_ring_source_funcs, sizeof (MappedRingSource)); - source->self = mapped_ring_buffer_ref (self); - g_source_set_callback ((GSource *)source, (GSourceFunc)source_func, user_data, destroy); - g_source_set_name ((GSource *)source, "MappedRingSource"); - ret = g_source_attach ((GSource *)source, g_main_context_default ()); - g_source_unref ((GSource *)source); - - return ret; -} - -guint -mapped_ring_buffer_create_source (MappedRingBuffer *self, - MappedRingBufferCallback source_func, - gpointer user_data) -{ - return mapped_ring_buffer_create_source_full (self, source_func, user_data, NULL); + return headpos == tailpos; } /** diff --git a/src/libsysprof-capture/mapped-ring-buffer.h b/src/libsysprof-capture/mapped-ring-buffer.h index 4eee6953..19740e17 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.h +++ b/src/libsysprof-capture/mapped-ring-buffer.h @@ -78,15 +78,6 @@ gboolean mapped_ring_buffer_drain (MappedRingBuffer MappedRingBufferCallback callback, gpointer user_data); G_GNUC_INTERNAL -guint mapped_ring_buffer_create_source (MappedRingBuffer *self, - MappedRingBufferCallback callback, - gpointer user_data); -G_GNUC_INTERNAL -guint mapped_ring_buffer_create_source_full (MappedRingBuffer *self, - MappedRingBufferCallback callback, - gpointer user_data, - GDestroyNotify destroy); - -G_DEFINE_AUTOPTR_CLEANUP_FUNC (MappedRingBuffer, mapped_ring_buffer_unref) +gboolean mapped_ring_buffer_is_empty (MappedRingBuffer *self); G_END_DECLS diff --git a/src/libsysprof/mapped-ring-buffer-source.c b/src/libsysprof/mapped-ring-buffer-source.c new file mode 100644 index 00000000..fdeb4b77 --- /dev/null +++ b/src/libsysprof/mapped-ring-buffer-source.c @@ -0,0 +1,121 @@ +/* mapped-ring-buffer-source.c + * + * Copyright 2020 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define G_LOG_DOMAIN "sysprof-mapped-ring-buffer-source" + +#include "config.h" + +#include + +#include "mapped-ring-buffer-source.h" + +typedef struct _MappedRingSource +{ + GSource source; + MappedRingBuffer *buffer; +} MappedRingSource; + +static gboolean +mapped_ring_source_dispatch (GSource *source, + GSourceFunc callback, + gpointer user_data) +{ + MappedRingSource *real_source = (MappedRingSource *)source; + + g_assert (source != NULL); + + return mapped_ring_buffer_drain (real_source->buffer, + (MappedRingBufferCallback)callback, + user_data); +} + +static void +mapped_ring_source_finalize (GSource *source) +{ + MappedRingSource *real_source = (MappedRingSource *)source; + + if (real_source != NULL) + g_clear_pointer (&real_source->buffer, mapped_ring_buffer_unref); +} + +static gboolean +mapped_ring_source_check (GSource *source) +{ + MappedRingSource *real_source = (MappedRingSource *)source; + + g_assert (real_source != NULL); + g_assert (real_source->buffer != NULL); + + return !mapped_ring_buffer_is_empty (real_source->buffer); +} + +static gboolean +mapped_ring_source_prepare (GSource *source, + gint *timeout_) +{ + MappedRingSource *real_source = (MappedRingSource *)source; + + g_assert (real_source != NULL); + g_assert (real_source->buffer != NULL); + + if (!mapped_ring_buffer_is_empty (real_source->buffer)) + return TRUE; + + *timeout_ = 5; + + return FALSE; +} + +static GSourceFuncs mapped_ring_source_funcs = { + .prepare = mapped_ring_source_prepare, + .check = mapped_ring_source_check, + .dispatch = mapped_ring_source_dispatch, + .finalize = mapped_ring_source_finalize, +}; + +guint +mapped_ring_buffer_create_source_full (MappedRingBuffer *self, + MappedRingBufferCallback source_func, + gpointer user_data, + GDestroyNotify destroy) +{ + MappedRingSource *source; + guint ret; + + g_return_val_if_fail (self != NULL, 0); + g_return_val_if_fail (source_func != NULL, 0); + + source = (MappedRingSource *)g_source_new (&mapped_ring_source_funcs, sizeof (MappedRingSource)); + source->buffer = mapped_ring_buffer_ref (self); + g_source_set_callback ((GSource *)source, (GSourceFunc)source_func, user_data, destroy); + g_source_set_name ((GSource *)source, "MappedRingSource"); + ret = g_source_attach ((GSource *)source, g_main_context_default ()); + g_source_unref ((GSource *)source); + + return ret; +} + +guint +mapped_ring_buffer_create_source (MappedRingBuffer *self, + MappedRingBufferCallback source_func, + gpointer user_data) +{ + return mapped_ring_buffer_create_source_full (self, source_func, user_data, NULL); +} diff --git a/src/libsysprof/mapped-ring-buffer-source.h b/src/libsysprof/mapped-ring-buffer-source.h new file mode 100644 index 00000000..167f4c84 --- /dev/null +++ b/src/libsysprof/mapped-ring-buffer-source.h @@ -0,0 +1,47 @@ +/* mapped-ring-buffer-source.h + * + * Copyright 2020 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#if !defined (SYSPROF_INSIDE) && !defined (SYSPROF_COMPILATION) +# error "Only can be included directly." +#endif + +#include + +#include "sysprof-version-macros.h" + +#include "mapped-ring-buffer.h" + +G_BEGIN_DECLS + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (MappedRingBuffer, mapped_ring_buffer_unref) + +G_GNUC_INTERNAL +guint mapped_ring_buffer_create_source (MappedRingBuffer *self, + MappedRingBufferCallback callback, + gpointer user_data); +G_GNUC_INTERNAL +guint mapped_ring_buffer_create_source_full (MappedRingBuffer *self, + MappedRingBufferCallback callback, + gpointer user_data, + GDestroyNotify destroy); + +G_END_DECLS diff --git a/src/libsysprof/meson.build b/src/libsysprof/meson.build index e2df0b8c..cc5f96ff 100644 --- a/src/libsysprof/meson.build +++ b/src/libsysprof/meson.build @@ -72,6 +72,7 @@ libsysprof_private_sources = [ 'binfile.c', 'demangle.cpp', 'elfparser.c', + 'mapped-ring-buffer-source.c', 'sysprof-flatpak.c', 'sysprof-helpers.c', 'sysprof-kallsyms.c', diff --git a/src/libsysprof/sysprof-control-source.c b/src/libsysprof/sysprof-control-source.c index 4ccdc482..fdf8f504 100644 --- a/src/libsysprof/sysprof-control-source.c +++ b/src/libsysprof/sysprof-control-source.c @@ -33,6 +33,7 @@ #include #include "mapped-ring-buffer.h" +#include "mapped-ring-buffer-source.h" #include "sysprof-control-source.h" From 2c2cbf6343f0cc88bfdb8c7a4023efc8808ff419 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 15:04:51 +0100 Subject: [PATCH 10/62] libsysprof-capture: Use C11 types instead of GLib types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an almost entirely mechanical* conversion from (for example) `gint` → `int`, `guint8` → `uint8_t`, etc. It is not entirely complete, as many GLib functions are still used in libsysprof-capture, which necessitate some use of GLib types. It also avoids renaming `gboolean` → `bool` as that’s a slightly more controversial change which will happen in the following commit. *Code was manually realigned afterwards. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 73 ++-- src/libsysprof-capture/mapped-ring-buffer.h | 23 +- src/libsysprof-capture/sysprof-address.c | 2 +- src/libsysprof-capture/sysprof-address.h | 10 +- .../sysprof-capture-condition.c | 60 ++-- .../sysprof-capture-condition.h | 16 +- .../sysprof-capture-cursor.c | 8 +- .../sysprof-capture-cursor.h | 4 +- .../sysprof-capture-reader.c | 151 ++++---- .../sysprof-capture-reader.h | 22 +- .../sysprof-capture-types.h | 135 +++---- .../sysprof-capture-util-private.h | 2 +- src/libsysprof-capture/sysprof-capture-util.c | 2 +- .../sysprof-capture-writer-cat.c | 63 ++-- .../sysprof-capture-writer.c | 333 +++++++++--------- .../sysprof-capture-writer.h | 191 +++++----- src/libsysprof-capture/sysprof-clock.c | 6 +- src/libsysprof-capture/sysprof-clock.h | 7 +- src/libsysprof-capture/sysprof-collector.c | 68 ++-- src/libsysprof-capture/sysprof-collector.h | 28 +- src/libsysprof-capture/sysprof-platform.c | 4 +- src/libsysprof-capture/sysprof-platform.h | 7 +- 22 files changed, 616 insertions(+), 599 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index ebc3ee93..a364ba8d 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -22,6 +22,7 @@ #include "config.h" +#include #include #include #include @@ -32,7 +33,7 @@ #include "mapped-ring-buffer.h" #define DEFAULT_N_PAGES 32 -#define BUFFER_MAX_SIZE ((G_MAXUINT32/2)-_sysprof_getpagesize()) +#define BUFFER_MAX_SIZE ((UINT32_MAX/2)-_sysprof_getpagesize()) enum { MODE_READER = 1, @@ -47,10 +48,10 @@ enum { */ typedef struct _MappedRingHeader { - guint32 head; - guint32 tail; - guint32 offset; - guint32 size; + uint32_t head; + uint32_t tail; + uint32_t offset; + uint32_t size; } MappedRingHeader; G_STATIC_ASSERT (sizeof (MappedRingHeader) == 16); @@ -61,12 +62,12 @@ G_STATIC_ASSERT (sizeof (MappedRingHeader) == 16); */ struct _MappedRingBuffer { - volatile gint ref_count; + volatile int ref_count; int mode; int fd; void *map; - gsize body_size; - gsize page_size; + size_t body_size; + size_t page_size; }; static inline MappedRingHeader * @@ -75,19 +76,19 @@ get_header (MappedRingBuffer *self) return (MappedRingHeader *)self->map; } -static inline gpointer +static inline void * get_body_at_pos (MappedRingBuffer *self, - gsize pos) + size_t pos) { g_assert (pos < (self->body_size + self->body_size)); - return (guint8 *)self->map + self->page_size + pos; + return (uint8_t *)self->map + self->page_size + pos; } -static gpointer -map_head_and_body_twice (int fd, - gsize head_size, - gsize body_size) +static void * +map_head_and_body_twice (int fd, + size_t head_size, + size_t body_size) { void *map; void *second; @@ -118,7 +119,7 @@ map_head_and_body_twice (int fd, * By mmap()'ing over the old region, the previous region is automatically * munmap()'d for us. */ - second = mmap ((guint8 *)map + head_size + body_size, + second = mmap ((uint8_t *)map + head_size + body_size, body_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, @@ -131,7 +132,7 @@ map_head_and_body_twice (int fd, return NULL; } - g_assert (second == (gpointer)((guint8 *)map + head_size + body_size)); + g_assert (second == (void *)((uint8_t *)map + head_size + body_size)); return map; } @@ -157,11 +158,11 @@ map_head_and_body_twice (int fd, * Returns: (transfer full): a #MappedRingBuffer */ MappedRingBuffer * -mapped_ring_buffer_new_reader (gsize buffer_size) +mapped_ring_buffer_new_reader (size_t buffer_size) { MappedRingBuffer *self; MappedRingHeader *header; - gsize page_size; + size_t page_size; void *map; int fd; @@ -212,7 +213,7 @@ mapped_ring_buffer_new_reader (gsize buffer_size) } MappedRingBuffer * -mapped_ring_buffer_new_readwrite (gsize buffer_size) +mapped_ring_buffer_new_readwrite (size_t buffer_size) { MappedRingBuffer *self; @@ -234,12 +235,12 @@ mapped_ring_buffer_new_readwrite (gsize buffer_size) * Returns: (transfer full) (nullable): a new #MappedRingBuffer */ MappedRingBuffer * -mapped_ring_buffer_new_writer (gint fd) +mapped_ring_buffer_new_writer (int fd) { MappedRingBuffer *self; MappedRingHeader *header; - gssize buffer_size; - gsize page_size; + ssize_t buffer_size; + size_t page_size; void *map; g_return_val_if_fail (fd > -1, NULL); @@ -346,7 +347,7 @@ mapped_ring_buffer_ref (MappedRingBuffer *self) return self; } -gint +int mapped_ring_buffer_get_fd (MappedRingBuffer *self) { g_return_val_if_fail (self != NULL, -1); @@ -377,13 +378,13 @@ mapped_ring_buffer_get_fd (MappedRingBuffer *self) * Returns: (nullable): a pointer to data of at least @length bytes * or %NULL if there is not enough space. */ -gpointer +void * mapped_ring_buffer_allocate (MappedRingBuffer *self, - gsize length) + size_t length) { MappedRingHeader *header; - gsize headpos; - gsize tailpos; + uint32_t headpos; + uint32_t tailpos; g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (self->mode & MODE_WRITER, NULL); @@ -436,10 +437,10 @@ mapped_ring_buffer_allocate (MappedRingBuffer *self, */ void mapped_ring_buffer_advance (MappedRingBuffer *self, - gsize length) + size_t length) { MappedRingHeader *header; - guint32 tail; + uint32_t tail; g_return_if_fail (self != NULL); g_return_if_fail (self->mode & MODE_WRITER); @@ -480,11 +481,11 @@ mapped_ring_buffer_advance (MappedRingBuffer *self, gboolean mapped_ring_buffer_drain (MappedRingBuffer *self, MappedRingBufferCallback callback, - gpointer user_data) + void *user_data) { MappedRingHeader *header; - gsize headpos; - gsize tailpos; + uint32_t headpos; + uint32_t tailpos; g_return_val_if_fail (self != NULL, FALSE); g_return_val_if_fail (self->mode & MODE_READER, FALSE); @@ -510,8 +511,8 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, while (headpos < tailpos) { - gconstpointer data = get_body_at_pos (self, headpos); - gsize len = tailpos - headpos; + const void *data = get_body_at_pos (self, headpos); + size_t len = tailpos - headpos; if (!callback (data, &len, user_data)) return FALSE; @@ -545,7 +546,7 @@ gboolean mapped_ring_buffer_is_empty (MappedRingBuffer *self) { MappedRingHeader *header; - guint32 headpos, tailpos; + uint32_t headpos, tailpos; header = get_header (self); diff --git a/src/libsysprof-capture/mapped-ring-buffer.h b/src/libsysprof-capture/mapped-ring-buffer.h index 19740e17..15d9a00e 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.h +++ b/src/libsysprof-capture/mapped-ring-buffer.h @@ -21,6 +21,7 @@ #pragma once #include +#include G_BEGIN_DECLS @@ -49,18 +50,18 @@ typedef struct _MappedRingBuffer MappedRingBuffer; * * Returns: %TRUE to coninue draining, otherwise %FALSE and draining stops */ -typedef gboolean (*MappedRingBufferCallback) (gconstpointer data, - gsize *length, - gpointer user_data); +typedef gboolean (*MappedRingBufferCallback) (const void *data, + size_t *length, + void *user_data); G_GNUC_INTERNAL -MappedRingBuffer *mapped_ring_buffer_new_reader (gsize buffer_size); +MappedRingBuffer *mapped_ring_buffer_new_reader (size_t buffer_size); G_GNUC_INTERNAL -MappedRingBuffer *mapped_ring_buffer_new_readwrite (gsize buffer_size); +MappedRingBuffer *mapped_ring_buffer_new_readwrite (size_t buffer_size); G_GNUC_INTERNAL -MappedRingBuffer *mapped_ring_buffer_new_writer (gint fd); +MappedRingBuffer *mapped_ring_buffer_new_writer (int fd); G_GNUC_INTERNAL -gint mapped_ring_buffer_get_fd (MappedRingBuffer *self); +int mapped_ring_buffer_get_fd (MappedRingBuffer *self); G_GNUC_INTERNAL MappedRingBuffer *mapped_ring_buffer_ref (MappedRingBuffer *self); G_GNUC_INTERNAL @@ -68,15 +69,15 @@ void mapped_ring_buffer_unref (MappedRingBuffer G_GNUC_INTERNAL void mapped_ring_buffer_clear (MappedRingBuffer *self); G_GNUC_INTERNAL -gpointer mapped_ring_buffer_allocate (MappedRingBuffer *self, - gsize length); +void *mapped_ring_buffer_allocate (MappedRingBuffer *self, + size_t length); G_GNUC_INTERNAL void mapped_ring_buffer_advance (MappedRingBuffer *self, - gsize length); + size_t length); G_GNUC_INTERNAL gboolean mapped_ring_buffer_drain (MappedRingBuffer *self, MappedRingBufferCallback callback, - gpointer user_data); + void *user_data); G_GNUC_INTERNAL gboolean mapped_ring_buffer_is_empty (MappedRingBuffer *self); diff --git a/src/libsysprof-capture/sysprof-address.c b/src/libsysprof-capture/sysprof-address.c index e6009627..8d2fc06f 100644 --- a/src/libsysprof-capture/sysprof-address.c +++ b/src/libsysprof-capture/sysprof-address.c @@ -105,7 +105,7 @@ sysprof_address_is_context_switch (SysprofAddress address, } } -const gchar * +const char * sysprof_address_context_to_string (SysprofAddressContext context) { switch (context) diff --git a/src/libsysprof-capture/sysprof-address.h b/src/libsysprof-capture/sysprof-address.h index 88cb5b72..ece0a99e 100644 --- a/src/libsysprof-capture/sysprof-address.h +++ b/src/libsysprof-capture/sysprof-address.h @@ -56,13 +56,15 @@ #pragma once +#include + #include "sysprof-version-macros.h" G_BEGIN_DECLS -typedef guint64 SysprofAddress; +typedef uint64_t SysprofAddress; -G_STATIC_ASSERT (sizeof (SysprofAddress) >= sizeof (gpointer)); +G_STATIC_ASSERT (sizeof (SysprofAddress) >= sizeof (void *)); typedef enum { @@ -79,9 +81,9 @@ SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_address_is_context_switch (SysprofAddress address, SysprofAddressContext *context); SYSPROF_AVAILABLE_IN_ALL -const gchar *sysprof_address_context_to_string (SysprofAddressContext context); +const char *sysprof_address_context_to_string (SysprofAddressContext context); -static inline gint +static inline int sysprof_address_compare (SysprofAddress a, SysprofAddress b) { diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 78fb0362..aaa81262 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -86,13 +86,13 @@ typedef enum struct _SysprofCaptureCondition { - volatile gint ref_count; + volatile int ref_count; SysprofCaptureConditionType type; union { GArray *where_type_in; struct { - gint64 begin; - gint64 end; + int64_t begin; + int64_t end; } where_time_between; GArray *where_pid_in; GArray *where_counter_in; @@ -100,7 +100,7 @@ struct _SysprofCaptureCondition SysprofCaptureCondition *left; SysprofCaptureCondition *right; } and, or; - gchar *where_file; + char *where_file; } u; }; @@ -122,7 +122,7 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, sysprof_capture_condition_match (self->u.or.right, frame); case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: - for (guint i = 0; i < self->u.where_type_in->len; i++) + for (size_t i = 0; i < self->u.where_type_in->len; i++) { if (frame->type == g_array_index (self->u.where_type_in, SysprofCaptureFrameType, i)) return TRUE; @@ -133,9 +133,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, return (frame->time >= self->u.where_time_between.begin && frame->time <= self->u.where_time_between.end); case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: - for (guint i = 0; i < self->u.where_pid_in->len; i++) + for (size_t i = 0; i < self->u.where_pid_in->len; i++) { - if (frame->pid == g_array_index (self->u.where_pid_in, gint32, i)) + if (frame->pid == g_array_index (self->u.where_pid_in, int32_t, i)) return TRUE; } return FALSE; @@ -145,11 +145,11 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, { const SysprofCaptureCounterSet *set = (SysprofCaptureCounterSet *)frame; - for (guint i = 0; i < self->u.where_counter_in->len; i++) + for (size_t i = 0; i < self->u.where_counter_in->len; i++) { - guint counter = g_array_index (self->u.where_counter_in, guint, i); + unsigned int counter = g_array_index (self->u.where_counter_in, unsigned int, i); - for (guint j = 0; j < set->n_values; j++) + for (unsigned int j = 0; j < set->n_values; j++) { if (counter == set->values[j].ids[0] || counter == set->values[j].ids[1] || @@ -167,11 +167,11 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, { const SysprofCaptureCounterDefine *def = (SysprofCaptureCounterDefine *)frame; - for (guint i = 0; i < self->u.where_counter_in->len; i++) + for (size_t i = 0; i < self->u.where_counter_in->len; i++) { - guint counter = g_array_index (self->u.where_counter_in, guint, i); + unsigned int counter = g_array_index (self->u.where_counter_in, unsigned int, i); - for (guint j = 0; j < def->n_counters; j++) + for (unsigned int j = 0; j < def->n_counters; j++) { if (def->counters[j].id == counter) return TRUE; @@ -225,7 +225,7 @@ sysprof_capture_condition_copy (const SysprofCaptureCondition *self) case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: return sysprof_capture_condition_new_where_type_in ( self->u.where_type_in->len, - (const SysprofCaptureFrameType *)(gpointer)self->u.where_type_in->data); + (const SysprofCaptureFrameType *)(void *)self->u.where_type_in->data); case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN: return sysprof_capture_condition_new_where_time_between ( @@ -235,12 +235,12 @@ sysprof_capture_condition_copy (const SysprofCaptureCondition *self) case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: return sysprof_capture_condition_new_where_pid_in ( self->u.where_pid_in->len, - (const gint32 *)(gpointer)self->u.where_pid_in->data); + (const int32_t *)(void *)self->u.where_pid_in->data); case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN: return sysprof_capture_condition_new_where_counter_in ( self->u.where_counter_in->len, - (const guint *)(gpointer)self->u.where_counter_in->data); + (const unsigned int *)(void *)self->u.where_counter_in->data); case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: return sysprof_capture_condition_new_where_file (self->u.where_file); @@ -315,8 +315,8 @@ sysprof_capture_condition_unref (SysprofCaptureCondition *self) } SysprofCaptureCondition * -sysprof_capture_condition_new_where_type_in (guint n_types, - const SysprofCaptureFrameType *types) +sysprof_capture_condition_new_where_type_in (unsigned int n_types, + const SysprofCaptureFrameType *types) { SysprofCaptureCondition *self; @@ -332,14 +332,14 @@ sysprof_capture_condition_new_where_type_in (guint n_types, } SysprofCaptureCondition * -sysprof_capture_condition_new_where_time_between (gint64 begin_time, - gint64 end_time) +sysprof_capture_condition_new_where_time_between (int64_t begin_time, + int64_t end_time) { SysprofCaptureCondition *self; if G_UNLIKELY (begin_time > end_time) { - gint64 tmp = begin_time; + int64_t tmp = begin_time; begin_time = end_time; end_time = tmp; } @@ -353,8 +353,8 @@ sysprof_capture_condition_new_where_time_between (gint64 begin_time, } SysprofCaptureCondition * -sysprof_capture_condition_new_where_pid_in (guint n_pids, - const gint32 *pids) +sysprof_capture_condition_new_where_pid_in (unsigned int n_pids, + const int32_t *pids) { SysprofCaptureCondition *self; @@ -362,16 +362,16 @@ sysprof_capture_condition_new_where_pid_in (guint n_pids, self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN; - self->u.where_pid_in = g_array_sized_new (FALSE, FALSE, sizeof (gint32), n_pids); + self->u.where_pid_in = g_array_sized_new (FALSE, FALSE, sizeof (int32_t), n_pids); g_array_set_size (self->u.where_pid_in, n_pids); - memcpy (self->u.where_pid_in->data, pids, sizeof (gint32) * n_pids); + memcpy (self->u.where_pid_in->data, pids, sizeof (int32_t) * n_pids); return self; } SysprofCaptureCondition * -sysprof_capture_condition_new_where_counter_in (guint n_counters, - const guint *counters) +sysprof_capture_condition_new_where_counter_in (unsigned int n_counters, + const unsigned int *counters) { SysprofCaptureCondition *self; @@ -379,12 +379,12 @@ sysprof_capture_condition_new_where_counter_in (guint n_counters, self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN; - self->u.where_counter_in = g_array_sized_new (FALSE, FALSE, sizeof (guint), n_counters); + self->u.where_counter_in = g_array_sized_new (FALSE, FALSE, sizeof (unsigned int), n_counters); if (n_counters > 0) { g_array_set_size (self->u.where_counter_in, n_counters); - memcpy (self->u.where_counter_in->data, counters, sizeof (guint) * n_counters); + memcpy (self->u.where_counter_in->data, counters, sizeof (unsigned int) * n_counters); } return self; @@ -454,7 +454,7 @@ sysprof_capture_condition_new_or (SysprofCaptureCondition *left, * Returns: (transfer full): a new #SysprofCaptureCondition */ SysprofCaptureCondition * -sysprof_capture_condition_new_where_file (const gchar *path) +sysprof_capture_condition_new_where_file (const char *path) { SysprofCaptureCondition *self; diff --git a/src/libsysprof-capture/sysprof-capture-condition.h b/src/libsysprof-capture/sysprof-capture-condition.h index 33c356c7..8792449e 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.h +++ b/src/libsysprof-capture/sysprof-capture-condition.h @@ -74,19 +74,19 @@ SYSPROF_AVAILABLE_IN_ALL SysprofCaptureCondition *sysprof_capture_condition_new_or (SysprofCaptureCondition *left, SysprofCaptureCondition *right); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureCondition *sysprof_capture_condition_new_where_type_in (guint n_types, +SysprofCaptureCondition *sysprof_capture_condition_new_where_type_in (unsigned int n_types, const SysprofCaptureFrameType *types); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureCondition *sysprof_capture_condition_new_where_time_between (gint64 begin_time, - gint64 end_time); +SysprofCaptureCondition *sysprof_capture_condition_new_where_time_between (int64_t begin_time, + int64_t end_time); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureCondition *sysprof_capture_condition_new_where_pid_in (guint n_pids, - const gint32 *pids); +SysprofCaptureCondition *sysprof_capture_condition_new_where_pid_in (unsigned int n_pids, + const int32_t *pids); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureCondition *sysprof_capture_condition_new_where_counter_in (guint n_counters, - const guint *counters); +SysprofCaptureCondition *sysprof_capture_condition_new_where_counter_in (unsigned int n_counters, + const unsigned int *counters); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureCondition *sysprof_capture_condition_new_where_file (const gchar *path); +SysprofCaptureCondition *sysprof_capture_condition_new_where_file (const char *path); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_condition_match (const SysprofCaptureCondition *self, const SysprofCaptureFrame *frame); diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index d2d014fb..b854a6c3 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -68,10 +68,10 @@ typedef const SysprofCaptureFrame *(*ReadDelegate) (SysprofCaptureReader *); struct _SysprofCaptureCursor { - volatile gint ref_count; + volatile int ref_count; GPtrArray *conditions; SysprofCaptureReader *reader; - guint reversed : 1; + unsigned int reversed : 1; }; static void @@ -138,7 +138,7 @@ sysprof_capture_cursor_unref (SysprofCaptureCursor *self) void sysprof_capture_cursor_foreach (SysprofCaptureCursor *self, SysprofCaptureCursorCallback callback, - gpointer user_data) + void *user_data) { g_return_if_fail (self != NULL); g_return_if_fail (self->reader != NULL); @@ -231,7 +231,7 @@ sysprof_capture_cursor_foreach (SysprofCaptureCursor *self, } else { - for (guint i = 0; i < self->conditions->len; i++) + for (size_t i = 0; i < self->conditions->len; i++) { const SysprofCaptureCondition *condition = g_ptr_array_index (self->conditions, i); diff --git a/src/libsysprof-capture/sysprof-capture-cursor.h b/src/libsysprof-capture/sysprof-capture-cursor.h index 13ef791a..8b2c0598 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.h +++ b/src/libsysprof-capture/sysprof-capture-cursor.h @@ -75,7 +75,7 @@ typedef struct _SysprofCaptureCursor SysprofCaptureCursor; * Returns: %TRUE if iteration should continue, otherwise %FALSE. */ typedef gboolean (*SysprofCaptureCursorCallback) (const SysprofCaptureFrame *frame, - gpointer user_data); + void *user_data); SYSPROF_AVAILABLE_IN_ALL SysprofCaptureCursor *sysprof_capture_cursor_new (SysprofCaptureReader *reader); @@ -88,7 +88,7 @@ SysprofCaptureReader *sysprof_capture_cursor_get_reader (SysprofCaptureCursor SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_cursor_foreach (SysprofCaptureCursor *self, SysprofCaptureCursorCallback callback, - gpointer user_data); + void *user_data); SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_cursor_reset (SysprofCaptureCursor *self); SYSPROF_AVAILABLE_IN_ALL diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index ed157621..4cf634bf 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -60,6 +60,7 @@ #include #include +#include #include #include #include @@ -71,19 +72,19 @@ struct _SysprofCaptureReader { - volatile gint ref_count; - gchar *filename; - guint8 *buf; - gsize bufsz; - gsize len; - gsize pos; - gsize fd_off; + volatile int ref_count; + char *filename; + uint8_t *buf; + size_t bufsz; + size_t len; + size_t pos; + size_t fd_off; int fd; - gint endian; + int endian; SysprofCaptureFileHeader header; - gint64 end_time; + int64_t end_time; SysprofCaptureStat st_buf; - guint st_buf_set : 1; + unsigned int st_buf_set : 1; }; static gboolean @@ -129,7 +130,7 @@ sysprof_capture_reader_finalize (SysprofCaptureReader *self) } } -const gchar * +const char * sysprof_capture_reader_get_time (SysprofCaptureReader *self) { g_return_val_if_fail (self != NULL, NULL); @@ -137,7 +138,7 @@ sysprof_capture_reader_get_time (SysprofCaptureReader *self) return self->header.capture_time; } -const gchar * +const char * sysprof_capture_reader_get_filename (SysprofCaptureReader *self) { g_return_val_if_fail (self != NULL, NULL); @@ -154,7 +155,7 @@ sysprof_capture_reader_discover_end_time (SysprofCaptureReader *self) while (sysprof_capture_reader_peek_frame (self, &frame)) { - gint64 end_time = frame.time; + int64_t end_time = frame.time; switch (frame.type) { @@ -215,7 +216,7 @@ sysprof_capture_reader_new_from_fd (int fd, self = g_new0 (SysprofCaptureReader, 1); self->ref_count = 1; - self->bufsz = G_MAXUSHORT * 2; + self->bufsz = USHRT_MAX * 2; self->buf = g_malloc (self->bufsz); self->len = 0; self->pos = 0; @@ -244,7 +245,7 @@ sysprof_capture_reader_new_from_fd (int fd, } SysprofCaptureReader * -sysprof_capture_reader_new (const gchar *filename, +sysprof_capture_reader_new (const char *filename, GError **error) { SysprofCaptureReader *self; @@ -350,7 +351,7 @@ sysprof_capture_reader_bswap_jitmap (SysprofCaptureReader *self, static gboolean sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self, - gsize len) + size_t len) { g_assert (self != NULL); g_assert (self->pos <= self->len); @@ -361,7 +362,7 @@ sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self, if ((self->len - self->pos) < len) { - gssize r; + ssize_t r; if (self->len > self->pos) memmove (self->buf, &self->buf[self->pos], self->len - self->pos); @@ -401,7 +402,7 @@ sysprof_capture_reader_skip (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof (SysprofCaptureFrame))) return FALSE; - frame = (SysprofCaptureFrame *)(gpointer)&self->buf[self->pos]; + frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, frame); if (frame->len < sizeof (SysprofCaptureFrame)) @@ -410,7 +411,7 @@ sysprof_capture_reader_skip (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, frame->len)) return FALSE; - frame = (SysprofCaptureFrame *)(gpointer)&self->buf[self->pos]; + frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos]; self->pos += frame->len; @@ -436,7 +437,7 @@ sysprof_capture_reader_peek_frame (SysprofCaptureReader *self, g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - real_frame = (SysprofCaptureFrame *)(gpointer)&self->buf[self->pos]; + real_frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos]; *frame = *real_frame; @@ -472,10 +473,10 @@ sysprof_capture_reader_peek_type (SysprofCaptureReader *self, static const SysprofCaptureFrame * sysprof_capture_reader_read_basic (SysprofCaptureReader *self, SysprofCaptureFrameType type, - gsize extra) + size_t extra) { SysprofCaptureFrame *frame; - gsize len = sizeof *frame + extra; + size_t len = sizeof *frame + extra; g_assert (self != NULL); g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); @@ -484,7 +485,7 @@ sysprof_capture_reader_read_basic (SysprofCaptureReader *self, if (!sysprof_capture_reader_ensure_space_for (self, len)) return NULL; - frame = (SysprofCaptureFrame *)(gpointer)&self->buf[self->pos]; + frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, frame); @@ -524,7 +525,7 @@ sysprof_capture_reader_read_fork (SysprofCaptureReader *self) g_assert (self != NULL); fk = (SysprofCaptureFork *) - sysprof_capture_reader_read_basic (self, SYSPROF_CAPTURE_FRAME_FORK, sizeof(guint32)); + sysprof_capture_reader_read_basic (self, SYSPROF_CAPTURE_FRAME_FORK, sizeof (uint32_t)); if (fk != NULL) { @@ -547,7 +548,7 @@ sysprof_capture_reader_read_map (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *map)) return NULL; - map = (SysprofCaptureMap *)(gpointer)&self->buf[self->pos]; + map = (SysprofCaptureMap *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &map->frame); @@ -560,7 +561,7 @@ sysprof_capture_reader_read_map (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, map->frame.len)) return NULL; - map = (SysprofCaptureMap *)(gpointer)&self->buf[self->pos]; + map = (SysprofCaptureMap *)(void *)&self->buf[self->pos]; if (self->buf[self->pos + map->frame.len - 1] != '\0') return NULL; @@ -587,7 +588,7 @@ sysprof_capture_reader_read_log (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *log)) return NULL; - log = (SysprofCaptureLog *)(gpointer)&self->buf[self->pos]; + log = (SysprofCaptureLog *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &log->frame); @@ -600,7 +601,7 @@ sysprof_capture_reader_read_log (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, log->frame.len)) return NULL; - log = (SysprofCaptureLog *)(gpointer)&self->buf[self->pos]; + log = (SysprofCaptureLog *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_log (self, log); @@ -612,7 +613,7 @@ sysprof_capture_reader_read_log (SysprofCaptureReader *self) /* Ensure trailing \0 in domain and message */ log->domain[sizeof log->domain - 1] = 0; if (log->frame.len > sizeof *log) - ((gchar *)log)[log->frame.len - 1] = 0; + ((char *)log)[log->frame.len - 1] = 0; return log; } @@ -629,7 +630,7 @@ sysprof_capture_reader_read_mark (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *mark)) return NULL; - mark = (SysprofCaptureMark *)(gpointer)&self->buf[self->pos]; + mark = (SysprofCaptureMark *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &mark->frame); @@ -642,7 +643,7 @@ sysprof_capture_reader_read_mark (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, mark->frame.len)) return NULL; - mark = (SysprofCaptureMark *)(gpointer)&self->buf[self->pos]; + mark = (SysprofCaptureMark *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_mark (self, mark); @@ -654,7 +655,7 @@ sysprof_capture_reader_read_mark (SysprofCaptureReader *self) /* Ensure trailing \0 in name and message */ mark->name[sizeof mark->name - 1] = 0; if (mark->frame.len > sizeof *mark) - ((gchar *)mark)[mark->frame.len - 1] = 0; + ((char *)mark)[mark->frame.len - 1] = 0; /* Maybe update end-time */ if G_UNLIKELY ((mark->frame.time + mark->duration) > self->end_time) @@ -675,7 +676,7 @@ sysprof_capture_reader_read_metadata (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *metadata)) return NULL; - metadata = (SysprofCaptureMetadata *)(gpointer)&self->buf[self->pos]; + metadata = (SysprofCaptureMetadata *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &metadata->frame); @@ -688,7 +689,7 @@ sysprof_capture_reader_read_metadata (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, metadata->frame.len)) return NULL; - metadata = (SysprofCaptureMetadata *)(gpointer)&self->buf[self->pos]; + metadata = (SysprofCaptureMetadata *)(void *)&self->buf[self->pos]; self->pos += metadata->frame.len; @@ -698,7 +699,7 @@ sysprof_capture_reader_read_metadata (SysprofCaptureReader *self) /* Ensure trailing \0 in .id and .metadata */ metadata->id[sizeof metadata->id - 1] = 0; if (metadata->frame.len > sizeof *metadata) - ((gchar *)metadata)[metadata->frame.len - 1] = 0; + ((char *)metadata)[metadata->frame.len - 1] = 0; return metadata; } @@ -715,7 +716,7 @@ sysprof_capture_reader_read_process (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *process)) return NULL; - process = (SysprofCaptureProcess *)(gpointer)&self->buf[self->pos]; + process = (SysprofCaptureProcess *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &process->frame); @@ -728,7 +729,7 @@ sysprof_capture_reader_read_process (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, process->frame.len)) return NULL; - process = (SysprofCaptureProcess *)(gpointer)&self->buf[self->pos]; + process = (SysprofCaptureProcess *)(void *)&self->buf[self->pos]; if (self->buf[self->pos + process->frame.len - 1] != '\0') return NULL; @@ -746,9 +747,9 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) { g_autoptr(GHashTable) ret = NULL; SysprofCaptureJitmap *jitmap; - guint8 *buf; - guint8 *endptr; - guint i; + uint8_t *buf; + uint8_t *endptr; + unsigned int i; g_assert (self != NULL); g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); @@ -757,7 +758,7 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *jitmap)) return NULL; - jitmap = (SysprofCaptureJitmap *)(gpointer)&self->buf[self->pos]; + jitmap = (SysprofCaptureJitmap *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &jitmap->frame); @@ -780,7 +781,7 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) for (i = 0; i < jitmap->n_jitmaps; i++) { SysprofCaptureAddress addr; - const gchar *str; + const char *str; if (buf + sizeof addr >= endptr) return NULL; @@ -788,7 +789,7 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) memcpy (&addr, buf, sizeof addr); buf += sizeof addr; - str = (gchar *)buf; + str = (char *)buf; buf = memchr (buf, '\0', (endptr - buf)); @@ -819,7 +820,7 @@ sysprof_capture_reader_read_sample (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *sample)) return NULL; - sample = (SysprofCaptureSample *)(gpointer)&self->buf[self->pos]; + sample = (SysprofCaptureSample *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &sample->frame); @@ -838,11 +839,11 @@ sysprof_capture_reader_read_sample (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sample->frame.len)) return NULL; - sample = (SysprofCaptureSample *)(gpointer)&self->buf[self->pos]; + sample = (SysprofCaptureSample *)(void *)&self->buf[self->pos]; if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) { - guint i; + unsigned int i; for (i = 0; i < sample->n_addrs; i++) sample->addrs[i] = GUINT64_SWAP_LE_BE (sample->addrs[i]); @@ -865,7 +866,7 @@ sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *def)) return NULL; - def = (SysprofCaptureCounterDefine *)(gpointer)&self->buf[self->pos]; + def = (SysprofCaptureCounterDefine *)(void *)&self->buf[self->pos]; if (def->frame.type != SYSPROF_CAPTURE_FRAME_CTRDEF) return NULL; @@ -882,11 +883,11 @@ sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, def->frame.len)) return NULL; - def = (SysprofCaptureCounterDefine *)(gpointer)&self->buf[self->pos]; + def = (SysprofCaptureCounterDefine *)(void *)&self->buf[self->pos]; if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) { - guint i; + unsigned int i; for (i = 0; i < def->n_counters; i++) { @@ -912,7 +913,7 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *set)) return NULL; - set = (SysprofCaptureCounterSet *)(gpointer)&self->buf[self->pos]; + set = (SysprofCaptureCounterSet *)(void *)&self->buf[self->pos]; if (set->frame.type != SYSPROF_CAPTURE_FRAME_CTRSET) return NULL; @@ -929,15 +930,15 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, set->frame.len)) return NULL; - set = (SysprofCaptureCounterSet *)(gpointer)&self->buf[self->pos]; + set = (SysprofCaptureCounterSet *)(void *)&self->buf[self->pos]; if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) { - guint i; + unsigned int i; for (i = 0; i < set->n_values; i++) { - guint j; + unsigned int j; for (j = 0; j < G_N_ELEMENTS (set->values[0].values); j++) { @@ -1026,12 +1027,12 @@ sysprof_capture_reader_splice (SysprofCaptureReader *self, */ gboolean sysprof_capture_reader_save_as (SysprofCaptureReader *self, - const gchar *filename, + const char *filename, GError **error) { struct stat stbuf; off_t in_off; - gsize to_write; + size_t to_write; int fd = -1; g_assert (self != NULL); @@ -1054,7 +1055,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, while (to_write > 0) { - gssize written; + ssize_t written; written = _sysprof_sendfile (fd, self->fd, &in_off, to_write); @@ -1064,7 +1065,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, if (written == 0 && errno != EAGAIN) goto handle_errno; - g_assert (written <= (gssize)to_write); + g_assert (written <= (ssize_t)to_write); to_write -= written; } @@ -1088,7 +1089,7 @@ handle_errno: return FALSE; } -gint64 +int64_t sysprof_capture_reader_get_start_time (SysprofCaptureReader *self) { g_return_val_if_fail (self != NULL, 0); @@ -1114,10 +1115,10 @@ sysprof_capture_reader_get_start_time (SysprofCaptureReader *self) * * Since: 3.22.1 */ -gint64 +int64_t sysprof_capture_reader_get_end_time (SysprofCaptureReader *self) { - gint64 end_time = 0; + int64_t end_time = 0; g_return_val_if_fail (self != NULL, 0); @@ -1212,7 +1213,7 @@ sysprof_capture_reader_read_file (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *file_chunk)) return NULL; - file_chunk = (SysprofCaptureFileChunk *)(gpointer)&self->buf[self->pos]; + file_chunk = (SysprofCaptureFileChunk *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &file_chunk->frame); @@ -1225,7 +1226,7 @@ sysprof_capture_reader_read_file (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, file_chunk->frame.len)) return NULL; - file_chunk = (SysprofCaptureFileChunk *)(gpointer)&self->buf[self->pos]; + file_chunk = (SysprofCaptureFileChunk *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_file_chunk (self, file_chunk); @@ -1244,7 +1245,7 @@ sysprof_capture_reader_read_file (SysprofCaptureReader *self) return file_chunk; } -gchar ** +char ** sysprof_capture_reader_list_files (SysprofCaptureReader *self) { g_autoptr(GHashTable) files = NULL; @@ -1280,13 +1281,13 @@ sysprof_capture_reader_list_files (SysprofCaptureReader *self) g_ptr_array_add (ar, g_strdup (key)); g_ptr_array_add (ar, NULL); - return (gchar **)g_ptr_array_free (g_steal_pointer (&ar), FALSE); + return (char **)g_ptr_array_free (g_steal_pointer (&ar), FALSE); } gboolean sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, - const gchar *path, - gint fd) + const char *path, + int fd) { g_assert (self != NULL); g_assert (path != NULL); @@ -1296,8 +1297,8 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, { SysprofCaptureFrameType type; const SysprofCaptureFileChunk *file; - const guint8 *buf; - gsize to_write; + const uint8_t *buf; + size_t to_write; if (!sysprof_capture_reader_peek_type (self, &type)) return FALSE; @@ -1316,7 +1317,7 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, while (to_write > 0) { - gssize written; + ssize_t written; written = _sysprof_write (fd, buf, to_write); if (written < 0) @@ -1325,7 +1326,7 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, if (written == 0 && errno != EAGAIN) return FALSE; - g_assert (written <= (gssize)to_write); + g_assert (written <= (ssize_t)to_write); buf += written; to_write -= written; @@ -1344,7 +1345,7 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, g_return_val_if_reached (FALSE); } -gint +int sysprof_capture_reader_get_byte_order (SysprofCaptureReader *self) { g_return_val_if_fail (self != NULL, 0); @@ -1354,7 +1355,7 @@ sysprof_capture_reader_get_byte_order (SysprofCaptureReader *self) const SysprofCaptureFileChunk * sysprof_capture_reader_find_file (SysprofCaptureReader *self, - const gchar *path) + const char *path) { SysprofCaptureFrameType type; @@ -1395,7 +1396,7 @@ sysprof_capture_reader_read_allocation (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, sizeof *ma)) return NULL; - ma = (SysprofCaptureAllocation *)(gpointer)&self->buf[self->pos]; + ma = (SysprofCaptureAllocation *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, &ma->frame); @@ -1419,11 +1420,11 @@ sysprof_capture_reader_read_allocation (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, ma->frame.len)) return NULL; - ma = (SysprofCaptureAllocation *)(gpointer)&self->buf[self->pos]; + ma = (SysprofCaptureAllocation *)(void *)&self->buf[self->pos]; if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) { - for (guint i = 0; i < ma->n_addrs; i++) + for (unsigned int i = 0; i < ma->n_addrs; i++) ma->addrs[i] = GUINT64_SWAP_LE_BE (ma->addrs[i]); } diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index 9c732b0a..09348584 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -64,7 +64,7 @@ G_BEGIN_DECLS typedef struct _SysprofCaptureReader SysprofCaptureReader; SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureReader *sysprof_capture_reader_new (const gchar *filename, +SysprofCaptureReader *sysprof_capture_reader_new (const char *filename, GError **error); SYSPROF_AVAILABLE_IN_ALL SysprofCaptureReader *sysprof_capture_reader_new_from_fd (int fd, @@ -76,15 +76,15 @@ SysprofCaptureReader *sysprof_capture_reader_ref ( SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_reader_unref (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gint sysprof_capture_reader_get_byte_order (SysprofCaptureReader *self); +int sysprof_capture_reader_get_byte_order (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -const gchar *sysprof_capture_reader_get_filename (SysprofCaptureReader *self); +const char *sysprof_capture_reader_get_filename (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -const gchar *sysprof_capture_reader_get_time (SysprofCaptureReader *self); +const char *sysprof_capture_reader_get_time (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gint64 sysprof_capture_reader_get_start_time (SysprofCaptureReader *self); +int64_t sysprof_capture_reader_get_start_time (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gint64 sysprof_capture_reader_get_end_time (SysprofCaptureReader *self); +int64_t sysprof_capture_reader_get_end_time (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_reader_skip (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL @@ -129,7 +129,7 @@ gboolean sysprof_capture_reader_splice ( GError **error); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_reader_save_as (SysprofCaptureReader *self, - const gchar *filename, + const char *filename, GError **error); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_reader_get_stat (SysprofCaptureReader *self, @@ -139,13 +139,13 @@ void sysprof_capture_reader_set_stat ( const SysprofCaptureStat *st_buf); SYSPROF_AVAILABLE_IN_ALL const SysprofCaptureFileChunk *sysprof_capture_reader_find_file (SysprofCaptureReader *self, - const gchar *path); + const char *path); SYSPROF_AVAILABLE_IN_ALL -gchar **sysprof_capture_reader_list_files (SysprofCaptureReader *self); +char **sysprof_capture_reader_list_files (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, - const gchar *path, - gint fd); + const char *path, + int fd); G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index fab5ba13..05f36c44 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -57,6 +57,9 @@ #pragma once #include +#include +#include +#include #include "sysprof-clock.h" @@ -92,25 +95,25 @@ typedef struct _SysprofCaptureWriter SysprofCaptureWriter; typedef struct _SysprofCaptureCursor SysprofCaptureCursor; typedef struct _SysprofCaptureCondition SysprofCaptureCondition; -typedef guint64 SysprofCaptureAddress; +typedef uint64_t SysprofCaptureAddress; typedef struct { /* * The number of frames indexed by SysprofCaptureFrameType */ - gsize frame_count[16]; + size_t frame_count[16]; /* * Padding for future expansion. */ - gsize padding[48]; + size_t padding[48]; } SysprofCaptureStat; typedef union { - gint64 v64; - gdouble vdbl; + int64_t v64; + double vdbl; } SysprofCaptureCounterValue; typedef enum @@ -137,28 +140,28 @@ typedef enum SYSPROF_ALIGNED_BEGIN(1) typedef struct { - guint32 magic; - guint32 version : 8; - guint32 little_endian : 1; - guint32 padding : 23; - gchar capture_time[64]; - gint64 time; - gint64 end_time; - gchar suffix[168]; + uint32_t magic; + uint32_t version : 8; + uint32_t little_endian : 1; + uint32_t padding : 23; + char capture_time[64]; + int64_t time; + int64_t end_time; + char suffix[168]; } SysprofCaptureFileHeader SYSPROF_ALIGNED_END(1); SYSPROF_ALIGNED_BEGIN(1) typedef struct { - guint16 len; - gint16 cpu; - gint32 pid; - gint64 time; - guint32 type : 8; - guint32 padding1 : 24; - guint32 padding2; - guint8 data[0]; + uint16_t len; + int16_t cpu; + int32_t pid; + int64_t time; + uint32_t type : 8; + uint32_t padding1 : 24; + uint32_t padding2; + uint8_t data[0]; } SysprofCaptureFrame SYSPROF_ALIGNED_END(1); @@ -166,11 +169,11 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - guint64 start; - guint64 end; - guint64 offset; - guint64 inode; - gchar filename[0]; + uint64_t start; + uint64_t end; + uint64_t offset; + uint64_t inode; + char filename[0]; } SysprofCaptureMap SYSPROF_ALIGNED_END(1); @@ -178,8 +181,8 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - guint32 n_jitmaps; - guint8 data[0]; + uint32_t n_jitmaps; + uint8_t data[0]; } SysprofCaptureJitmap SYSPROF_ALIGNED_END(1); @@ -187,7 +190,7 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - gchar cmdline[0]; + char cmdline[0]; } SysprofCaptureProcess SYSPROF_ALIGNED_END(1); @@ -195,9 +198,9 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - guint32 n_addrs : 16; - guint32 padding1 : 16; - gint32 tid; + uint32_t n_addrs : 16; + uint32_t padding1 : 16; + int32_t tid; SysprofCaptureAddress addrs[0]; } SysprofCaptureSample SYSPROF_ALIGNED_END(1); @@ -206,7 +209,7 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - gint32 child_pid; + int32_t child_pid; } SysprofCaptureFork SYSPROF_ALIGNED_END(1); @@ -227,11 +230,11 @@ SYSPROF_ALIGNED_END(1); SYSPROF_ALIGNED_BEGIN(1) typedef struct { - gchar category[32]; - gchar name[32]; - gchar description[52]; - guint32 id : 24; - guint32 type : 8; + char category[32]; + char name[32]; + char description[52]; + uint32_t id : 24; + uint32_t type : 8; SysprofCaptureCounterValue value; } SysprofCaptureCounter SYSPROF_ALIGNED_END(1); @@ -240,9 +243,9 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - guint32 n_counters : 16; - guint32 padding1 : 16; - guint32 padding2; + uint32_t n_counters : 16; + uint32_t padding1 : 16; + uint32_t padding2; SysprofCaptureCounter counters[0]; } SysprofCaptureCounterDefine SYSPROF_ALIGNED_END(1); @@ -255,7 +258,7 @@ typedef struct * bytes. So this makes a nice 2-cacheline aligned size which is * useful when the number of counters is rather small. */ - guint32 ids[8]; + uint32_t ids[8]; SysprofCaptureCounterValue values[8]; } SysprofCaptureCounterValues SYSPROF_ALIGNED_END(1); @@ -264,9 +267,9 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - guint32 n_values : 16; - guint32 padding1 : 16; - guint32 padding2; + uint32_t n_values : 16; + uint32_t padding1 : 16; + uint32_t padding2; SysprofCaptureCounterValues values[0]; } SysprofCaptureCounterSet SYSPROF_ALIGNED_END(1); @@ -275,10 +278,10 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - gint64 duration; - gchar group[24]; - gchar name[40]; - gchar message[0]; + int64_t duration; + char group[24]; + char name[40]; + char message[0]; } SysprofCaptureMark SYSPROF_ALIGNED_END(1); @@ -286,8 +289,8 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - gchar id[40]; - gchar metadata[0]; + char id[40]; + char metadata[0]; } SysprofCaptureMetadata SYSPROF_ALIGNED_END(1); @@ -295,11 +298,11 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - guint32 severity : 16; - guint32 padding1 : 16; - guint32 padding2 : 32; - gchar domain[32]; - gchar message[0]; + uint32_t severity : 16; + uint32_t padding1 : 16; + uint32_t padding2 : 32; + char domain[32]; + char message[0]; } SysprofCaptureLog SYSPROF_ALIGNED_END(1); @@ -307,11 +310,11 @@ SYSPROF_ALIGNED_BEGIN(1) typedef struct { SysprofCaptureFrame frame; - guint32 is_last : 1; - guint32 padding1 : 15; - guint32 len : 16; - gchar path[256]; - guint8 data[0]; + uint32_t is_last : 1; + uint32_t padding1 : 15; + uint32_t len : 16; + char path[256]; + uint8_t data[0]; } SysprofCaptureFileChunk SYSPROF_ALIGNED_END(1); @@ -320,10 +323,10 @@ typedef struct { SysprofCaptureFrame frame; SysprofCaptureAddress alloc_addr; - gint64 alloc_size; - gint32 tid; - guint32 n_addrs : 16; - guint32 padding1 : 16; + int64_t alloc_size; + int32_t tid; + uint32_t n_addrs : 16; + uint32_t padding1 : 16; SysprofCaptureAddress addrs[0]; } SysprofCaptureAllocation SYSPROF_ALIGNED_END(1); @@ -350,7 +353,7 @@ G_STATIC_ASSERT (sizeof (SysprofCaptureAllocation) == 48); G_STATIC_ASSERT ((G_STRUCT_OFFSET (SysprofCaptureAllocation, addrs) % SYSPROF_CAPTURE_ALIGN) == 0); G_STATIC_ASSERT ((G_STRUCT_OFFSET (SysprofCaptureSample, addrs) % SYSPROF_CAPTURE_ALIGN) == 0); -static inline gint +static inline int sysprof_capture_address_compare (SysprofCaptureAddress a, SysprofCaptureAddress b) { diff --git a/src/libsysprof-capture/sysprof-capture-util-private.h b/src/libsysprof-capture/sysprof-capture-util-private.h index 5350a178..26ba79e1 100644 --- a/src/libsysprof-capture/sysprof-capture-util-private.h +++ b/src/libsysprof-capture/sysprof-capture-util-private.h @@ -84,7 +84,7 @@ ssize_t _sysprof_pwrite (int fd, ssize_t _sysprof_write (int fd, const void *buf, size_t count); -gint32 _sysprof_getpid (void); +int32_t _sysprof_getpid (void); ssize_t _sysprof_sendfile (int out_fd, int in_fd, off_t *offset, diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index 58336e7f..4571b4dd 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -157,7 +157,7 @@ ssize_t #endif } -gint32 +int32_t (_sysprof_getpid) (void) { #ifdef G_OS_WIN32 diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 0e473f40..504835a6 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -59,14 +59,15 @@ #include "config.h" #include +#include #include #include #include typedef struct { - guint64 src; - guint64 dst; + uint64_t src; + uint64_t dst; } TranslateItem; enum { @@ -76,15 +77,15 @@ enum { }; static void -translate_table_clear (GArray **tables, - guint table) +translate_table_clear (GArray **tables, + unsigned int table) { g_clear_pointer (&tables[table], g_array_unref); } -static gint -compare_by_src (gconstpointer a, - gconstpointer b) +static int +compare_by_src (const void *a, + const void *b) { const TranslateItem *itema = a; const TranslateItem *itemb = b; @@ -98,18 +99,18 @@ compare_by_src (gconstpointer a, } static void -translate_table_sort (GArray **tables, - guint table) +translate_table_sort (GArray **tables, + unsigned int table) { if (tables[table]) g_array_sort (tables[table], compare_by_src); } static void -translate_table_add (GArray **tables, - guint table, - guint64 src, - guint64 dst) +translate_table_add (GArray **tables, + unsigned int table, + uint64_t src, + uint64_t dst) { const TranslateItem item = { src, dst }; @@ -119,10 +120,10 @@ translate_table_add (GArray **tables, g_array_append_val (tables[table], item); } -static guint64 -translate_table_translate (GArray **tables, - guint table, - guint64 src) +static uint64_t +translate_table_translate (GArray **tables, + unsigned int table, + uint64_t src) { const TranslateItem *item; TranslateItem key = { src, 0 }; @@ -152,9 +153,9 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, { GArray *tables[N_TRANSLATE] = { NULL }; SysprofCaptureFrameType type; - gint64 start_time; - gint64 first_start_time = G_MAXINT64; - gint64 end_time = -1; + int64_t start_time; + int64_t first_start_time = INT64_MAX; + int64_t end_time = -1; g_return_val_if_fail (self != NULL, FALSE); g_return_val_if_fail (reader != NULL, FALSE); @@ -176,8 +177,8 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, { g_autoptr(GHashTable) jitmap = NULL; GHashTableIter iter; - const gchar *name; - guint64 addr; + const char *name; + uint64_t addr; if (type != SYSPROF_CAPTURE_FRAME_JITMAP) { @@ -192,7 +193,7 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, g_hash_table_iter_init (&iter, jitmap); while (g_hash_table_iter_next (&iter, (gpointer *)&addr, (gpointer *)&name)) { - guint64 replace = sysprof_capture_writer_add_jitmap (self, name); + uint64_t replace = sysprof_capture_writer_add_jitmap (self, name); /* We need to keep a table of replacement addresses so that * we can translate the samples into the destination address * space that we synthesized for the address identifier. @@ -378,7 +379,7 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, { SysprofCaptureAddress addrs[frame->n_addrs]; - for (guint z = 0; z < frame->n_addrs; z++) + for (unsigned int z = 0; z < frame->n_addrs; z++) addrs[z] = translate_table_translate (tables, TRANSLATE_ADDR, frame->addrs[z]); sysprof_capture_writer_add_sample (self, @@ -403,10 +404,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, { g_autoptr(GArray) counter = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounter)); - for (guint z = 0; z < frame->n_counters; z++) + for (unsigned int z = 0; z < frame->n_counters; z++) { SysprofCaptureCounter c = frame->counters[z]; - guint src = c.id; + unsigned int src = c.id; c.id = sysprof_capture_writer_request_counter (self, 1); @@ -440,15 +441,15 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, g_autoptr(GArray) ids = g_array_new (FALSE, FALSE, sizeof (guint)); g_autoptr(GArray) values = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounterValue)); - for (guint z = 0; z < frame->n_values; z++) + for (unsigned int z = 0; z < frame->n_values; z++) { const SysprofCaptureCounterValues *v = &frame->values[z]; - for (guint y = 0; y < G_N_ELEMENTS (v->ids); y++) + for (unsigned int y = 0; y < G_N_ELEMENTS (v->ids); y++) { if (v->ids[y]) { - guint dst = translate_table_translate (tables, TRANSLATE_CTR, v->ids[y]); + unsigned int dst = translate_table_translate (tables, TRANSLATE_CTR, v->ids[y]); SysprofCaptureCounterValue value = v->values[y]; g_array_append_val (ids, dst); @@ -463,8 +464,8 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, frame->frame.time, frame->frame.cpu, frame->frame.pid, - (const guint *)(gpointer)ids->data, - (const SysprofCaptureCounterValue *)(gpointer)values->data, + (const unsigned int *)(void *)ids->data, + (const SysprofCaptureCounterValue *)(void *)values->data, ids->len); } diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 8e4c5a61..57c176c2 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -65,6 +65,7 @@ #include #include #include +#include #include #include #include @@ -83,10 +84,10 @@ typedef struct { /* A pinter into the string buffer */ - const gchar *str; + const char *str; /* The unique address for the string */ - guint64 addr; + uint64_t addr; } SysprofCaptureJitmapBucket; struct _SysprofCaptureWriter @@ -98,7 +99,7 @@ struct _SysprofCaptureWriter * * This is paired with a closed hash table for deduplication. */ - gchar addr_buf[4096*4]; + char addr_buf[4096*4]; /* Our hashtable for deduplication. */ SysprofCaptureJitmapBucket addr_hash[512]; @@ -107,38 +108,38 @@ struct _SysprofCaptureWriter * alinged for the write buffer. This improves the performance of large * writes to the target file-descriptor. */ - volatile gint ref_count; + volatile int ref_count; /* * Our address sequence counter. The value that comes from * monotonically increasing this is OR'd with JITMAP_MARK to denote * the address name should come from the JIT map. */ - gsize addr_seq; + size_t addr_seq; /* Our position in addr_buf. */ - gsize addr_buf_pos; + size_t addr_buf_pos; /* * The number of hash table items in @addr_hash. This is an * optimization so that we can avoid calculating the number of strings * when flushing out the jitmap. */ - guint addr_hash_size; + unsigned int addr_hash_size; /* Capture file handle */ int fd; /* Our write buffer for fd */ - guint8 *buf; - gsize pos; - gsize len; + uint8_t *buf; + size_t pos; + size_t len; /* GSource for periodic flush */ GSource *periodic_flush; /* counter id sequence */ - gint next_counter_id; + int next_counter_id; /* Statistics while recording */ SysprofCaptureStat stat; @@ -146,10 +147,10 @@ struct _SysprofCaptureWriter static inline void sysprof_capture_writer_frame_init (SysprofCaptureFrame *frame_, - gint len, - gint cpu, - gint32 pid, - gint64 time_, + int len, + int cpu, + int32_t pid, + int64_t time_, SysprofCaptureFrameType type) { g_assert (frame_ != NULL); @@ -207,9 +208,9 @@ sysprof_capture_writer_unref (SysprofCaptureWriter *self) static gboolean sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) { - const guint8 *buf; - gssize written; - gsize to_write; + const uint8_t *buf; + ssize_t written; + size_t to_write; g_assert (self != NULL); g_assert (self->pos <= self->len); @@ -230,7 +231,7 @@ sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) if (written == 0 && errno != EAGAIN) return FALSE; - g_assert (written <= (gssize)to_write); + g_assert (written <= (ssize_t)to_write); buf += written; to_write -= written; @@ -242,17 +243,17 @@ sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) } static inline void -sysprof_capture_writer_realign (gsize *pos) +sysprof_capture_writer_realign (size_t *pos) { *pos = (*pos + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1); } static inline gboolean sysprof_capture_writer_ensure_space_for (SysprofCaptureWriter *self, - gsize len) + size_t len) { /* Check for max frame size */ - if (len > G_MAXUSHORT) + if (len > USHRT_MAX) return FALSE; if ((self->len - self->pos) < len) @@ -264,11 +265,11 @@ sysprof_capture_writer_ensure_space_for (SysprofCaptureWriter *self, return TRUE; } -static inline gpointer +static inline void * sysprof_capture_writer_allocate (SysprofCaptureWriter *self, - gsize *len) + size_t *len) { - gpointer p; + void *p; g_assert (self != NULL); g_assert (len != NULL); @@ -279,7 +280,7 @@ sysprof_capture_writer_allocate (SysprofCaptureWriter *self, if (!sysprof_capture_writer_ensure_space_for (self, *len)) return NULL; - p = (gpointer)&self->buf[self->pos]; + p = (void *)&self->buf[self->pos]; self->pos += *len; @@ -292,8 +293,8 @@ static gboolean sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) { SysprofCaptureJitmap jitmap; - gssize r; - gsize len; + ssize_t r; + size_t len; g_assert (self != NULL); @@ -318,7 +319,7 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) return FALSE; r = _sysprof_write (self->fd, self->addr_buf, len - sizeof jitmap); - if (r < 0 || (gsize)r != (len - sizeof jitmap)) + if (r < 0 || (size_t)r != (len - sizeof jitmap)) return FALSE; self->addr_buf_pos = 0; @@ -332,11 +333,11 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) static gboolean sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, - const gchar *name, + const char *name, SysprofCaptureAddress *addr) { - guint hash; - guint i; + unsigned int hash; + unsigned int i; g_assert (self != NULL); g_assert (name != NULL); @@ -377,13 +378,13 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, static SysprofCaptureAddress sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, - const gchar *str) + const char *str) { SysprofCaptureAddress addr; - gchar *dst; - gsize len; - guint hash; - guint i; + char *dst; + size_t len; + unsigned int hash; + unsigned int i; g_assert (self != NULL); g_assert (str != NULL); @@ -408,7 +409,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, addr = SYSPROF_CAPTURE_JITMAP_MARK | ++self->addr_seq; /* Copy the address into the buffer */ - dst = (gchar *)&self->addr_buf[self->addr_buf_pos]; + dst = (char *)&self->addr_buf[self->addr_buf_pos]; memcpy (dst, &addr, sizeof addr); /* @@ -459,14 +460,14 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, } SysprofCaptureWriter * -sysprof_capture_writer_new_from_fd (int fd, - gsize buffer_size) +sysprof_capture_writer_new_from_fd (int fd, + size_t buffer_size) { g_autofree gchar *nowstr = NULL; g_autoptr(GDateTime) now = NULL; SysprofCaptureWriter *self; SysprofCaptureFileHeader *header; - gsize header_len = sizeof(*header); + size_t header_len = sizeof(*header); if (fd < 0) return NULL; @@ -538,8 +539,8 @@ sysprof_capture_writer_new_from_fd (int fd, } SysprofCaptureWriter * -sysprof_capture_writer_new (const gchar *filename, - gsize buffer_size) +sysprof_capture_writer_new (const char *filename, + size_t buffer_size) { SysprofCaptureWriter *self; int fd; @@ -561,17 +562,17 @@ sysprof_capture_writer_new (const gchar *filename, gboolean sysprof_capture_writer_add_map (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - guint64 start, - guint64 end, - guint64 offset, - guint64 inode, - const gchar *filename) + int64_t time, + int cpu, + int32_t pid, + uint64_t start, + uint64_t end, + uint64_t offset, + uint64_t inode, + const char *filename) { SysprofCaptureMap *ev; - gsize len; + size_t len; if (filename == NULL) filename = ""; @@ -606,17 +607,17 @@ sysprof_capture_writer_add_map (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - guint64 duration, - const gchar *group, - const gchar *name, - const gchar *message) + int64_t time, + int cpu, + int32_t pid, + uint64_t duration, + const char *group, + const char *name, + const char *message) { SysprofCaptureMark *ev; - gsize message_len; - gsize len; + size_t message_len; + size_t len; g_assert (self != NULL); g_assert (name != NULL); @@ -650,15 +651,15 @@ sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *id, - const gchar *metadata, - gssize metadata_len) + int64_t time, + int cpu, + int32_t pid, + const char *id, + const char *metadata, + ssize_t metadata_len) { SysprofCaptureMetadata *ev; - gsize len; + size_t len; g_assert (self != NULL); g_assert (id != NULL); @@ -695,7 +696,7 @@ sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, SysprofCaptureAddress sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self, - const gchar *name) + const char *name) { SysprofCaptureAddress addr = INVALID_ADDRESS; @@ -713,13 +714,13 @@ sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_process (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *cmdline) + int64_t time, + int cpu, + int32_t pid, + const char *cmdline) { SysprofCaptureProcess *ev; - gsize len; + size_t len; if (cmdline == NULL) cmdline = ""; @@ -750,15 +751,15 @@ sysprof_capture_writer_add_process (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 tid, + int64_t time, + int cpu, + int32_t pid, + int32_t tid, const SysprofCaptureAddress *addrs, - guint n_addrs) + unsigned int n_addrs) { SysprofCaptureSample *ev; - gsize len; + size_t len; g_assert (self != NULL); @@ -786,13 +787,13 @@ sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 child_pid) + int64_t time, + int cpu, + int32_t pid, + int32_t child_pid) { SysprofCaptureFork *ev; - gsize len = sizeof *ev; + size_t len = sizeof *ev; g_assert (self != NULL); @@ -815,12 +816,12 @@ sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid) + int64_t time, + int cpu, + int32_t pid) { SysprofCaptureExit *ev; - gsize len = sizeof *ev; + size_t len = sizeof *ev; g_assert (self != NULL); @@ -842,12 +843,12 @@ sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid) + int64_t time, + int cpu, + int32_t pid) { SysprofCaptureTimestamp *ev; - gsize len = sizeof *ev; + size_t len = sizeof *ev; g_assert (self != NULL); @@ -870,7 +871,7 @@ sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, static gboolean sysprof_capture_writer_flush_end_time (SysprofCaptureWriter *self) { - gint64 end_time = SYSPROF_CAPTURE_CURRENT_TIME; + int64_t end_time = SYSPROF_CAPTURE_CURRENT_TIME; ssize_t ret; g_assert (self != NULL); @@ -914,10 +915,10 @@ sysprof_capture_writer_flush (SysprofCaptureWriter *self) */ gboolean sysprof_capture_writer_save_as (SysprofCaptureWriter *self, - const gchar *filename, + const char *filename, GError **error) { - gsize to_write; + size_t to_write; off_t in_off; off_t pos; int fd = -1; @@ -940,7 +941,7 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter *self, while (to_write > 0) { - gssize written; + ssize_t written; written = _sysprof_sendfile (fd, self->fd, &in_off, pos); @@ -950,7 +951,7 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter *self, if (written == 0 && errno != EAGAIN) goto handle_errno; - g_assert (written <= (gssize)to_write); + g_assert (written <= (ssize_t)to_write); to_write -= written; } @@ -997,7 +998,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, { struct stat stbuf; off_t in_off; - gsize to_write; + size_t to_write; g_assert (self != NULL); g_assert (self->fd != -1); @@ -1019,7 +1020,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, while (to_write > 0) { - gssize written; + ssize_t written; written = _sysprof_sendfile (self->fd, fd, &in_off, to_write); @@ -1029,7 +1030,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, if (written == 0 && errno != EAGAIN) goto handle_errno; - g_assert (written <= (gssize)to_write); + g_assert (written <= (ssize_t)to_write); to_write -= written; } @@ -1166,15 +1167,15 @@ sysprof_capture_writer_stat (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, + int64_t time, + int cpu, + int32_t pid, const SysprofCaptureCounter *counters, - guint n_counters) + unsigned int n_counters) { SysprofCaptureCounterDefine *def; - gsize len; - guint i; + size_t len; + unsigned int i; g_assert (self != NULL); g_assert (counters != NULL); @@ -1216,19 +1217,19 @@ sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const guint *counters_ids, + int64_t time, + int cpu, + int32_t pid, + const unsigned int *counters_ids, const SysprofCaptureCounterValue *values, - guint n_counters) + unsigned int n_counters) { SysprofCaptureCounterSet *set; - gsize len; - guint n_groups; - guint group; - guint field; - guint i; + size_t len; + unsigned int n_groups; + unsigned int group; + unsigned int field; + unsigned int i; g_assert (self != NULL); g_assert (counters_ids != NULL || n_counters == 0); @@ -1292,11 +1293,11 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, * * Returns: The next series of counter values or zero on failure. */ -guint +unsigned int sysprof_capture_writer_request_counter (SysprofCaptureWriter *self, - guint n_counters) + unsigned int n_counters) { - gint ret; + int ret; g_assert (self != NULL); @@ -1311,8 +1312,8 @@ sysprof_capture_writer_request_counter (SysprofCaptureWriter *self, gboolean _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, - gint64 start_time, - gint64 end_time) + int64_t start_time, + int64_t end_time) { ssize_t ret; @@ -1340,9 +1341,9 @@ do_end: } SysprofCaptureWriter * -sysprof_capture_writer_new_from_env (gsize buffer_size) +sysprof_capture_writer_new_from_env (size_t buffer_size) { - const gchar *fdstr; + const char *fdstr; int fd; if (!(fdstr = g_getenv ("SYSPROF_TRACE_FD"))) @@ -1361,7 +1362,7 @@ sysprof_capture_writer_new_from_env (gsize buffer_size) return sysprof_capture_writer_new_from_fd (dup (fd), buffer_size); } -gsize +size_t sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self) { g_return_val_if_fail (self != NULL, 0); @@ -1371,16 +1372,16 @@ sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self) gboolean sysprof_capture_writer_add_log (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - GLogLevelFlags severity, - const gchar *domain, - const gchar *message) + int64_t time, + int cpu, + int32_t pid, + int severity, + const char *domain, + const char *message) { SysprofCaptureLog *ev; - gsize message_len; - gsize len; + size_t message_len; + size_t len; g_assert (self != NULL); @@ -1416,16 +1417,16 @@ sysprof_capture_writer_add_log (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_file (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *path, - gboolean is_last, - const guint8 *data, - gsize data_len) + int64_t time, + int cpu, + int32_t pid, + const char *path, + bool is_last, + const uint8_t *data, + size_t data_len) { SysprofCaptureFileChunk *ev; - gsize len; + size_t len; g_assert (self != NULL); @@ -1454,20 +1455,20 @@ sysprof_capture_writer_add_file (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *path, - gint fd) + int64_t time, + int cpu, + int32_t pid, + const char *path, + int fd) { - guint8 data[(4096*4L) - sizeof(SysprofCaptureFileChunk)]; + uint8_t data[(4096*4L) - sizeof(SysprofCaptureFileChunk)]; g_assert (self != NULL); for (;;) { gboolean is_last; - gssize n_read; + ssize_t n_read; again: n_read = read (fd, data, sizeof data); @@ -1524,18 +1525,18 @@ sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 tid, + int64_t time, + int cpu, + int32_t pid, + int32_t tid, SysprofCaptureAddress alloc_addr, - gint64 alloc_size, + int64_t alloc_size, SysprofBacktraceFunc backtrace_func, - gpointer backtrace_data) + void *backtrace_data) { SysprofCaptureAllocation *ev; - gsize len; - guint n_addrs; + size_t len; + unsigned int n_addrs; g_assert (self != NULL); g_assert (backtrace_func != NULL); @@ -1565,7 +1566,7 @@ sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, if (ev->n_addrs < MAX_UNWIND_DEPTH) { - gsize diff = (sizeof (SysprofCaptureAddress) * (MAX_UNWIND_DEPTH - ev->n_addrs)); + size_t diff = (sizeof (SysprofCaptureAddress) * (MAX_UNWIND_DEPTH - ev->n_addrs)); ev->frame.len -= diff; self->pos -= diff; @@ -1578,17 +1579,17 @@ sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, gboolean sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 tid, + int64_t time, + int cpu, + int32_t pid, + int32_t tid, SysprofCaptureAddress alloc_addr, - gint64 alloc_size, + int64_t alloc_size, const SysprofCaptureAddress *addrs, - guint n_addrs) + unsigned int n_addrs) { SysprofCaptureAllocation *ev; - gsize len; + size_t len; g_assert (self != NULL); @@ -1624,8 +1625,8 @@ gboolean _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, const SysprofCaptureFrame *fr) { - gpointer begin; - gsize len; + void *begin; + size_t len; g_assert (self != NULL); g_assert ((fr->len & 0x7) == 0); diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index 409e88b4..a20bb822 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -56,6 +56,9 @@ #pragma once +#include +#include + #include "sysprof-capture-types.h" #include "sysprof-version-macros.h" @@ -72,20 +75,20 @@ typedef struct _SysprofCaptureWriter SysprofCaptureWriter; * * Returns: the number of addresses filled in @addrs */ -typedef gint (*SysprofBacktraceFunc) (SysprofCaptureAddress *addrs, - guint n_addrs, - gpointer user_data); +typedef int (*SysprofBacktraceFunc) (SysprofCaptureAddress *addrs, + unsigned int n_addrs, + void *user_data); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureWriter *sysprof_capture_writer_new_from_env (gsize buffer_size); +SysprofCaptureWriter *sysprof_capture_writer_new_from_env (size_t buffer_size); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureWriter *sysprof_capture_writer_new (const gchar *filename, - gsize buffer_size); +SysprofCaptureWriter *sysprof_capture_writer_new (const char *filename, + size_t buffer_size); SYSPROF_AVAILABLE_IN_ALL SysprofCaptureWriter *sysprof_capture_writer_new_from_fd (int fd, - gsize buffer_size); + size_t buffer_size); SYSPROF_AVAILABLE_IN_ALL -gsize sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self); +size_t sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self); SYSPROF_AVAILABLE_IN_ALL SysprofCaptureWriter *sysprof_capture_writer_ref (SysprofCaptureWriter *self); SYSPROF_AVAILABLE_IN_ALL @@ -96,135 +99,135 @@ void sysprof_capture_writer_stat (Sy SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self, GMainContext *main_context, - guint timeout_seconds); + unsigned int timeout_seconds); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_file (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *path, + int64_t time, + int cpu, + int32_t pid, + const char *path, gboolean is_last, - const guint8 *data, - gsize data_len); + const uint8_t *data, + size_t data_len); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *path, - gint fd); + int64_t time, + int cpu, + int32_t pid, + const char *path, + int fd); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_map (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - guint64 start, - guint64 end, - guint64 offset, - guint64 inode, - const gchar *filename); + int64_t time, + int cpu, + int32_t pid, + uint64_t start, + uint64_t end, + uint64_t offset, + uint64_t inode, + const char *filename); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - guint64 duration, - const gchar *group, - const gchar *name, - const gchar *message); + int64_t time, + int cpu, + int32_t pid, + uint64_t duration, + const char *group, + const char *name, + const char *message); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *id, - const gchar *metadata, - gssize metadata_len); + int64_t time, + int cpu, + int32_t pid, + const char *id, + const char *metadata, + ssize_t metadata_len); SYSPROF_AVAILABLE_IN_ALL -guint64 sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self, - const gchar *name); +uint64_t sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self, + const char *name); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_process (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const gchar *cmdline); + int64_t time, + int cpu, + int32_t pid, + const char *cmdline); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 tid, + int64_t time, + int cpu, + int32_t pid, + int32_t tid, const SysprofCaptureAddress *addrs, - guint n_addrs); + unsigned int n_addrs); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 child_pid); + int64_t time, + int cpu, + int32_t pid, + int32_t child_pid); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid); + int64_t time, + int cpu, + int32_t pid); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid); + int64_t time, + int cpu, + int32_t pid); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, + int64_t time, + int cpu, + int32_t pid, const SysprofCaptureCounter *counters, - guint n_counters); + unsigned int n_counters); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - const guint *counters_ids, + int64_t time, + int cpu, + int32_t pid, + const unsigned int *counters_ids, const SysprofCaptureCounterValue *values, - guint n_counters); + unsigned int n_counters); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_add_log (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - GLogLevelFlags severity, - const gchar *domain, - const gchar *message); + int64_t time, + int cpu, + int32_t pid, + int severity, + const char *domain, + const char *message); SYSPROF_AVAILABLE_IN_3_36 gboolean sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 tid, + int64_t time, + int cpu, + int32_t pid, + int32_t tid, SysprofCaptureAddress alloc_addr, - gint64 alloc_size, + int64_t alloc_size, SysprofBacktraceFunc backtrace_func, - gpointer backtrace_data); + void *backtrace_data); SYSPROF_AVAILABLE_IN_3_36 gboolean sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, - gint64 time, - gint cpu, - gint32 pid, - gint32 tid, + int64_t time, + int cpu, + int32_t pid, + int32_t tid, SysprofCaptureAddress alloc_addr, - gint64 alloc_size, + int64_t alloc_size, const SysprofCaptureAddress *addrs, - guint n_addrs); + unsigned int n_addrs); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_flush (SysprofCaptureWriter *self); SYSPROF_AVAILABLE_IN_ALL gboolean sysprof_capture_writer_save_as (SysprofCaptureWriter *self, - const gchar *filename, + const char *filename, GError **error); SYSPROF_AVAILABLE_IN_ALL -guint sysprof_capture_writer_request_counter (SysprofCaptureWriter *self, - guint n_counters); +unsigned int sysprof_capture_writer_request_counter (SysprofCaptureWriter *self, + unsigned int n_counters); SYSPROF_AVAILABLE_IN_ALL SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, GError **error); @@ -245,8 +248,8 @@ gboolean _sysprof_capture_writer_splice_from_fd (Sy GError **error) G_GNUC_INTERNAL; G_GNUC_INTERNAL gboolean _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, - gint64 start_time, - gint64 end_time) G_GNUC_INTERNAL; + int64_t start_time, + int64_t end_time) G_GNUC_INTERNAL; G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-clock.c b/src/libsysprof-capture/sysprof-clock.c index 066605c3..f517deea 100644 --- a/src/libsysprof-capture/sysprof-clock.c +++ b/src/libsysprof-capture/sysprof-clock.c @@ -60,12 +60,12 @@ #include "sysprof-clock.h" -gint sysprof_clock = -1; +int sysprof_clock = -1; void sysprof_clock_init (void) { - static const gint clock_ids[] = { + static const int clock_ids[] = { CLOCK_MONOTONIC, CLOCK_MONOTONIC_RAW, #ifdef __linux__ @@ -78,7 +78,7 @@ sysprof_clock_init (void) if (sysprof_clock != -1) return; - for (guint i = 0; i < G_N_ELEMENTS (clock_ids); i++) + for (unsigned int i = 0; i < G_N_ELEMENTS (clock_ids); i++) { struct timespec ts; int clock_id = clock_ids [i]; diff --git a/src/libsysprof-capture/sysprof-clock.h b/src/libsysprof-capture/sysprof-clock.h index ac101a33..fba81a4f 100644 --- a/src/libsysprof-capture/sysprof-clock.h +++ b/src/libsysprof-capture/sysprof-clock.h @@ -57,15 +57,16 @@ #pragma once #include +#include #include #include "sysprof-version-macros.h" G_BEGIN_DECLS -typedef gint SysprofClock; -typedef gint64 SysprofTimeStamp; -typedef gint32 SysprofTimeSysprofan; +typedef int SysprofClock; +typedef int64_t SysprofTimeStamp; +typedef int32_t SysprofTimeSysprofan; #define SYSPROF_NSEC_PER_SEC G_GINT64_CONSTANT(1000000000) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 064586de..29e74318 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -87,10 +87,10 @@ typedef struct int pid; } SysprofCollector; -#define COLLECTOR_INVALID ((gpointer)&invalid) +#define COLLECTOR_INVALID ((void *)&invalid) static MappedRingBuffer *request_writer (void); -static void sysprof_collector_free (gpointer data); +static void sysprof_collector_free (void *data); static const SysprofCollector *sysprof_collector_get (void); static G_LOCK_DEFINE (control_fd); @@ -115,8 +115,8 @@ _do_getcpu (void) #endif } -static inline gsize -realign (gsize size) +static inline size_t +realign (size_t size) { return (size + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1); } @@ -129,7 +129,7 @@ request_writer (void) if (conn == NULL) { - const gchar *fdstr = g_getenv ("SYSPROF_CONTROL_FD"); + const char *fdstr = g_getenv ("SYSPROF_CONTROL_FD"); int peer_fd = -1; if (fdstr != NULL) @@ -166,7 +166,7 @@ request_writer (void) if (g_output_stream_write_all (G_OUTPUT_STREAM (out_stream), CREATRING, CREATRING_LEN, &len, NULL, NULL) && len == CREATRING_LEN) { - gint ring_fd = g_unix_connection_receive_fd (conn, NULL, NULL); + int ring_fd = g_unix_connection_receive_fd (conn, NULL, NULL); if (ring_fd > -1) { @@ -198,7 +198,7 @@ write_final_frame (MappedRingBuffer *ring) } static void -sysprof_collector_free (gpointer data) +sysprof_collector_free (void *data) { SysprofCollector *collector = data; @@ -263,7 +263,7 @@ sysprof_collector_get (void) void sysprof_collector_init (void) { - static gsize once_init; + static size_t once_init; if (g_once_init_enter (&once_init)) { @@ -293,19 +293,19 @@ sysprof_collector_init (void) void sysprof_collector_allocate (SysprofCaptureAddress alloc_addr, - gint64 alloc_size, + int64_t alloc_size, SysprofBacktraceFunc backtrace_func, - gpointer backtrace_data) + void *backtrace_data) { COLLECTOR_BEGIN { SysprofCaptureAllocation *ev; - gsize len; + size_t len; len = sizeof *ev + (sizeof (SysprofCaptureAllocation) * MAX_UNWIND_DEPTH); if ((ev = mapped_ring_buffer_allocate (collector->buffer, len))) { - gint n_addrs; + int n_addrs; /* First take a backtrace, so that backtrace_func() can overwrite * a little bit of data *BEFORE* ev->addrs as stratch space. This @@ -338,18 +338,18 @@ sysprof_collector_allocate (SysprofCaptureAddress alloc_addr, } void -sysprof_collector_sample (SysprofBacktraceFunc backtrace_func, - gpointer backtrace_data) +sysprof_collector_sample (SysprofBacktraceFunc backtrace_func, + void *backtrace_data) { COLLECTOR_BEGIN { SysprofCaptureSample *ev; - gsize len; + size_t len; len = sizeof *ev + (sizeof (SysprofCaptureSample) * MAX_UNWIND_DEPTH); if ((ev = mapped_ring_buffer_allocate (collector->buffer, len))) { - gint n_addrs; + int n_addrs; /* See comment from sysprof_collector_allocate(). */ if (backtrace_func) @@ -373,16 +373,16 @@ sysprof_collector_sample (SysprofBacktraceFunc backtrace_func, } void -sysprof_collector_mark (gint64 time, - gint64 duration, - const gchar *group, - const gchar *mark, - const gchar *message) +sysprof_collector_mark (int64_t time, + int64_t duration, + const char *group, + const char *mark, + const char *message) { COLLECTOR_BEGIN { SysprofCaptureMark *ev; - gsize len; - gsize sl; + size_t len; + size_t sl; if (group == NULL) group = ""; @@ -416,14 +416,14 @@ sysprof_collector_mark (gint64 time, } void -sysprof_collector_log (GLogLevelFlags severity, - const gchar *domain, - const gchar *message) +sysprof_collector_log (int severity, + const char *domain, + const char *message) { COLLECTOR_BEGIN { SysprofCaptureLog *ev; - gsize len; - gsize sl; + size_t len; + size_t sl; if (domain == NULL) domain = ""; @@ -455,17 +455,17 @@ sysprof_collector_log (GLogLevelFlags severity, } void -sysprof_collector_log_printf (GLogLevelFlags severity, - const gchar *domain, - const gchar *format, +sysprof_collector_log_printf (int severity, + const char *domain, + const char *format, ...) { COLLECTOR_BEGIN { - g_autofree gchar *formatted = NULL; + g_autofree char *formatted = NULL; SysprofCaptureLog *ev; va_list args; - gsize len; - gsize sl; + size_t len; + size_t sl; va_start (args, format); formatted = g_strdup_vprintf (format, args); diff --git a/src/libsysprof-capture/sysprof-collector.h b/src/libsysprof-capture/sysprof-collector.h index f234bc2f..c7ff9a63 100644 --- a/src/libsysprof-capture/sysprof-collector.h +++ b/src/libsysprof-capture/sysprof-collector.h @@ -64,26 +64,26 @@ SYSPROF_AVAILABLE_IN_3_36 void sysprof_collector_init (void); SYSPROF_AVAILABLE_IN_3_36 void sysprof_collector_allocate (SysprofCaptureAddress alloc_addr, - gint64 alloc_size, + int64_t alloc_size, SysprofBacktraceFunc backtrace_func, - gpointer backtrace_data); + void *backtrace_data); SYSPROF_AVAILABLE_IN_3_36 void sysprof_collector_sample (SysprofBacktraceFunc backtrace_func, - gpointer backtrace_data); + void *backtrace_data); SYSPROF_AVAILABLE_IN_3_36 -void sysprof_collector_mark (gint64 time, - gint64 duration, - const gchar *group, - const gchar *mark, - const gchar *message); +void sysprof_collector_mark (int64_t time, + int64_t duration, + const char *group, + const char *mark, + const char *message); SYSPROF_AVAILABLE_IN_3_36 -void sysprof_collector_log (GLogLevelFlags severity, - const gchar *domain, - const gchar *message); +void sysprof_collector_log (int severity, + const char *domain, + const char *message); SYSPROF_AVAILABLE_IN_3_38 -void sysprof_collector_log_printf (GLogLevelFlags severity, - const gchar *domain, - const gchar *format, +void sysprof_collector_log_printf (int severity, + const char *domain, + const char *format, ...) G_GNUC_PRINTF (3, 4); G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-platform.c b/src/libsysprof-capture/sysprof-platform.c index 99c9a55c..51464d19 100644 --- a/src/libsysprof-capture/sysprof-platform.c +++ b/src/libsysprof-capture/sysprof-platform.c @@ -77,7 +77,7 @@ * Returns: An fd if successful; otherwise -1 and errno is set. */ int -sysprof_memfd_create (const gchar *name) +sysprof_memfd_create (const char *name) { #ifdef __NR_memfd_create if (name == NULL) @@ -116,7 +116,7 @@ sysprof_memfd_create (const gchar *name) * * Since: 3.36 */ -gsize +size_t sysprof_getpagesize (void) { return _sysprof_getpagesize (); diff --git a/src/libsysprof-capture/sysprof-platform.h b/src/libsysprof-capture/sysprof-platform.h index 6dbe4ff2..3f0b3768 100644 --- a/src/libsysprof-capture/sysprof-platform.h +++ b/src/libsysprof-capture/sysprof-platform.h @@ -56,13 +56,16 @@ #pragma once +#include +#include + #include "sysprof-version-macros.h" G_BEGIN_DECLS SYSPROF_AVAILABLE_IN_ALL -int sysprof_memfd_create (const gchar *desc); +int sysprof_memfd_create (const char *desc); SYSPROF_AVAILABLE_IN_3_36 -gsize sysprof_getpagesize (void); +size_t sysprof_getpagesize (void); G_END_DECLS From 5636bbf4f0f886853ae79430a24c44c7f6eb711b Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 15:35:51 +0100 Subject: [PATCH 11/62] libsysprof-capture: Use stdbool instead of gboolean Another step towards dropping GLib as a dependency of libsysprof-capture. Unlike the previous commit which replaced GLib integer types with the bitwise equivalent C standard types, `stdbool` is potentially a different width from `gboolean`, so this is an ABI break. It therefore involves some changes to callback functions in the tests and tools, and in libsysprof. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 12 +- src/libsysprof-capture/mapped-ring-buffer.h | 11 +- src/libsysprof-capture/sysprof-address.c | 17 +- src/libsysprof-capture/sysprof-address.h | 3 +- .../sysprof-capture-condition.c | 21 ++- .../sysprof-capture-condition.h | 4 +- .../sysprof-capture-cursor.h | 6 +- .../sysprof-capture-reader.c | 64 +++---- .../sysprof-capture-reader.h | 18 +- .../sysprof-capture-writer-cat.c | 7 +- .../sysprof-capture-writer.c | 178 +++++++++--------- .../sysprof-capture-writer.h | 47 ++--- src/libsysprof-capture/sysprof-clock.c | 2 + src/libsysprof-capture/sysprof-collector.c | 5 +- src/libsysprof/sysprof-control-source.c | 8 +- src/libsysprof/sysprof-local-profiler.c | 6 +- src/libsysprof/sysprof-memprof-profile.c | 10 +- src/tests/test-capture-cursor.c | 6 +- src/tests/test-mapped-ring-buffer.c | 24 +-- src/tools/list-threads.c | 6 +- 20 files changed, 235 insertions(+), 220 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index a364ba8d..76d30a65 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -478,7 +478,7 @@ mapped_ring_buffer_advance (MappedRingBuffer *self, * Returns: %TRUE if the buffer was drained, %FALSE if @callback prematurely * returned while draining. */ -gboolean +bool mapped_ring_buffer_drain (MappedRingBuffer *self, MappedRingBufferCallback callback, void *user_data) @@ -499,7 +499,7 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, g_assert (tailpos < self->body_size); if (headpos == tailpos) - return TRUE; + return true; /* If head needs to wrap around to get to tail, we can just rely on * our double mapping instead actually manually wrapping/copying data. @@ -515,10 +515,10 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, size_t len = tailpos - headpos; if (!callback (data, &len, user_data)) - return FALSE; + return false; if (len > (tailpos - headpos)) - return FALSE; + return false; headpos += len; @@ -528,7 +528,7 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, g_atomic_int_set (&header->head, headpos); } - return TRUE; + return true; } /** @@ -542,7 +542,7 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, * * Returns: %TRUE if the buffer is empty, %FALSE otherwise */ -gboolean +bool mapped_ring_buffer_is_empty (MappedRingBuffer *self) { MappedRingHeader *header; diff --git a/src/libsysprof-capture/mapped-ring-buffer.h b/src/libsysprof-capture/mapped-ring-buffer.h index 15d9a00e..0607aab0 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.h +++ b/src/libsysprof-capture/mapped-ring-buffer.h @@ -21,6 +21,7 @@ #pragma once #include +#include #include G_BEGIN_DECLS @@ -50,9 +51,9 @@ typedef struct _MappedRingBuffer MappedRingBuffer; * * Returns: %TRUE to coninue draining, otherwise %FALSE and draining stops */ -typedef gboolean (*MappedRingBufferCallback) (const void *data, - size_t *length, - void *user_data); +typedef bool (*MappedRingBufferCallback) (const void *data, + size_t *length, + void *user_data); G_GNUC_INTERNAL MappedRingBuffer *mapped_ring_buffer_new_reader (size_t buffer_size); @@ -75,10 +76,10 @@ G_GNUC_INTERNAL void mapped_ring_buffer_advance (MappedRingBuffer *self, size_t length); G_GNUC_INTERNAL -gboolean mapped_ring_buffer_drain (MappedRingBuffer *self, +bool mapped_ring_buffer_drain (MappedRingBuffer *self, MappedRingBufferCallback callback, void *user_data); G_GNUC_INTERNAL -gboolean mapped_ring_buffer_is_empty (MappedRingBuffer *self); +bool mapped_ring_buffer_is_empty (MappedRingBuffer *self); G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-address.c b/src/libsysprof-capture/sysprof-address.c index 8d2fc06f..0ddb68ce 100644 --- a/src/libsysprof-capture/sysprof-address.c +++ b/src/libsysprof-capture/sysprof-address.c @@ -61,10 +61,11 @@ #else # include "sysprof-address-fallback.h" #endif +#include #include "sysprof-address.h" -gboolean +bool sysprof_address_is_context_switch (SysprofAddress address, SysprofAddressContext *context) { @@ -77,31 +78,31 @@ sysprof_address_is_context_switch (SysprofAddress address, { case PERF_CONTEXT_HV: *context = SYSPROF_ADDRESS_CONTEXT_HYPERVISOR; - return TRUE; + return true; case PERF_CONTEXT_KERNEL: *context = SYSPROF_ADDRESS_CONTEXT_KERNEL; - return TRUE; + return true; case PERF_CONTEXT_USER: *context = SYSPROF_ADDRESS_CONTEXT_USER; - return TRUE; + return true; case PERF_CONTEXT_GUEST: *context = SYSPROF_ADDRESS_CONTEXT_GUEST; - return TRUE; + return true; case PERF_CONTEXT_GUEST_KERNEL: *context = SYSPROF_ADDRESS_CONTEXT_GUEST_KERNEL; - return TRUE; + return true; case PERF_CONTEXT_GUEST_USER: *context = SYSPROF_ADDRESS_CONTEXT_GUEST_USER; - return TRUE; + return true; default: *context = SYSPROF_ADDRESS_CONTEXT_NONE; - return FALSE; + return false; } } diff --git a/src/libsysprof-capture/sysprof-address.h b/src/libsysprof-capture/sysprof-address.h index ece0a99e..3b5ca4e9 100644 --- a/src/libsysprof-capture/sysprof-address.h +++ b/src/libsysprof-capture/sysprof-address.h @@ -56,6 +56,7 @@ #pragma once +#include #include #include "sysprof-version-macros.h" @@ -78,7 +79,7 @@ typedef enum } SysprofAddressContext; SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_address_is_context_switch (SysprofAddress address, +bool sysprof_address_is_context_switch (SysprofAddress address, SysprofAddressContext *context); SYSPROF_AVAILABLE_IN_ALL const char *sysprof_address_context_to_string (SysprofAddressContext context); diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index aaa81262..62f12e1e 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -58,6 +58,7 @@ #include "config.h" +#include #include #include "sysprof-capture-condition.h" @@ -104,7 +105,7 @@ struct _SysprofCaptureCondition } u; }; -gboolean +bool sysprof_capture_condition_match (const SysprofCaptureCondition *self, const SysprofCaptureFrame *frame) { @@ -125,9 +126,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, for (size_t i = 0; i < self->u.where_type_in->len; i++) { if (frame->type == g_array_index (self->u.where_type_in, SysprofCaptureFrameType, i)) - return TRUE; + return true; } - return FALSE; + return false; case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN: return (frame->time >= self->u.where_time_between.begin && frame->time <= self->u.where_time_between.end); @@ -136,9 +137,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, for (size_t i = 0; i < self->u.where_pid_in->len; i++) { if (frame->pid == g_array_index (self->u.where_pid_in, int32_t, i)) - return TRUE; + return true; } - return FALSE; + return false; case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN: if (frame->type == SYSPROF_CAPTURE_FRAME_CTRSET) @@ -159,7 +160,7 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, counter == set->values[j].ids[5] || counter == set->values[j].ids[6] || counter == set->values[j].ids[7]) - return TRUE; + return true; } } } @@ -174,16 +175,16 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, for (unsigned int j = 0; j < def->n_counters; j++) { if (def->counters[j].id == counter) - return TRUE; + return true; } } } - return FALSE; + return false; case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: if (frame->type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK) - return FALSE; + return false; return g_strcmp0 (((const SysprofCaptureFileChunk *)frame)->path, self->u.where_file) == 0; @@ -193,7 +194,7 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, g_assert_not_reached (); - return FALSE; + return false; } static SysprofCaptureCondition * diff --git a/src/libsysprof-capture/sysprof-capture-condition.h b/src/libsysprof-capture/sysprof-capture-condition.h index 8792449e..185df91c 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.h +++ b/src/libsysprof-capture/sysprof-capture-condition.h @@ -56,6 +56,8 @@ #pragma once +#include + #include "sysprof-capture-types.h" #include "sysprof-version-macros.h" @@ -88,7 +90,7 @@ SysprofCaptureCondition *sysprof_capture_condition_new_where_counter_in (unsig SYSPROF_AVAILABLE_IN_ALL SysprofCaptureCondition *sysprof_capture_condition_new_where_file (const char *path); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_condition_match (const SysprofCaptureCondition *self, +bool sysprof_capture_condition_match (const SysprofCaptureCondition *self, const SysprofCaptureFrame *frame); G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-cursor.h b/src/libsysprof-capture/sysprof-capture-cursor.h index 8b2c0598..4635006a 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.h +++ b/src/libsysprof-capture/sysprof-capture-cursor.h @@ -56,6 +56,8 @@ #pragma once +#include + #include "sysprof-capture-types.h" #include "sysprof-version-macros.h" @@ -74,8 +76,8 @@ typedef struct _SysprofCaptureCursor SysprofCaptureCursor; * * Returns: %TRUE if iteration should continue, otherwise %FALSE. */ -typedef gboolean (*SysprofCaptureCursorCallback) (const SysprofCaptureFrame *frame, - void *user_data); +typedef bool (*SysprofCaptureCursorCallback) (const SysprofCaptureFrame *frame, + void *user_data); SYSPROF_AVAILABLE_IN_ALL SysprofCaptureCursor *sysprof_capture_cursor_new (SysprofCaptureReader *reader); diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 4cf634bf..d6505149 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -87,7 +87,7 @@ struct _SysprofCaptureReader unsigned int st_buf_set : 1; }; -static gboolean +static bool sysprof_capture_reader_read_file_header (SysprofCaptureReader *self, SysprofCaptureFileHeader *header, GError **error) @@ -101,7 +101,7 @@ sysprof_capture_reader_read_file_header (SysprofCaptureReader *self, G_FILE_ERROR, g_file_error_from_errno (errno), "%s", g_strerror (errno)); - return FALSE; + return false; } if (header->magic != SYSPROF_CAPTURE_MAGIC) @@ -110,12 +110,12 @@ sysprof_capture_reader_read_file_header (SysprofCaptureReader *self, G_FILE_ERROR, G_FILE_ERROR_FAILED, "Capture file magic does not match"); - return FALSE; + return false; } header->capture_time[sizeof header->capture_time - 1] = '\0'; - return TRUE; + return true; } static void @@ -349,7 +349,7 @@ sysprof_capture_reader_bswap_jitmap (SysprofCaptureReader *self, jitmap->n_jitmaps = GUINT64_SWAP_LE_BE (jitmap->n_jitmaps); } -static gboolean +static bool sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self, size_t len) { @@ -391,7 +391,7 @@ sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self, return (self->len - self->pos) >= len; } -gboolean +bool sysprof_capture_reader_skip (SysprofCaptureReader *self) { SysprofCaptureFrame *frame; @@ -400,28 +400,28 @@ sysprof_capture_reader_skip (SysprofCaptureReader *self) g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); if (!sysprof_capture_reader_ensure_space_for (self, sizeof (SysprofCaptureFrame))) - return FALSE; + return false; frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos]; sysprof_capture_reader_bswap_frame (self, frame); if (frame->len < sizeof (SysprofCaptureFrame)) - return FALSE; + return false; if (!sysprof_capture_reader_ensure_space_for (self, frame->len)) - return FALSE; + return false; frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos]; self->pos += frame->len; if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0) - return FALSE; + return false; - return TRUE; + return true; } -gboolean +bool sysprof_capture_reader_peek_frame (SysprofCaptureReader *self, SysprofCaptureFrame *frame) { @@ -433,7 +433,7 @@ sysprof_capture_reader_peek_frame (SysprofCaptureReader *self, g_assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *real_frame)) - return FALSE; + return false; g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); @@ -453,7 +453,7 @@ sysprof_capture_reader_peek_frame (SysprofCaptureReader *self, return frame->type > 0 && frame->type < SYSPROF_CAPTURE_FRAME_LAST; } -gboolean +bool sysprof_capture_reader_peek_type (SysprofCaptureReader *self, SysprofCaptureFrameType *type) { @@ -463,7 +463,7 @@ sysprof_capture_reader_peek_type (SysprofCaptureReader *self, g_assert (type != NULL); if (!sysprof_capture_reader_peek_frame (self, &frame)) - return FALSE; + return false; *type = frame.type; @@ -953,7 +953,7 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) return set; } -gboolean +bool sysprof_capture_reader_reset (SysprofCaptureReader *self) { g_assert (self != NULL); @@ -962,7 +962,7 @@ sysprof_capture_reader_reset (SysprofCaptureReader *self) self->pos = 0; self->len = 0; - return TRUE; + return true; } SysprofCaptureReader * @@ -986,7 +986,7 @@ sysprof_capture_reader_unref (SysprofCaptureReader *self) sysprof_capture_reader_finalize (self); } -gboolean +bool sysprof_capture_reader_splice (SysprofCaptureReader *self, SysprofCaptureWriter *dest, GError **error) @@ -1002,7 +1002,7 @@ sysprof_capture_reader_splice (SysprofCaptureReader *self, G_FILE_ERROR, g_file_error_from_errno (errno), "%s", g_strerror (errno)); - return FALSE; + return false; } /* @@ -1025,7 +1025,7 @@ sysprof_capture_reader_splice (SysprofCaptureReader *self, * * Returns: %TRUE on success; otherwise %FALSE and @error is set. */ -gboolean +bool sysprof_capture_reader_save_as (SysprofCaptureReader *self, const char *filename, GError **error) @@ -1075,7 +1075,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, close (fd); - return TRUE; + return true; handle_errno: if (fd != -1) @@ -1086,7 +1086,7 @@ handle_errno: g_file_error_from_errno (errno), "%s", g_strerror (errno)); - return FALSE; + return false; } int64_t @@ -1180,16 +1180,16 @@ sysprof_capture_reader_set_stat (SysprofCaptureReader *self, if (st_buf != NULL) { self->st_buf = *st_buf; - self->st_buf_set = TRUE; + self->st_buf_set = true; } else { memset (&self->st_buf, 0, sizeof (self->st_buf)); - self->st_buf_set = FALSE; + self->st_buf_set = false; } } -gboolean +bool sysprof_capture_reader_get_stat (SysprofCaptureReader *self, SysprofCaptureStat *st_buf) { @@ -1284,7 +1284,7 @@ sysprof_capture_reader_list_files (SysprofCaptureReader *self) return (char **)g_ptr_array_free (g_steal_pointer (&ar), FALSE); } -gboolean +bool sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, const char *path, int fd) @@ -1301,13 +1301,13 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, size_t to_write; if (!sysprof_capture_reader_peek_type (self, &type)) - return FALSE; + return false; if (type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK) goto skip; if (!(file = sysprof_capture_reader_read_file (self))) - return FALSE; + return false; if (g_strcmp0 (path, file->path) != 0) goto skip; @@ -1321,10 +1321,10 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, written = _sysprof_write (fd, buf, to_write); if (written < 0) - return FALSE; + return false; if (written == 0 && errno != EAGAIN) - return FALSE; + return false; g_assert (written <= (ssize_t)to_write); @@ -1335,11 +1335,11 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, if (!file->is_last) continue; - return TRUE; + return true; skip: if (!sysprof_capture_reader_skip (self)) - return FALSE; + return false; } g_return_val_if_reached (FALSE); diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index 09348584..d4e3e7df 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -56,6 +56,8 @@ #pragma once +#include + #include "sysprof-capture-types.h" #include "sysprof-version-macros.h" @@ -86,12 +88,12 @@ int64_t sysprof_capture_reader_get_start_time ( SYSPROF_AVAILABLE_IN_ALL int64_t sysprof_capture_reader_get_end_time (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_skip (SysprofCaptureReader *self); +bool sysprof_capture_reader_skip (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_peek_type (SysprofCaptureReader *self, +bool sysprof_capture_reader_peek_type (SysprofCaptureReader *self, SysprofCaptureFrameType *type); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_peek_frame (SysprofCaptureReader *self, +bool sysprof_capture_reader_peek_frame (SysprofCaptureReader *self, SysprofCaptureFrame *frame); SYSPROF_AVAILABLE_IN_ALL const SysprofCaptureLog *sysprof_capture_reader_read_log (SysprofCaptureReader *self); @@ -122,17 +124,17 @@ const SysprofCaptureFileChunk *sysprof_capture_reader_read_file ( SYSPROF_AVAILABLE_IN_3_36 const SysprofCaptureAllocation *sysprof_capture_reader_read_allocation (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_reset (SysprofCaptureReader *self); +bool sysprof_capture_reader_reset (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_splice (SysprofCaptureReader *self, +bool sysprof_capture_reader_splice (SysprofCaptureReader *self, SysprofCaptureWriter *dest, GError **error); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_save_as (SysprofCaptureReader *self, +bool sysprof_capture_reader_save_as (SysprofCaptureReader *self, const char *filename, GError **error); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_get_stat (SysprofCaptureReader *self, +bool sysprof_capture_reader_get_stat (SysprofCaptureReader *self, SysprofCaptureStat *st_buf); SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_reader_set_stat (SysprofCaptureReader *self, @@ -143,7 +145,7 @@ const SysprofCaptureFileChunk *sysprof_capture_reader_find_file ( SYSPROF_AVAILABLE_IN_ALL char **sysprof_capture_reader_list_files (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, +bool sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, const char *path, int fd); diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 504835a6..013db74d 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -59,6 +59,7 @@ #include "config.h" #include +#include #include #include #include @@ -146,7 +147,7 @@ translate_table_translate (GArray **tables, return item != NULL ? item->dst : src; } -gboolean +bool sysprof_capture_writer_cat (SysprofCaptureWriter *self, SysprofCaptureReader *reader, GError **error) @@ -513,7 +514,7 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, translate_table_clear (tables, TRANSLATE_ADDR); translate_table_clear (tables, TRANSLATE_CTR); - return TRUE; + return true; panic: g_set_error (error, @@ -524,5 +525,5 @@ panic: translate_table_clear (tables, TRANSLATE_ADDR); translate_table_clear (tables, TRANSLATE_CTR); - return FALSE; + return false; } diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 57c176c2..c655a681 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -205,7 +205,7 @@ sysprof_capture_writer_unref (SysprofCaptureWriter *self) sysprof_capture_writer_finalize (self); } -static gboolean +static bool sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) { const uint8_t *buf; @@ -217,7 +217,7 @@ sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); if (self->pos == 0) - return TRUE; + return true; buf = self->buf; to_write = self->pos; @@ -226,10 +226,10 @@ sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) { written = _sysprof_write (self->fd, buf, to_write); if (written < 0) - return FALSE; + return false; if (written == 0 && errno != EAGAIN) - return FALSE; + return false; g_assert (written <= (ssize_t)to_write); @@ -239,7 +239,7 @@ sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) self->pos = 0; - return TRUE; + return true; } static inline void @@ -248,21 +248,21 @@ sysprof_capture_writer_realign (size_t *pos) *pos = (*pos + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1); } -static inline gboolean +static inline bool sysprof_capture_writer_ensure_space_for (SysprofCaptureWriter *self, size_t len) { /* Check for max frame size */ if (len > USHRT_MAX) - return FALSE; + return false; if ((self->len - self->pos) < len) { if (!sysprof_capture_writer_flush_data (self)) - return FALSE; + return false; } - return TRUE; + return true; } static inline void * @@ -289,7 +289,7 @@ sysprof_capture_writer_allocate (SysprofCaptureWriter *self, return p; } -static gboolean +static bool sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) { SysprofCaptureJitmap jitmap; @@ -299,7 +299,7 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) g_assert (self != NULL); if (self->addr_hash_size == 0) - return TRUE; + return true; g_assert (self->addr_buf_pos > 0); @@ -316,11 +316,11 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) jitmap.n_jitmaps = self->addr_hash_size; if (sizeof jitmap != _sysprof_write (self->fd, &jitmap, sizeof jitmap)) - return FALSE; + return false; r = _sysprof_write (self->fd, self->addr_buf, len - sizeof jitmap); if (r < 0 || (size_t)r != (len - sizeof jitmap)) - return FALSE; + return false; self->addr_buf_pos = 0; self->addr_hash_size = 0; @@ -328,10 +328,10 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) self->stat.frame_count[SYSPROF_CAPTURE_FRAME_JITMAP]++; - return TRUE; + return true; } -static gboolean +static bool sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, const char *name, SysprofCaptureAddress *addr) @@ -350,12 +350,12 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i]; if (bucket->str == NULL) - return FALSE; + return false; if (strcmp (bucket->str, name) == 0) { *addr = bucket->addr; - return TRUE; + return true; } } @@ -364,16 +364,16 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i]; if (bucket->str == NULL) - return FALSE; + return false; if (strcmp (bucket->str, name) == 0) { *addr = bucket->addr; - return TRUE; + return true; } } - return FALSE; + return false; } static SysprofCaptureAddress @@ -512,9 +512,9 @@ sysprof_capture_writer_new_from_fd (int fd, header->magic = SYSPROF_CAPTURE_MAGIC; header->version = 1; #if G_BYTE_ORDER == G_LITTLE_ENDIAN - header->little_endian = TRUE; + header->little_endian = true; #else - header->little_endian = FALSE; + header->little_endian = false; #endif header->padding = 0; g_strlcpy (header->capture_time, nowstr, sizeof header->capture_time); @@ -560,7 +560,7 @@ sysprof_capture_writer_new (const char *filename, return self; } -gboolean +bool sysprof_capture_writer_add_map (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -584,7 +584,7 @@ sysprof_capture_writer_add_map (SysprofCaptureWriter *self, ev = (SysprofCaptureMap *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -602,10 +602,10 @@ sysprof_capture_writer_add_map (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MAP]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -630,7 +630,7 @@ sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, len = sizeof *ev + message_len; ev = (SysprofCaptureMark *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -646,10 +646,10 @@ sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MARK]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -676,7 +676,7 @@ sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, len = sizeof *ev + metadata_len + 1; ev = (SysprofCaptureMetadata *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -691,7 +691,7 @@ sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_METADATA]++; - return TRUE; + return true; } SysprofCaptureAddress @@ -712,7 +712,7 @@ sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self, return addr; } -gboolean +bool sysprof_capture_writer_add_process (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -732,7 +732,7 @@ sysprof_capture_writer_add_process (SysprofCaptureWriter *self, ev = (SysprofCaptureProcess *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -746,10 +746,10 @@ sysprof_capture_writer_add_process (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_PROCESS]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -767,7 +767,7 @@ sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, ev = (SysprofCaptureSample *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -782,10 +782,10 @@ sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_SAMPLE]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -799,7 +799,7 @@ sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, ev = (SysprofCaptureFork *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -811,10 +811,10 @@ sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_FORK]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -827,7 +827,7 @@ sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, ev = (SysprofCaptureExit *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -838,10 +838,10 @@ sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_EXIT]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -854,7 +854,7 @@ sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, ev = (SysprofCaptureTimestamp *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -865,10 +865,10 @@ sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_TIMESTAMP]++; - return TRUE; + return true; } -static gboolean +static bool sysprof_capture_writer_flush_end_time (SysprofCaptureWriter *self) { int64_t end_time = SYSPROF_CAPTURE_CURRENT_TIME; @@ -887,10 +887,10 @@ again: if (ret < 0 && errno == EAGAIN) goto again; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_flush (SysprofCaptureWriter *self) { g_assert (self != NULL); @@ -913,7 +913,7 @@ sysprof_capture_writer_flush (SysprofCaptureWriter *self) * * Returns: %TRUE if successful, otherwise %FALSE and @error is set. */ -gboolean +bool sysprof_capture_writer_save_as (SysprofCaptureWriter *self, const char *filename, GError **error) @@ -958,7 +958,7 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter *self, close (fd); - return TRUE; + return true; handle_errno: g_set_error (error, @@ -972,7 +972,7 @@ handle_errno: g_unlink (filename); } - return FALSE; + return false; } /** @@ -991,7 +991,7 @@ handle_errno: * * Returns: %TRUE if successful; otherwise %FALSE and @error is set. */ -gboolean +bool _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, int fd, GError **error) @@ -1012,7 +1012,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, G_FILE_ERROR, G_FILE_ERROR_INVAL, "Cannot splice, possibly corrupt file."); - return FALSE; + return false; } in_off = 256; @@ -1035,7 +1035,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, to_write -= written; } - return TRUE; + return true; handle_errno: g_set_error (error, @@ -1043,7 +1043,7 @@ handle_errno: g_file_error_from_errno (errno), "%s", g_strerror (errno)); - return FALSE; + return false; } /** @@ -1059,12 +1059,12 @@ handle_errno: * * Returns: %TRUE if successful, otherwise %FALSE and and @error is set. */ -gboolean +bool sysprof_capture_writer_splice (SysprofCaptureWriter *self, SysprofCaptureWriter *dest, GError **error) { - gboolean ret; + bool ret; off_t pos; g_assert (self != NULL); @@ -1086,7 +1086,7 @@ sysprof_capture_writer_splice (SysprofCaptureWriter *self, /* Now reset or file-descriptor position (it should be the same */ if (pos != lseek (self->fd, pos, SEEK_SET)) { - ret = FALSE; + ret = false; goto handle_errno; } @@ -1098,7 +1098,7 @@ handle_errno: g_file_error_from_errno (errno), "%s", g_strerror (errno)); - return FALSE; + return false; } /** @@ -1165,7 +1165,7 @@ sysprof_capture_writer_stat (SysprofCaptureWriter *self, *stat = self->stat; } -gboolean +bool sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -1181,13 +1181,13 @@ sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, g_assert (counters != NULL); if (n_counters == 0) - return TRUE; + return true; len = sizeof *def + (sizeof *counters * n_counters); def = (SysprofCaptureCounterDefine *)sysprof_capture_writer_allocate (self, &len); if (!def) - return FALSE; + return false; sysprof_capture_writer_frame_init (&def->frame, len, @@ -1212,10 +1212,10 @@ sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_CTRDEF]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -1236,7 +1236,7 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, g_assert (values != NULL || !n_counters); if (n_counters == 0) - return TRUE; + return true; /* Determine how many value groups we need */ n_groups = n_counters / G_N_ELEMENTS (set->values[0].values); @@ -1247,7 +1247,7 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, set = (SysprofCaptureCounterSet *)sysprof_capture_writer_allocate (self, &len); if (!set) - return FALSE; + return false; memset (set, 0, len); @@ -1277,7 +1277,7 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_CTRSET]++; - return TRUE; + return true; } /** @@ -1310,7 +1310,7 @@ sysprof_capture_writer_request_counter (SysprofCaptureWriter *self, return ret; } -gboolean +bool _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, int64_t start_time, int64_t end_time) @@ -1337,7 +1337,7 @@ do_end: if (ret < 0 && errno == EAGAIN) goto do_end; - return TRUE; + return true; } SysprofCaptureWriter * @@ -1370,7 +1370,7 @@ sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self) return self->len; } -gboolean +bool sysprof_capture_writer_add_log (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -1395,7 +1395,7 @@ sysprof_capture_writer_add_log (SysprofCaptureWriter *self, len = sizeof *ev + message_len; ev = (SysprofCaptureLog *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -1412,10 +1412,10 @@ sysprof_capture_writer_add_log (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_LOG]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_file (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -1433,7 +1433,7 @@ sysprof_capture_writer_add_file (SysprofCaptureWriter *self, len = sizeof *ev + data_len; ev = (SysprofCaptureFileChunk *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -1450,10 +1450,10 @@ sysprof_capture_writer_add_file (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_FILE_CHUNK]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -1467,7 +1467,7 @@ sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, for (;;) { - gboolean is_last; + bool is_last; ssize_t n_read; again: @@ -1478,13 +1478,13 @@ sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, is_last = n_read == 0; if (!sysprof_capture_writer_add_file (self, time, cpu, pid, path, is_last, data, n_read)) - return FALSE; + return false; if (is_last) break; } - return TRUE; + return true; } static gboolean @@ -1523,7 +1523,7 @@ sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self, g_source_attach (self->periodic_flush, main_context); } -gboolean +bool sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -1544,7 +1544,7 @@ sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, len = sizeof *ev + (MAX_UNWIND_DEPTH * sizeof (SysprofCaptureAddress)); ev = (SysprofCaptureAllocation *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -1574,10 +1574,10 @@ sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_ALLOCATION]++; - return TRUE; + return true; } -gboolean +bool sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, int64_t time, int cpu, @@ -1599,7 +1599,7 @@ sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress)); ev = (SysprofCaptureAllocation *)sysprof_capture_writer_allocate (self, &len); if (!ev) - return FALSE; + return false; sysprof_capture_writer_frame_init (&ev->frame, len, @@ -1618,10 +1618,10 @@ sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, self->stat.frame_count[SYSPROF_CAPTURE_FRAME_ALLOCATION]++; - return TRUE; + return true; } -gboolean +bool _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, const SysprofCaptureFrame *fr) { @@ -1635,7 +1635,7 @@ _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, len = fr->len; if (!(begin = sysprof_capture_writer_allocate (self, &len))) - return FALSE; + return false; g_assert (fr->len == len); g_assert (fr->type < 16); @@ -1645,5 +1645,5 @@ _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, if (fr->type < G_N_ELEMENTS (self->stat.frame_count)) self->stat.frame_count[fr->type]++; - return TRUE; + return true; } diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index a20bb822..d2f96c30 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -56,6 +56,7 @@ #pragma once +#include #include #include @@ -101,23 +102,23 @@ void sysprof_capture_writer_set_flush_delay (Sy GMainContext *main_context, unsigned int timeout_seconds); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_file (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_file (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, const char *path, - gboolean is_last, + bool is_last, const uint8_t *data, size_t data_len); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, const char *path, int fd); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_map (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_map (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -127,7 +128,7 @@ gboolean sysprof_capture_writer_add_map (Sy uint64_t inode, const char *filename); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -136,7 +137,7 @@ gboolean sysprof_capture_writer_add_mark (Sy const char *name, const char *message); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -147,13 +148,13 @@ SYSPROF_AVAILABLE_IN_ALL uint64_t sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self, const char *name); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_process (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_process (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, const char *cmdline); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -161,30 +162,30 @@ gboolean sysprof_capture_writer_add_sample (Sy const SysprofCaptureAddress *addrs, unsigned int n_addrs); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, int32_t child_pid); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, +bool sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, const SysprofCaptureCounter *counters, unsigned int n_counters); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, +bool sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -192,7 +193,7 @@ gboolean sysprof_capture_writer_set_counters (Sy const SysprofCaptureCounterValue *values, unsigned int n_counters); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_add_log (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_log (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -200,7 +201,7 @@ gboolean sysprof_capture_writer_add_log (Sy const char *domain, const char *message); SYSPROF_AVAILABLE_IN_3_36 -gboolean sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -210,7 +211,7 @@ gboolean sysprof_capture_writer_add_allocation (Sy SysprofBacktraceFunc backtrace_func, void *backtrace_data); SYSPROF_AVAILABLE_IN_3_36 -gboolean sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, +bool sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, int64_t time, int cpu, int32_t pid, @@ -220,9 +221,9 @@ gboolean sysprof_capture_writer_add_allocation_copy (Sy const SysprofCaptureAddress *addrs, unsigned int n_addrs); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_flush (SysprofCaptureWriter *self); +bool sysprof_capture_writer_flush (SysprofCaptureWriter *self); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_save_as (SysprofCaptureWriter *self, +bool sysprof_capture_writer_save_as (SysprofCaptureWriter *self, const char *filename, GError **error); SYSPROF_AVAILABLE_IN_ALL @@ -232,22 +233,22 @@ SYSPROF_AVAILABLE_IN_ALL SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, GError **error); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_splice (SysprofCaptureWriter *self, +bool sysprof_capture_writer_splice (SysprofCaptureWriter *self, SysprofCaptureWriter *dest, GError **error); SYSPROF_AVAILABLE_IN_ALL -gboolean sysprof_capture_writer_cat (SysprofCaptureWriter *self, +bool sysprof_capture_writer_cat (SysprofCaptureWriter *self, SysprofCaptureReader *reader, GError **error); G_GNUC_INTERNAL -gboolean _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, +bool _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, const SysprofCaptureFrame *frame); G_GNUC_INTERNAL -gboolean _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, +bool _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, int fd, GError **error) G_GNUC_INTERNAL; G_GNUC_INTERNAL -gboolean _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, +bool _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, int64_t start_time, int64_t end_time) G_GNUC_INTERNAL; diff --git a/src/libsysprof-capture/sysprof-clock.c b/src/libsysprof-capture/sysprof-clock.c index f517deea..004305e9 100644 --- a/src/libsysprof-capture/sysprof-clock.c +++ b/src/libsysprof-capture/sysprof-clock.c @@ -58,6 +58,8 @@ #include "config.h" +#include + #include "sysprof-clock.h" int sysprof_clock = -1; diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 29e74318..1d1b40fe 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -66,6 +66,7 @@ #ifdef __linux__ # include #endif +#include #include #include #include @@ -82,7 +83,7 @@ typedef struct { MappedRingBuffer *buffer; - gboolean is_shared; + bool is_shared; int tid; int pid; } SysprofCollector; @@ -99,7 +100,7 @@ static GPrivate single_trace_key = G_PRIVATE_INIT (NULL); static SysprofCollector *shared_collector; static SysprofCollector invalid; -static inline gboolean +static inline bool use_single_trace (void) { return GPOINTER_TO_INT (g_private_get (&single_trace_key)); diff --git a/src/libsysprof/sysprof-control-source.c b/src/libsysprof/sysprof-control-source.c index fdf8f504..e39ebde8 100644 --- a/src/libsysprof/sysprof-control-source.c +++ b/src/libsysprof/sysprof-control-source.c @@ -123,10 +123,10 @@ sysprof_control_source_init (SysprofControlSource *self) g_array_set_clear_func (self->source_ids, remove_source_id); } -static gboolean -event_frame_cb (gconstpointer data, - gsize *length, - gpointer user_data) +static bool +event_frame_cb (const void *data, + size_t *length, + void *user_data) { const SysprofCaptureFrame *fr = data; RingData *rd = user_data; diff --git a/src/libsysprof/sysprof-local-profiler.c b/src/libsysprof/sysprof-local-profiler.c index f993a8e1..0fc32aaf 100644 --- a/src/libsysprof/sysprof-local-profiler.c +++ b/src/libsysprof/sysprof-local-profiler.c @@ -983,9 +983,9 @@ profiler_iface_init (SysprofProfilerInterface *iface) iface->stopped = sysprof_local_profiler_real_stopped; } -static gboolean +static bool find_profiler_meta_cb (const SysprofCaptureFrame *frame, - gpointer user_data) + void *user_data) { const SysprofCaptureMetadata *meta = (const SysprofCaptureMetadata *)frame; GKeyFile **keyfile = user_data; @@ -1006,7 +1006,7 @@ find_profiler_meta_cb (const SysprofCaptureFrame *frame, return *keyfile == NULL; } - return TRUE; + return true; } SysprofProfiler * diff --git a/src/libsysprof/sysprof-memprof-profile.c b/src/libsysprof/sysprof-memprof-profile.c index dca1cca1..30984106 100644 --- a/src/libsysprof/sysprof-memprof-profile.c +++ b/src/libsysprof/sysprof-memprof-profile.c @@ -251,9 +251,9 @@ create_cursor (SysprofCaptureReader *reader) return cursor; } -static gboolean +static bool all_allocs_foreach_cb (const SysprofCaptureFrame *frame, - gpointer user_data) + void *user_data) { Generate *g = user_data; @@ -273,12 +273,12 @@ all_allocs_foreach_cb (const SysprofCaptureFrame *frame, (gchar *)g_string_chunk_insert_const (g->symbols, cmdline)); } - return TRUE; + return true; } /* Short-circuit if we don't care about this frame */ if (!sysprof_selection_contains (g->selection, frame->time)) - return TRUE; + return true; if (frame->type == SYSPROF_CAPTURE_FRAME_ALLOCATION) { @@ -361,7 +361,7 @@ all_allocs_foreach_cb (const SysprofCaptureFrame *frame, } } - return TRUE; + return true; } static gint diff --git a/src/tests/test-capture-cursor.c b/src/tests/test-capture-cursor.c index dc24cba9..dd884160 100644 --- a/src/tests/test-capture-cursor.c +++ b/src/tests/test-capture-cursor.c @@ -22,13 +22,13 @@ #include #include -static gboolean +static bool increment (const SysprofCaptureFrame *frame, - gpointer user_data) + void *user_data) { gint *count= user_data; (*count)++; - return TRUE; + return true; } static void diff --git a/src/tests/test-mapped-ring-buffer.c b/src/tests/test-mapped-ring-buffer.c index 3213c7e4..707c0dcb 100644 --- a/src/tests/test-mapped-ring-buffer.c +++ b/src/tests/test-mapped-ring-buffer.c @@ -5,10 +5,10 @@ static gsize real_count; -static gboolean -drain_nth_cb (gconstpointer data, - gsize *len, - gpointer user_data) +static bool +drain_nth_cb (const void *data, + size_t *len, + void *user_data) { const gint64 *v64 = data; g_assert_cmpint (*len, >=, 8); @@ -17,10 +17,10 @@ drain_nth_cb (gconstpointer data, return G_SOURCE_CONTINUE; } -static gboolean -drain_count_cb (gconstpointer data, - gsize *len, - gpointer user_data) +static bool +drain_count_cb (const void *data, + size_t *len, + void *user_data) { const gint64 *v64 = data; g_assert_cmpint (*len, >=, 8); @@ -93,10 +93,10 @@ typedef struct gint64 done; } ThreadedMessage; -static gboolean -handle_msg (gconstpointer data, - gsize *length, - gpointer user_data) +static bool +handle_msg (const void *data, + size_t *length, + void *user_data) { const ThreadedMessage *msg = data; gboolean *done = user_data; diff --git a/src/tools/list-threads.c b/src/tools/list-threads.c index 5016c7bd..30837c57 100644 --- a/src/tools/list-threads.c +++ b/src/tools/list-threads.c @@ -26,9 +26,9 @@ #include "../libsysprof/sysprof-capture-autocleanups.h" -static gboolean +static bool foreach_cb (const SysprofCaptureFrame *frame, - gpointer user_data) + void *user_data) { const SysprofCaptureSample *sample = (SysprofCaptureSample *)frame; GHashTable *seen = user_data; @@ -38,7 +38,7 @@ foreach_cb (const SysprofCaptureFrame *frame, GINT_TO_POINTER (sample->tid), GINT_TO_POINTER (frame->pid)); - return TRUE; + return true; } gint From 8e28ac6e81b12a7af974af8585362444cb5496a7 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 16:26:15 +0100 Subject: [PATCH 12/62] libsysprof-capture: Use assert() instead of g_assert() Also use it instead of `g_return_if_fail()`. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 57 +++--- src/libsysprof-capture/sysprof-address.h | 4 +- .../sysprof-capture-condition.c | 36 ++-- .../sysprof-capture-cursor.c | 30 +-- .../sysprof-capture-reader.c | 192 +++++++++--------- .../sysprof-capture-types.h | 41 ++-- src/libsysprof-capture/sysprof-capture-util.c | 7 +- .../sysprof-capture-writer-cat.c | 6 +- .../sysprof-capture-writer.c | 178 ++++++++-------- src/libsysprof-capture/sysprof-clock.c | 4 +- src/libsysprof-capture/sysprof-collector.c | 3 +- .../sysprof-macros-internal.h | 62 ++++++ 12 files changed, 349 insertions(+), 271 deletions(-) create mode 100644 src/libsysprof-capture/sysprof-macros-internal.h diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index 76d30a65..fb1890b3 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -22,6 +22,7 @@ #include "config.h" +#include #include #include #include @@ -54,7 +55,7 @@ typedef struct _MappedRingHeader uint32_t size; } MappedRingHeader; -G_STATIC_ASSERT (sizeof (MappedRingHeader) == 16); +static_assert (sizeof (MappedRingHeader) == 16, "MappedRingHeader changed size"); /* * MappedRingBuffer is used to wrap both the reader and writer @@ -80,7 +81,7 @@ static inline void * get_body_at_pos (MappedRingBuffer *self, size_t pos) { - g_assert (pos < (self->body_size + self->body_size)); + assert (pos < (self->body_size + self->body_size)); return (uint8_t *)self->map + self->page_size + pos; } @@ -132,7 +133,7 @@ map_head_and_body_twice (int fd, return NULL; } - g_assert (second == (void *)((uint8_t *)map + head_size + body_size)); + assert (second == (void *)((uint8_t *)map + head_size + body_size)); return map; } @@ -166,8 +167,8 @@ mapped_ring_buffer_new_reader (size_t buffer_size) void *map; int fd; - g_return_val_if_fail ((buffer_size % _sysprof_getpagesize ()) == 0, NULL); - g_return_val_if_fail (buffer_size < BUFFER_MAX_SIZE, NULL); + assert ((buffer_size % _sysprof_getpagesize ()) == 0); + assert (buffer_size < BUFFER_MAX_SIZE); page_size = _sysprof_getpagesize (); @@ -243,7 +244,7 @@ mapped_ring_buffer_new_writer (int fd) size_t page_size; void *map; - g_return_val_if_fail (fd > -1, NULL); + assert (fd > -1); page_size = _sysprof_getpagesize (); @@ -329,8 +330,8 @@ mapped_ring_buffer_finalize (MappedRingBuffer *self) void mapped_ring_buffer_unref (MappedRingBuffer *self) { - g_return_if_fail (self != NULL); - g_return_if_fail (self->ref_count > 0); + assert (self != NULL); + assert (self->ref_count > 0); if (g_atomic_int_dec_and_test (&self->ref_count)) mapped_ring_buffer_finalize (self); @@ -339,8 +340,8 @@ mapped_ring_buffer_unref (MappedRingBuffer *self) MappedRingBuffer * mapped_ring_buffer_ref (MappedRingBuffer *self) { - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (self->ref_count > 0, NULL); + assert (self != NULL); + assert (self->ref_count > 0); g_atomic_int_inc (&self->ref_count); @@ -350,7 +351,7 @@ mapped_ring_buffer_ref (MappedRingBuffer *self) int mapped_ring_buffer_get_fd (MappedRingBuffer *self) { - g_return_val_if_fail (self != NULL, -1); + assert (self != NULL); return self->fd; } @@ -386,11 +387,11 @@ mapped_ring_buffer_allocate (MappedRingBuffer *self, uint32_t headpos; uint32_t tailpos; - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (self->mode & MODE_WRITER, NULL); - g_return_val_if_fail (length > 0, NULL); - g_return_val_if_fail (length < self->body_size, NULL); - g_return_val_if_fail ((length & 0x7) == 0, NULL); + assert (self != NULL); + assert (self->mode & MODE_WRITER); + assert (length > 0); + assert (length < self->body_size); + assert ((length & 0x7) == 0); header = get_header (self); headpos = g_atomic_int_get (&header->head); @@ -442,11 +443,11 @@ mapped_ring_buffer_advance (MappedRingBuffer *self, MappedRingHeader *header; uint32_t tail; - g_return_if_fail (self != NULL); - g_return_if_fail (self->mode & MODE_WRITER); - g_return_if_fail (length > 0); - g_return_if_fail (length < self->body_size); - g_return_if_fail ((length & 0x7) == 0); + assert (self != NULL); + assert (self->mode & MODE_WRITER); + assert (length > 0); + assert (length < self->body_size); + assert ((length & 0x7) == 0); header = get_header (self); tail = header->tail; @@ -487,16 +488,16 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, uint32_t headpos; uint32_t tailpos; - g_return_val_if_fail (self != NULL, FALSE); - g_return_val_if_fail (self->mode & MODE_READER, FALSE); - g_return_val_if_fail (callback != NULL, FALSE); + assert (self != NULL); + assert (self->mode & MODE_READER); + assert (callback != NULL); header = get_header (self); headpos = g_atomic_int_get (&header->head); tailpos = g_atomic_int_get (&header->tail); - g_assert (headpos < self->body_size); - g_assert (tailpos < self->body_size); + assert (headpos < self->body_size); + assert (tailpos < self->body_size); if (headpos == tailpos) return true; @@ -507,7 +508,7 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, if (tailpos < headpos) tailpos += self->body_size; - g_assert (headpos < tailpos); + assert (headpos < tailpos); while (headpos < tailpos) { @@ -571,7 +572,7 @@ mapped_ring_buffer_clear (MappedRingBuffer *self) { MappedRingHeader *header; - g_return_if_fail (self != NULL); + assert (self != NULL); header = get_header (self); header->head = 0; diff --git a/src/libsysprof-capture/sysprof-address.h b/src/libsysprof-capture/sysprof-address.h index 3b5ca4e9..39b70684 100644 --- a/src/libsysprof-capture/sysprof-address.h +++ b/src/libsysprof-capture/sysprof-address.h @@ -56,6 +56,7 @@ #pragma once +#include #include #include @@ -65,7 +66,8 @@ G_BEGIN_DECLS typedef uint64_t SysprofAddress; -G_STATIC_ASSERT (sizeof (SysprofAddress) >= sizeof (void *)); +static_assert (sizeof (SysprofAddress) >= sizeof (void *), + "Address space is too big"); typedef enum { diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 62f12e1e..ea3aea6e 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -58,10 +58,12 @@ #include "config.h" +#include #include #include #include "sysprof-capture-condition.h" +#include "sysprof-macros-internal.h" /** * SECTION:sysprof-capture-condition @@ -109,8 +111,8 @@ bool sysprof_capture_condition_match (const SysprofCaptureCondition *self, const SysprofCaptureFrame *frame) { - g_assert (self != NULL); - g_assert (frame != NULL); + assert (self != NULL); + assert (frame != NULL); switch (self->type) { @@ -192,7 +194,7 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, break; } - g_assert_not_reached (); + sysprof_assert_not_reached (); return false; } @@ -250,7 +252,7 @@ sysprof_capture_condition_copy (const SysprofCaptureCondition *self) break; } - g_return_val_if_reached (NULL); + sysprof_assert_not_reached (); } static void @@ -288,7 +290,7 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self) break; default: - g_assert_not_reached (); + sysprof_assert_not_reached (); break; } @@ -298,8 +300,8 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self) SysprofCaptureCondition * sysprof_capture_condition_ref (SysprofCaptureCondition *self) { - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (self->ref_count > 0, NULL); + assert (self != NULL); + assert (self->ref_count > 0); g_atomic_int_inc (&self->ref_count); return self; @@ -308,8 +310,8 @@ sysprof_capture_condition_ref (SysprofCaptureCondition *self) void sysprof_capture_condition_unref (SysprofCaptureCondition *self) { - g_return_if_fail (self != NULL); - g_return_if_fail (self->ref_count > 0); + assert (self != NULL); + assert (self->ref_count > 0); if (g_atomic_int_dec_and_test (&self->ref_count)) sysprof_capture_condition_finalize (self); @@ -321,7 +323,7 @@ sysprof_capture_condition_new_where_type_in (unsigned int n_ty { SysprofCaptureCondition *self; - g_return_val_if_fail (types != NULL, NULL); + assert (types != NULL); self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN; @@ -359,7 +361,7 @@ sysprof_capture_condition_new_where_pid_in (unsigned int n_pids, { SysprofCaptureCondition *self; - g_return_val_if_fail (pids != NULL, NULL); + assert (pids != NULL); self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN; @@ -376,7 +378,7 @@ sysprof_capture_condition_new_where_counter_in (unsigned int n_counters, { SysprofCaptureCondition *self; - g_return_val_if_fail (counters != NULL || n_counters == 0, NULL); + assert (counters != NULL || n_counters == 0); self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN; @@ -407,8 +409,8 @@ sysprof_capture_condition_new_and (SysprofCaptureCondition *left, { SysprofCaptureCondition *self; - g_return_val_if_fail (left != NULL, NULL); - g_return_val_if_fail (right != NULL, NULL); + assert (left != NULL); + assert (right != NULL); self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_AND; @@ -434,8 +436,8 @@ sysprof_capture_condition_new_or (SysprofCaptureCondition *left, { SysprofCaptureCondition *self; - g_return_val_if_fail (left != NULL, NULL); - g_return_val_if_fail (right != NULL, NULL); + assert (left != NULL); + assert (right != NULL); self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_OR; @@ -459,7 +461,7 @@ sysprof_capture_condition_new_where_file (const char *path) { SysprofCaptureCondition *self; - g_return_val_if_fail (path != NULL, NULL); + assert (path != NULL); self = sysprof_capture_condition_init (); self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE; diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index b854a6c3..62b20722 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -58,6 +58,8 @@ #include "config.h" +#include + #include "sysprof-capture-condition.h" #include "sysprof-capture-cursor.h" #include "sysprof-capture-reader.h" @@ -105,8 +107,8 @@ sysprof_capture_cursor_init (void) SysprofCaptureCursor * sysprof_capture_cursor_ref (SysprofCaptureCursor *self) { - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (self->ref_count > 0, NULL); + assert (self != NULL); + assert (self->ref_count > 0); g_atomic_int_inc (&self->ref_count); return self; @@ -121,8 +123,8 @@ sysprof_capture_cursor_ref (SysprofCaptureCursor *self) void sysprof_capture_cursor_unref (SysprofCaptureCursor *self) { - g_return_if_fail (self != NULL); - g_return_if_fail (self->ref_count > 0); + assert (self != NULL); + assert (self->ref_count > 0); if (g_atomic_int_dec_and_test (&self->ref_count)) sysprof_capture_cursor_finalize (self); @@ -140,9 +142,9 @@ sysprof_capture_cursor_foreach (SysprofCaptureCursor *self, SysprofCaptureCursorCallback callback, void *user_data) { - g_return_if_fail (self != NULL); - g_return_if_fail (self->reader != NULL); - g_return_if_fail (callback != NULL); + assert (self != NULL); + assert (self->reader != NULL); + assert (callback != NULL); for (;;) { @@ -249,8 +251,8 @@ sysprof_capture_cursor_foreach (SysprofCaptureCursor *self, void sysprof_capture_cursor_reset (SysprofCaptureCursor *self) { - g_return_if_fail (self != NULL); - g_return_if_fail (self->reader != NULL); + assert (self != NULL); + assert (self->reader != NULL); sysprof_capture_reader_reset (self->reader); } @@ -258,7 +260,7 @@ sysprof_capture_cursor_reset (SysprofCaptureCursor *self) void sysprof_capture_cursor_reverse (SysprofCaptureCursor *self) { - g_return_if_fail (self != NULL); + assert (self != NULL); self->reversed = !self->reversed; } @@ -275,8 +277,8 @@ void sysprof_capture_cursor_add_condition (SysprofCaptureCursor *self, SysprofCaptureCondition *condition) { - g_return_if_fail (self != NULL); - g_return_if_fail (condition != NULL); + assert (self != NULL); + assert (condition != NULL); g_ptr_array_add (self->conditions, condition); } @@ -292,7 +294,7 @@ sysprof_capture_cursor_new (SysprofCaptureReader *reader) { SysprofCaptureCursor *self; - g_return_val_if_fail (reader != NULL, NULL); + assert (reader != NULL); self = sysprof_capture_cursor_init (); self->reader = sysprof_capture_reader_copy (reader); @@ -311,7 +313,7 @@ sysprof_capture_cursor_new (SysprofCaptureReader *reader) SysprofCaptureReader * sysprof_capture_cursor_get_reader (SysprofCaptureCursor *self) { - g_return_val_if_fail (self != NULL, NULL); + assert (self != NULL); return self->reader; } diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index d6505149..59ce530c 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -58,6 +58,7 @@ #include "config.h" +#include #include #include #include @@ -69,6 +70,7 @@ #include "sysprof-capture-reader.h" #include "sysprof-capture-util-private.h" #include "sysprof-capture-writer.h" +#include "sysprof-macros-internal.h" struct _SysprofCaptureReader { @@ -92,8 +94,8 @@ sysprof_capture_reader_read_file_header (SysprofCaptureReader *self, SysprofCaptureFileHeader *header, GError **error) { - g_assert (self != NULL); - g_assert (header != NULL); + assert (self != NULL); + assert (header != NULL); if (sizeof *header != _sysprof_pread (self->fd, header, sizeof *header, 0L)) { @@ -133,7 +135,7 @@ sysprof_capture_reader_finalize (SysprofCaptureReader *self) const char * sysprof_capture_reader_get_time (SysprofCaptureReader *self) { - g_return_val_if_fail (self != NULL, NULL); + assert (self != NULL); return self->header.capture_time; } @@ -141,7 +143,7 @@ sysprof_capture_reader_get_time (SysprofCaptureReader *self) const char * sysprof_capture_reader_get_filename (SysprofCaptureReader *self) { - g_return_val_if_fail (self != NULL, NULL); + assert (self != NULL); return self->filename; } @@ -151,7 +153,7 @@ sysprof_capture_reader_discover_end_time (SysprofCaptureReader *self) { SysprofCaptureFrame frame; - g_assert (self != NULL); + assert (self != NULL); while (sysprof_capture_reader_peek_frame (self, &frame)) { @@ -212,7 +214,7 @@ sysprof_capture_reader_new_from_fd (int fd, { SysprofCaptureReader *self; - g_assert (fd > -1); + assert (fd > -1); self = g_new0 (SysprofCaptureReader, 1); self->ref_count = 1; @@ -251,7 +253,7 @@ sysprof_capture_reader_new (const char *filename, SysprofCaptureReader *self; int fd; - g_assert (filename != NULL); + assert (filename != NULL); if (-1 == (fd = open (filename, O_RDONLY, 0))) { @@ -277,8 +279,8 @@ static inline void sysprof_capture_reader_bswap_frame (SysprofCaptureReader *self, SysprofCaptureFrame *frame) { - g_assert (self != NULL); - g_assert (frame!= NULL); + assert (self != NULL); + assert (frame!= NULL); if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) { @@ -293,8 +295,8 @@ static inline void sysprof_capture_reader_bswap_file_chunk (SysprofCaptureReader *self, SysprofCaptureFileChunk *file_chunk) { - g_assert (self != NULL); - g_assert (file_chunk != NULL); + assert (self != NULL); + assert (file_chunk != NULL); if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) file_chunk->len = GUINT16_SWAP_LE_BE (file_chunk->len); @@ -304,8 +306,8 @@ static inline void sysprof_capture_reader_bswap_log (SysprofCaptureReader *self, SysprofCaptureLog *log) { - g_assert (self != NULL); - g_assert (log != NULL); + assert (self != NULL); + assert (log != NULL); if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) log->severity = GUINT16_SWAP_LE_BE (log->severity); @@ -315,8 +317,8 @@ static inline void sysprof_capture_reader_bswap_map (SysprofCaptureReader *self, SysprofCaptureMap *map) { - g_assert (self != NULL); - g_assert (map != NULL); + assert (self != NULL); + assert (map != NULL); if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) { @@ -331,8 +333,8 @@ static inline void sysprof_capture_reader_bswap_mark (SysprofCaptureReader *self, SysprofCaptureMark *mark) { - g_assert (self != NULL); - g_assert (mark != NULL); + assert (self != NULL); + assert (mark != NULL); if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) mark->duration = GUINT64_SWAP_LE_BE (mark->duration); @@ -342,8 +344,8 @@ static inline void sysprof_capture_reader_bswap_jitmap (SysprofCaptureReader *self, SysprofCaptureJitmap *jitmap) { - g_assert (self != NULL); - g_assert (jitmap != NULL); + assert (self != NULL); + assert (jitmap != NULL); if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) jitmap->n_jitmaps = GUINT64_SWAP_LE_BE (jitmap->n_jitmaps); @@ -353,9 +355,9 @@ static bool sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self, size_t len) { - g_assert (self != NULL); - g_assert (self->pos <= self->len); - g_assert (len > 0); + assert (self != NULL); + assert (self->pos <= self->len); + assert (len > 0); /* Ensure alignment of length to read */ len = (len + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1); @@ -371,8 +373,8 @@ sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self, while (self->len < len) { - g_assert ((self->pos + self->len) < self->bufsz); - g_assert (self->len < self->bufsz); + assert ((self->pos + self->len) < self->bufsz); + assert (self->len < self->bufsz); /* Read into our buffer after our current read position */ r = _sysprof_pread (self->fd, @@ -396,8 +398,8 @@ sysprof_capture_reader_skip (SysprofCaptureReader *self) { SysprofCaptureFrame *frame; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); if (!sysprof_capture_reader_ensure_space_for (self, sizeof (SysprofCaptureFrame))) return false; @@ -427,15 +429,15 @@ sysprof_capture_reader_peek_frame (SysprofCaptureReader *self, { SysprofCaptureFrame *real_frame; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->len); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->len); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *real_frame)) return false; - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); real_frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos]; @@ -459,8 +461,8 @@ sysprof_capture_reader_peek_type (SysprofCaptureReader *self, { SysprofCaptureFrame frame; - g_assert (self != NULL); - g_assert (type != NULL); + assert (self != NULL); + assert (type != NULL); if (!sysprof_capture_reader_peek_frame (self, &frame)) return false; @@ -478,9 +480,9 @@ sysprof_capture_reader_read_basic (SysprofCaptureReader *self, SysprofCaptureFrame *frame; size_t len = sizeof *frame + extra; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, len)) return NULL; @@ -522,7 +524,7 @@ sysprof_capture_reader_read_fork (SysprofCaptureReader *self) { SysprofCaptureFork *fk; - g_assert (self != NULL); + assert (self != NULL); fk = (SysprofCaptureFork *) sysprof_capture_reader_read_basic (self, SYSPROF_CAPTURE_FRAME_FORK, sizeof (uint32_t)); @@ -541,9 +543,9 @@ sysprof_capture_reader_read_map (SysprofCaptureReader *self) { SysprofCaptureMap *map; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *map)) return NULL; @@ -581,9 +583,9 @@ sysprof_capture_reader_read_log (SysprofCaptureReader *self) { SysprofCaptureLog *log; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *log)) return NULL; @@ -623,9 +625,9 @@ sysprof_capture_reader_read_mark (SysprofCaptureReader *self) { SysprofCaptureMark *mark; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *mark)) return NULL; @@ -669,9 +671,9 @@ sysprof_capture_reader_read_metadata (SysprofCaptureReader *self) { SysprofCaptureMetadata *metadata; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *metadata)) return NULL; @@ -709,9 +711,9 @@ sysprof_capture_reader_read_process (SysprofCaptureReader *self) { SysprofCaptureProcess *process; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *process)) return NULL; @@ -751,9 +753,9 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) uint8_t *endptr; unsigned int i; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *jitmap)) return NULL; @@ -813,9 +815,9 @@ sysprof_capture_reader_read_sample (SysprofCaptureReader *self) { SysprofCaptureSample *sample; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *sample)) return NULL; @@ -859,9 +861,9 @@ sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self) { SysprofCaptureCounterDefine *def; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *def)) return NULL; @@ -906,9 +908,9 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) { SysprofCaptureCounterSet *set; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *set)) return NULL; @@ -956,7 +958,7 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) bool sysprof_capture_reader_reset (SysprofCaptureReader *self) { - g_assert (self != NULL); + assert (self != NULL); self->fd_off = sizeof (SysprofCaptureFileHeader); self->pos = 0; @@ -968,8 +970,8 @@ sysprof_capture_reader_reset (SysprofCaptureReader *self) SysprofCaptureReader * sysprof_capture_reader_ref (SysprofCaptureReader *self) { - g_assert (self != NULL); - g_assert (self->ref_count > 0); + assert (self != NULL); + assert (self->ref_count > 0); g_atomic_int_inc (&self->ref_count); @@ -979,8 +981,8 @@ sysprof_capture_reader_ref (SysprofCaptureReader *self) void sysprof_capture_reader_unref (SysprofCaptureReader *self) { - g_assert (self != NULL); - g_assert (self->ref_count > 0); + assert (self != NULL); + assert (self->ref_count > 0); if (g_atomic_int_dec_and_test (&self->ref_count)) sysprof_capture_reader_finalize (self); @@ -991,9 +993,9 @@ sysprof_capture_reader_splice (SysprofCaptureReader *self, SysprofCaptureWriter *dest, GError **error) { - g_assert (self != NULL); - g_assert (self->fd != -1); - g_assert (dest != NULL); + assert (self != NULL); + assert (self->fd != -1); + assert (dest != NULL); /* Flush before writing anything to ensure consistency */ if (!sysprof_capture_writer_flush (dest)) @@ -1035,8 +1037,8 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, size_t to_write; int fd = -1; - g_assert (self != NULL); - g_assert (filename != NULL); + assert (self != NULL); + assert (filename != NULL); if (-1 == (fd = open (filename, O_CREAT | O_WRONLY, 0640))) goto handle_errno; @@ -1065,7 +1067,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, if (written == 0 && errno != EAGAIN) goto handle_errno; - g_assert (written <= (ssize_t)to_write); + assert (written <= (ssize_t)to_write); to_write -= written; } @@ -1092,7 +1094,7 @@ handle_errno: int64_t sysprof_capture_reader_get_start_time (SysprofCaptureReader *self) { - g_return_val_if_fail (self != NULL, 0); + assert (self != NULL); if (self->endian != G_BYTE_ORDER) return GUINT64_SWAP_LE_BE (self->header.time); @@ -1120,7 +1122,7 @@ sysprof_capture_reader_get_end_time (SysprofCaptureReader *self) { int64_t end_time = 0; - g_return_val_if_fail (self != NULL, 0); + assert (self != NULL); if (self->header.end_time != 0) { @@ -1149,7 +1151,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self) SysprofCaptureReader *copy; int fd; - g_return_val_if_fail (self != NULL, NULL); + assert (self != NULL); if (-1 == (fd = dup (self->fd))) return NULL; @@ -1175,7 +1177,7 @@ void sysprof_capture_reader_set_stat (SysprofCaptureReader *self, const SysprofCaptureStat *st_buf) { - g_return_if_fail (self != NULL); + assert (self != NULL); if (st_buf != NULL) { @@ -1193,7 +1195,7 @@ bool sysprof_capture_reader_get_stat (SysprofCaptureReader *self, SysprofCaptureStat *st_buf) { - g_return_val_if_fail (self != NULL, FALSE); + assert (self != NULL); if (st_buf != NULL) *st_buf = self->st_buf; @@ -1206,9 +1208,9 @@ sysprof_capture_reader_read_file (SysprofCaptureReader *self) { SysprofCaptureFileChunk *file_chunk; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *file_chunk)) return NULL; @@ -1254,7 +1256,7 @@ sysprof_capture_reader_list_files (SysprofCaptureReader *self) GHashTableIter iter; const gchar *key; - g_assert (self != NULL); + assert (self != NULL); ar = g_ptr_array_new_with_free_func (g_free); files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); @@ -1289,9 +1291,9 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, const char *path, int fd) { - g_assert (self != NULL); - g_assert (path != NULL); - g_assert (fd > -1); + assert (self != NULL); + assert (path != NULL); + assert (fd > -1); for (;;) { @@ -1326,7 +1328,7 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, if (written == 0 && errno != EAGAIN) return false; - g_assert (written <= (ssize_t)to_write); + assert (written <= (ssize_t)to_write); buf += written; to_write -= written; @@ -1342,13 +1344,13 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, return false; } - g_return_val_if_reached (FALSE); + sysprof_assert_not_reached (); } int sysprof_capture_reader_get_byte_order (SysprofCaptureReader *self) { - g_return_val_if_fail (self != NULL, 0); + assert (self != NULL); return self->endian; } @@ -1359,8 +1361,8 @@ sysprof_capture_reader_find_file (SysprofCaptureReader *self, { SysprofCaptureFrameType type; - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (path != NULL, NULL); + assert (self != NULL); + assert (path != NULL); while (sysprof_capture_reader_peek_type (self, &type)) { @@ -1389,9 +1391,9 @@ sysprof_capture_reader_read_allocation (SysprofCaptureReader *self) { SysprofCaptureAllocation *ma; - g_assert (self != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); - g_assert (self->pos <= self->bufsz); + assert (self != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self->pos <= self->bufsz); if (!sysprof_capture_reader_ensure_space_for (self, sizeof *ma)) return NULL; diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index 05f36c44..ffe18867 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -56,6 +56,7 @@ #pragma once +#include #include #include #include @@ -331,27 +332,27 @@ typedef struct } SysprofCaptureAllocation SYSPROF_ALIGNED_END(1); -G_STATIC_ASSERT (sizeof (SysprofCaptureFileHeader) == 256); -G_STATIC_ASSERT (sizeof (SysprofCaptureFrame) == 24); -G_STATIC_ASSERT (sizeof (SysprofCaptureMap) == 56); -G_STATIC_ASSERT (sizeof (SysprofCaptureJitmap) == 28); -G_STATIC_ASSERT (sizeof (SysprofCaptureProcess) == 24); -G_STATIC_ASSERT (sizeof (SysprofCaptureSample) == 32); -G_STATIC_ASSERT (sizeof (SysprofCaptureFork) == 28); -G_STATIC_ASSERT (sizeof (SysprofCaptureExit) == 24); -G_STATIC_ASSERT (sizeof (SysprofCaptureTimestamp) == 24); -G_STATIC_ASSERT (sizeof (SysprofCaptureCounter) == 128); -G_STATIC_ASSERT (sizeof (SysprofCaptureCounterValues) == 96); -G_STATIC_ASSERT (sizeof (SysprofCaptureCounterDefine) == 32); -G_STATIC_ASSERT (sizeof (SysprofCaptureCounterSet) == 32); -G_STATIC_ASSERT (sizeof (SysprofCaptureMark) == 96); -G_STATIC_ASSERT (sizeof (SysprofCaptureMetadata) == 64); -G_STATIC_ASSERT (sizeof (SysprofCaptureLog) == 64); -G_STATIC_ASSERT (sizeof (SysprofCaptureFileChunk) == 284); -G_STATIC_ASSERT (sizeof (SysprofCaptureAllocation) == 48); +static_assert (sizeof (SysprofCaptureFileHeader) == 256, "SysprofCaptureFileHeader changed size"); +static_assert (sizeof (SysprofCaptureFrame) == 24, "SysprofCaptureFrame changed size"); +static_assert (sizeof (SysprofCaptureMap) == 56, "SysprofCaptureMap changed size"); +static_assert (sizeof (SysprofCaptureJitmap) == 28, "SysprofCaptureJitmap changed size"); +static_assert (sizeof (SysprofCaptureProcess) == 24, "SysprofCaptureProcess changed size"); +static_assert (sizeof (SysprofCaptureSample) == 32, "SysprofCaptureSample changed size"); +static_assert (sizeof (SysprofCaptureFork) == 28, "SysprofCaptureFork changed size"); +static_assert (sizeof (SysprofCaptureExit) == 24, "SysprofCaptureExit changed size"); +static_assert (sizeof (SysprofCaptureTimestamp) == 24, "SysprofCaptureTimestamp changed size"); +static_assert (sizeof (SysprofCaptureCounter) == 128, "SysprofCaptureCounter changed size"); +static_assert (sizeof (SysprofCaptureCounterValues) == 96, "SysprofCaptureCounterValues changed size"); +static_assert (sizeof (SysprofCaptureCounterDefine) == 32, "SysprofCaptureCounterDefine changed size"); +static_assert (sizeof (SysprofCaptureCounterSet) == 32, "SysprofCaptureCounterSet changed size"); +static_assert (sizeof (SysprofCaptureMark) == 96, "SysprofCaptureMark changed size"); +static_assert (sizeof (SysprofCaptureMetadata) == 64, "SysprofCaptureMetadata changed size"); +static_assert (sizeof (SysprofCaptureLog) == 64, "SysprofCaptureLog changed size"); +static_assert (sizeof (SysprofCaptureFileChunk) == 284, "SysprofCaptureFileChunk changed size"); +static_assert (sizeof (SysprofCaptureAllocation) == 48, "SysprofCaptureAllocation changed size"); -G_STATIC_ASSERT ((G_STRUCT_OFFSET (SysprofCaptureAllocation, addrs) % SYSPROF_CAPTURE_ALIGN) == 0); -G_STATIC_ASSERT ((G_STRUCT_OFFSET (SysprofCaptureSample, addrs) % SYSPROF_CAPTURE_ALIGN) == 0); +static_assert ((offsetof (SysprofCaptureAllocation, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureAllocation.addrs is not aligned"); +static_assert ((offsetof (SysprofCaptureSample, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureSample.addrs is not aligned"); static inline int sysprof_capture_address_compare (SysprofCaptureAddress a, diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index 4571b4dd..3281172a 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -58,6 +58,7 @@ #include "config.h" +#include #include #include #include @@ -205,14 +206,14 @@ ssize_t if (n_read <= 0) return -1; - g_assert (count >= n_read); + assert (count >= n_read); count -= n_read; rpos += n_read; while (wpos < rpos) { - g_assert (off < sizeof buf); + assert (off < sizeof buf); errno = 0; n_written = write (out_fd, &buf[off], rpos - wpos); @@ -226,7 +227,7 @@ ssize_t } } - g_assert (count == 0); + assert (count == 0); if (offset != NULL) *offset = rpos; diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 013db74d..d86ba7a7 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -158,8 +158,8 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, int64_t first_start_time = INT64_MAX; int64_t end_time = -1; - g_return_val_if_fail (self != NULL, FALSE); - g_return_val_if_fail (reader != NULL, FALSE); + assert (self != NULL); + assert (reader != NULL); sysprof_capture_reader_reset (reader); @@ -459,7 +459,7 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, } } - g_assert (ids->len == values->len); + assert (ids->len == values->len); sysprof_capture_writer_set_counters (self, frame->frame.time, diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index c655a681..16bf7530 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -62,6 +62,7 @@ # define _GNU_SOURCE #endif +#include #include #include #include @@ -75,6 +76,7 @@ #include "sysprof-capture-reader.h" #include "sysprof-capture-util-private.h" #include "sysprof-capture-writer.h" +#include "sysprof-macros-internal.h" #define DEFAULT_BUFFER_SIZE (_sysprof_getpagesize() * 64L) #define INVALID_ADDRESS (G_GUINT64_CONSTANT(0)) @@ -153,7 +155,7 @@ sysprof_capture_writer_frame_init (SysprofCaptureFrame *frame_, int64_t time_, SysprofCaptureFrameType type) { - g_assert (frame_ != NULL); + assert (frame_ != NULL); frame_->len = len; frame_->cpu = cpu; @@ -187,8 +189,8 @@ sysprof_capture_writer_finalize (SysprofCaptureWriter *self) SysprofCaptureWriter * sysprof_capture_writer_ref (SysprofCaptureWriter *self) { - g_assert (self != NULL); - g_assert (self->ref_count > 0); + assert (self != NULL); + assert (self->ref_count > 0); g_atomic_int_inc (&self->ref_count); @@ -198,8 +200,8 @@ sysprof_capture_writer_ref (SysprofCaptureWriter *self) void sysprof_capture_writer_unref (SysprofCaptureWriter *self) { - g_assert (self != NULL); - g_assert (self->ref_count > 0); + assert (self != NULL); + assert (self->ref_count > 0); if (g_atomic_int_dec_and_test (&self->ref_count)) sysprof_capture_writer_finalize (self); @@ -212,9 +214,9 @@ sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) ssize_t written; size_t to_write; - g_assert (self != NULL); - g_assert (self->pos <= self->len); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self != NULL); + assert (self->pos <= self->len); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); if (self->pos == 0) return true; @@ -231,7 +233,7 @@ sysprof_capture_writer_flush_data (SysprofCaptureWriter *self) if (written == 0 && errno != EAGAIN) return false; - g_assert (written <= (ssize_t)to_write); + assert (written <= (ssize_t)to_write); buf += written; to_write -= written; @@ -271,9 +273,9 @@ sysprof_capture_writer_allocate (SysprofCaptureWriter *self, { void *p; - g_assert (self != NULL); - g_assert (len != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self != NULL); + assert (len != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); sysprof_capture_writer_realign (len); @@ -284,7 +286,7 @@ sysprof_capture_writer_allocate (SysprofCaptureWriter *self, self->pos += *len; - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); return p; } @@ -296,12 +298,12 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) ssize_t r; size_t len; - g_assert (self != NULL); + assert (self != NULL); if (self->addr_hash_size == 0) return true; - g_assert (self->addr_buf_pos > 0); + assert (self->addr_buf_pos > 0); len = sizeof jitmap + self->addr_buf_pos; @@ -339,9 +341,9 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, unsigned int hash; unsigned int i; - g_assert (self != NULL); - g_assert (name != NULL); - g_assert (addr != NULL); + assert (self != NULL); + assert (name != NULL); + assert (addr != NULL); hash = g_str_hash (name) % G_N_ELEMENTS (self->addr_hash); @@ -386,9 +388,9 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, unsigned int hash; unsigned int i; - g_assert (self != NULL); - g_assert (str != NULL); - g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); + assert (self != NULL); + assert (str != NULL); + assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0); len = sizeof addr + strlen (str) + 1; @@ -398,12 +400,12 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, if (!sysprof_capture_writer_flush_jitmap (self)) return INVALID_ADDRESS; - g_assert (self->addr_hash_size == 0); - g_assert (self->addr_buf_pos == 0); + assert (self->addr_hash_size == 0); + assert (self->addr_buf_pos == 0); } - g_assert (self->addr_hash_size < G_N_ELEMENTS (self->addr_hash)); - g_assert (len > sizeof addr); + assert (self->addr_hash_size < G_N_ELEMENTS (self->addr_hash)); + assert (len > sizeof addr); /* Allocate the next unique address */ addr = SYSPROF_CAPTURE_JITMAP_MARK | ++self->addr_seq; @@ -421,7 +423,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, /* Advance our string cache position */ self->addr_buf_pos += len; - g_assert (self->addr_buf_pos <= sizeof self->addr_buf); + assert (self->addr_buf_pos <= sizeof self->addr_buf); /* Now place the address into the hashtable */ hash = g_str_hash (str) % G_N_ELEMENTS (self->addr_hash); @@ -454,7 +456,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, } } - g_assert_not_reached (); + sysprof_assert_not_reached (); return INVALID_ADDRESS; } @@ -475,8 +477,8 @@ sysprof_capture_writer_new_from_fd (int fd, if (buffer_size == 0) buffer_size = DEFAULT_BUFFER_SIZE; - g_assert (fd != -1); - g_assert (buffer_size % _sysprof_getpagesize() == 0); + assert (fd != -1); + assert (buffer_size % _sysprof_getpagesize() == 0); /* This is only useful on files, memfd, etc */ if (ftruncate (fd, 0) != 0) { /* Do Nothing */ } @@ -528,12 +530,12 @@ sysprof_capture_writer_new_from_fd (int fd, return NULL; } - g_assert (self->pos == 0); - g_assert (self->len > 0); - g_assert (self->len % _sysprof_getpagesize() == 0); - g_assert (self->buf != NULL); - g_assert (self->addr_hash_size == 0); - g_assert (self->fd != -1); + assert (self->pos == 0); + assert (self->len > 0); + assert (self->len % _sysprof_getpagesize() == 0); + assert (self->buf != NULL); + assert (self->addr_hash_size == 0); + assert (self->fd != -1); return self; } @@ -545,8 +547,8 @@ sysprof_capture_writer_new (const char *filename, SysprofCaptureWriter *self; int fd; - g_assert (filename != NULL); - g_assert (buffer_size % _sysprof_getpagesize() == 0); + assert (filename != NULL); + assert (buffer_size % _sysprof_getpagesize() == 0); if ((-1 == (fd = open (filename, O_CREAT | O_RDWR, 0640))) || (-1 == ftruncate (fd, 0L))) @@ -577,8 +579,8 @@ sysprof_capture_writer_add_map (SysprofCaptureWriter *self, if (filename == NULL) filename = ""; - g_assert (self != NULL); - g_assert (filename != NULL); + assert (self != NULL); + assert (filename != NULL); len = sizeof *ev + strlen (filename) + 1; @@ -619,9 +621,9 @@ sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, size_t message_len; size_t len; - g_assert (self != NULL); - g_assert (name != NULL); - g_assert (group != NULL); + assert (self != NULL); + assert (name != NULL); + assert (group != NULL); if (message == NULL) message = ""; @@ -661,8 +663,8 @@ sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, SysprofCaptureMetadata *ev; size_t len; - g_assert (self != NULL); - g_assert (id != NULL); + assert (self != NULL); + assert (id != NULL); if (metadata == NULL) { @@ -703,8 +705,8 @@ sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self, if (name == NULL) name = ""; - g_assert (self != NULL); - g_assert (name != NULL); + assert (self != NULL); + assert (name != NULL); if (!sysprof_capture_writer_lookup_jitmap (self, name, &addr)) addr = sysprof_capture_writer_insert_jitmap (self, name); @@ -725,8 +727,8 @@ sysprof_capture_writer_add_process (SysprofCaptureWriter *self, if (cmdline == NULL) cmdline = ""; - g_assert (self != NULL); - g_assert (cmdline != NULL); + assert (self != NULL); + assert (cmdline != NULL); len = sizeof *ev + strlen (cmdline) + 1; @@ -761,7 +763,7 @@ sysprof_capture_writer_add_sample (SysprofCaptureWriter *self, SysprofCaptureSample *ev; size_t len; - g_assert (self != NULL); + assert (self != NULL); len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress)); @@ -795,7 +797,7 @@ sysprof_capture_writer_add_fork (SysprofCaptureWriter *self, SysprofCaptureFork *ev; size_t len = sizeof *ev; - g_assert (self != NULL); + assert (self != NULL); ev = (SysprofCaptureFork *)sysprof_capture_writer_allocate (self, &len); if (!ev) @@ -823,7 +825,7 @@ sysprof_capture_writer_add_exit (SysprofCaptureWriter *self, SysprofCaptureExit *ev; size_t len = sizeof *ev; - g_assert (self != NULL); + assert (self != NULL); ev = (SysprofCaptureExit *)sysprof_capture_writer_allocate (self, &len); if (!ev) @@ -850,7 +852,7 @@ sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self, SysprofCaptureTimestamp *ev; size_t len = sizeof *ev; - g_assert (self != NULL); + assert (self != NULL); ev = (SysprofCaptureTimestamp *)sysprof_capture_writer_allocate (self, &len); if (!ev) @@ -874,7 +876,7 @@ sysprof_capture_writer_flush_end_time (SysprofCaptureWriter *self) int64_t end_time = SYSPROF_CAPTURE_CURRENT_TIME; ssize_t ret; - g_assert (self != NULL); + assert (self != NULL); /* This field is opportunistic, so a failure is okay. */ @@ -893,7 +895,7 @@ again: bool sysprof_capture_writer_flush (SysprofCaptureWriter *self) { - g_assert (self != NULL); + assert (self != NULL); return sysprof_capture_writer_flush_jitmap (self) && sysprof_capture_writer_flush_data (self) && @@ -923,9 +925,9 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter *self, off_t pos; int fd = -1; - g_assert (self != NULL); - g_assert (self->fd != -1); - g_assert (filename != NULL); + assert (self != NULL); + assert (self->fd != -1); + assert (filename != NULL); if (-1 == (fd = open (filename, O_CREAT | O_RDWR, 0640))) goto handle_errno; @@ -951,7 +953,7 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter *self, if (written == 0 && errno != EAGAIN) goto handle_errno; - g_assert (written <= (ssize_t)to_write); + assert (written <= (ssize_t)to_write); to_write -= written; } @@ -1000,8 +1002,8 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, off_t in_off; size_t to_write; - g_assert (self != NULL); - g_assert (self->fd != -1); + assert (self != NULL); + assert (self->fd != -1); if (-1 == fstat (fd, &stbuf)) goto handle_errno; @@ -1030,7 +1032,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, if (written == 0 && errno != EAGAIN) goto handle_errno; - g_assert (written <= (ssize_t)to_write); + assert (written <= (ssize_t)to_write); to_write -= written; } @@ -1067,10 +1069,10 @@ sysprof_capture_writer_splice (SysprofCaptureWriter *self, bool ret; off_t pos; - g_assert (self != NULL); - g_assert (self->fd != -1); - g_assert (dest != NULL); - g_assert (dest->fd != -1); + assert (self != NULL); + assert (self->fd != -1); + assert (dest != NULL); + assert (dest->fd != -1); /* Flush before writing anything to ensure consistency */ if (!sysprof_capture_writer_flush (self) || !sysprof_capture_writer_flush (dest)) @@ -1122,8 +1124,8 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, SysprofCaptureReader *ret; int copy; - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (self->fd != -1, NULL); + assert (self != NULL); + assert (self->fd != -1); if (!sysprof_capture_writer_flush (self)) { @@ -1159,8 +1161,8 @@ void sysprof_capture_writer_stat (SysprofCaptureWriter *self, SysprofCaptureStat *stat) { - g_return_if_fail (self != NULL); - g_return_if_fail (stat != NULL); + assert (self != NULL); + assert (stat != NULL); *stat = self->stat; } @@ -1177,8 +1179,8 @@ sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, size_t len; unsigned int i; - g_assert (self != NULL); - g_assert (counters != NULL); + assert (self != NULL); + assert (counters != NULL); if (n_counters == 0) return true; @@ -1231,9 +1233,9 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, unsigned int field; unsigned int i; - g_assert (self != NULL); - g_assert (counters_ids != NULL || n_counters == 0); - g_assert (values != NULL || !n_counters); + assert (self != NULL); + assert (counters_ids != NULL || n_counters == 0); + assert (values != NULL || !n_counters); if (n_counters == 0) return true; @@ -1299,7 +1301,7 @@ sysprof_capture_writer_request_counter (SysprofCaptureWriter *self, { int ret; - g_assert (self != NULL); + assert (self != NULL); if (MAX_COUNTERS - n_counters < self->next_counter_id) return 0; @@ -1317,7 +1319,7 @@ _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, { ssize_t ret; - g_assert (self != NULL); + assert (self != NULL); do_start: ret = _sysprof_pwrite (self->fd, @@ -1365,7 +1367,7 @@ sysprof_capture_writer_new_from_env (size_t buffer_size) size_t sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self) { - g_return_val_if_fail (self != NULL, 0); + assert (self != NULL); return self->len; } @@ -1383,7 +1385,7 @@ sysprof_capture_writer_add_log (SysprofCaptureWriter *self, size_t message_len; size_t len; - g_assert (self != NULL); + assert (self != NULL); if (domain == NULL) domain = ""; @@ -1428,7 +1430,7 @@ sysprof_capture_writer_add_file (SysprofCaptureWriter *self, SysprofCaptureFileChunk *ev; size_t len; - g_assert (self != NULL); + assert (self != NULL); len = sizeof *ev + data_len; ev = (SysprofCaptureFileChunk *)sysprof_capture_writer_allocate (self, &len); @@ -1463,7 +1465,7 @@ sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, { uint8_t data[(4096*4L) - sizeof(SysprofCaptureFileChunk)]; - g_assert (self != NULL); + assert (self != NULL); for (;;) { @@ -1538,8 +1540,8 @@ sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, size_t len; unsigned int n_addrs; - g_assert (self != NULL); - g_assert (backtrace_func != NULL); + assert (self != NULL); + assert (backtrace_func != NULL); len = sizeof *ev + (MAX_UNWIND_DEPTH * sizeof (SysprofCaptureAddress)); ev = (SysprofCaptureAllocation *)sysprof_capture_writer_allocate (self, &len); @@ -1591,7 +1593,7 @@ sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self, SysprofCaptureAllocation *ev; size_t len; - g_assert (self != NULL); + assert (self != NULL); if (n_addrs > 0xFFF) n_addrs = 0xFFF; @@ -1628,17 +1630,17 @@ _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, void *begin; size_t len; - g_assert (self != NULL); - g_assert ((fr->len & 0x7) == 0); - g_assert (fr->type < SYSPROF_CAPTURE_FRAME_LAST); + assert (self != NULL); + assert ((fr->len & 0x7) == 0); + assert (fr->type < SYSPROF_CAPTURE_FRAME_LAST); len = fr->len; if (!(begin = sysprof_capture_writer_allocate (self, &len))) return false; - g_assert (fr->len == len); - g_assert (fr->type < 16); + assert (fr->len == len); + assert (fr->type < 16); memcpy (begin, fr, fr->len); diff --git a/src/libsysprof-capture/sysprof-clock.c b/src/libsysprof-capture/sysprof-clock.c index 004305e9..e65b025d 100644 --- a/src/libsysprof-capture/sysprof-clock.c +++ b/src/libsysprof-capture/sysprof-clock.c @@ -58,9 +58,11 @@ #include "config.h" +#include #include #include "sysprof-clock.h" +#include "sysprof-macros-internal.h" int sysprof_clock = -1; @@ -92,5 +94,5 @@ sysprof_clock_init (void) } } - g_assert_not_reached (); + sysprof_assert_not_reached (); } diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 1d1b40fe..938a9508 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -60,6 +60,7 @@ # define _GNU_SOURCE #endif +#include #include #include #include @@ -185,7 +186,7 @@ write_final_frame (MappedRingBuffer *ring) { SysprofCaptureFrame *fr; - g_assert (ring != NULL); + assert (ring != NULL); if ((fr = mapped_ring_buffer_allocate (ring, sizeof *fr))) { diff --git a/src/libsysprof-capture/sysprof-macros-internal.h b/src/libsysprof-capture/sysprof-macros-internal.h new file mode 100644 index 00000000..f227a230 --- /dev/null +++ b/src/libsysprof-capture/sysprof-macros-internal.h @@ -0,0 +1,62 @@ +/* sysprof-macros.h + * + * Copyright 2020 Endless OS Foundation + * + * Author: + * - Philip Withnall + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Subject to the terms and conditions of this license, each copyright holder + * and contributor hereby grants to those receiving rights under this license + * a perpetual, worldwide, non-exclusive, no-charge, royalty-free, + * irrevocable (except for failure to satisfy the conditions of this license) + * patent license to make, have made, use, offer to sell, sell, import, and + * otherwise transfer this software, where such license applies only to those + * patent claims, already acquired or hereafter acquired, licensable by such + * copyright holder or contributor that are necessarily infringed by: + * + * (a) their Contribution(s) (the licensed copyrights of copyright holders + * and non-copyrightable additions of contributors, in source or binary + * form) alone; or + * + * (b) combination of their Contribution(s) with the work of authorship to + * which such Contribution(s) was added by such copyright holder or + * contributor, if, at the time the Contribution is added, such addition + * causes such combination to be necessarily infringed. The patent license + * shall not apply to any other combinations which include the + * Contribution. + * + * Except as expressly stated above, no rights or licenses from any copyright + * holder or contributor is granted under this license, whether expressly, by + * implication, estoppel or otherwise. + * + * DISCLAIMER + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * SPDX-License-Identifier: BSD-2-Clause-Patent + */ + +#pragma once + +#define sysprof_assert_not_reached() assert (false) From 41ec04ea99bfec15b450700601b0637677ee9e16 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 16:39:06 +0100 Subject: [PATCH 13/62] libsysprof-capture: Replace G_BEGIN_DECLS with SYSPROF_BEGIN_DECLS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It does the same job, but doesn’t require GLib. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.h | 6 +- src/libsysprof-capture/meson.build | 1 + src/libsysprof-capture/sysprof-address.h | 5 +- .../sysprof-capture-condition.h | 5 +- .../sysprof-capture-cursor.h | 5 +- .../sysprof-capture-reader.h | 5 +- .../sysprof-capture-types.h | 5 +- .../sysprof-capture-writer.h | 4 +- src/libsysprof-capture/sysprof-capture.h | 4 -- src/libsysprof-capture/sysprof-clock.h | 5 +- src/libsysprof-capture/sysprof-collector.h | 5 +- src/libsysprof-capture/sysprof-macros.h | 68 +++++++++++++++++++ src/libsysprof-capture/sysprof-platform.h | 5 +- 13 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 src/libsysprof-capture/sysprof-macros.h diff --git a/src/libsysprof-capture/mapped-ring-buffer.h b/src/libsysprof-capture/mapped-ring-buffer.h index 0607aab0..5e2df525 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.h +++ b/src/libsysprof-capture/mapped-ring-buffer.h @@ -24,7 +24,9 @@ #include #include -G_BEGIN_DECLS +#include "sysprof-macros.h" + +SYSPROF_BEGIN_DECLS typedef struct _MappedRingBuffer MappedRingBuffer; @@ -82,4 +84,4 @@ bool mapped_ring_buffer_drain (MappedRingBuffer G_GNUC_INTERNAL bool mapped_ring_buffer_is_empty (MappedRingBuffer *self); -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/meson.build b/src/libsysprof-capture/meson.build index 8ed7934a..77a12586 100644 --- a/src/libsysprof-capture/meson.build +++ b/src/libsysprof-capture/meson.build @@ -7,6 +7,7 @@ libsysprof_capture_headers = files([ 'sysprof-capture-types.h', 'sysprof-capture-writer.h', 'sysprof-collector.h', + 'sysprof-macros.h', 'sysprof-platform.h', 'sysprof-capture.h', 'sysprof-version-macros.h', diff --git a/src/libsysprof-capture/sysprof-address.h b/src/libsysprof-capture/sysprof-address.h index 39b70684..098bcc31 100644 --- a/src/libsysprof-capture/sysprof-address.h +++ b/src/libsysprof-capture/sysprof-address.h @@ -60,9 +60,10 @@ #include #include +#include "sysprof-macros.h" #include "sysprof-version-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS typedef uint64_t SysprofAddress; @@ -98,4 +99,4 @@ sysprof_address_compare (SysprofAddress a, return 1; } -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-condition.h b/src/libsysprof-capture/sysprof-capture-condition.h index 185df91c..78ea7268 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.h +++ b/src/libsysprof-capture/sysprof-capture-condition.h @@ -59,9 +59,10 @@ #include #include "sysprof-capture-types.h" +#include "sysprof-macros.h" #include "sysprof-version-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS SYSPROF_AVAILABLE_IN_ALL SysprofCaptureCondition *sysprof_capture_condition_copy (const SysprofCaptureCondition *self); @@ -93,4 +94,4 @@ SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_condition_match (const SysprofCaptureCondition *self, const SysprofCaptureFrame *frame); -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-cursor.h b/src/libsysprof-capture/sysprof-capture-cursor.h index 4635006a..5d5693b5 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.h +++ b/src/libsysprof-capture/sysprof-capture-cursor.h @@ -59,9 +59,10 @@ #include #include "sysprof-capture-types.h" +#include "sysprof-macros.h" #include "sysprof-version-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS typedef struct _SysprofCaptureCursor SysprofCaptureCursor; @@ -99,4 +100,4 @@ SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_cursor_add_condition (SysprofCaptureCursor *self, SysprofCaptureCondition *condition); -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index d4e3e7df..c417f9b6 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -59,9 +59,10 @@ #include #include "sysprof-capture-types.h" +#include "sysprof-macros.h" #include "sysprof-version-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS typedef struct _SysprofCaptureReader SysprofCaptureReader; @@ -150,4 +151,4 @@ bool sysprof_capture_reader_read_file_fd ( int fd); -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index ffe18867..109e7427 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -63,8 +63,9 @@ #include #include "sysprof-clock.h" +#include "sysprof-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS #define SYSPROF_CAPTURE_MAGIC (GUINT32_TO_LE(0xFDCA975E)) #define SYSPROF_CAPTURE_ALIGN (sizeof(SysprofCaptureAddress)) @@ -366,4 +367,4 @@ sysprof_capture_address_compare (SysprofCaptureAddress a, return 0; } -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index d2f96c30..cc430909 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -63,7 +63,7 @@ #include "sysprof-capture-types.h" #include "sysprof-version-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS typedef struct _SysprofCaptureWriter SysprofCaptureWriter; @@ -253,4 +253,4 @@ bool _sysprof_capture_writer_set_time_range (Sy int64_t end_time) G_GNUC_INTERNAL; -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture.h b/src/libsysprof-capture/sysprof-capture.h index 59b68b91..e5b2dd5b 100644 --- a/src/libsysprof-capture/sysprof-capture.h +++ b/src/libsysprof-capture/sysprof-capture.h @@ -58,8 +58,6 @@ #include -G_BEGIN_DECLS - #define SYSPROF_CAPTURE_INSIDE # include "sysprof-address.h" @@ -74,5 +72,3 @@ G_BEGIN_DECLS # include "sysprof-version-macros.h" #undef SYSPROF_CAPTURE_INSIDE - -G_END_DECLS diff --git a/src/libsysprof-capture/sysprof-clock.h b/src/libsysprof-capture/sysprof-clock.h index fba81a4f..54695cba 100644 --- a/src/libsysprof-capture/sysprof-clock.h +++ b/src/libsysprof-capture/sysprof-clock.h @@ -60,9 +60,10 @@ #include #include +#include "sysprof-macros.h" #include "sysprof-version-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS typedef int SysprofClock; typedef int64_t SysprofTimeStamp; @@ -95,4 +96,4 @@ sysprof_clock_get_relative_time (SysprofTimeStamp epoch) SYSPROF_AVAILABLE_IN_ALL void sysprof_clock_init (void); -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-collector.h b/src/libsysprof-capture/sysprof-collector.h index c7ff9a63..f59ca921 100644 --- a/src/libsysprof-capture/sysprof-collector.h +++ b/src/libsysprof-capture/sysprof-collector.h @@ -57,8 +57,9 @@ #pragma once #include "sysprof-capture-types.h" +#include "sysprof-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS SYSPROF_AVAILABLE_IN_3_36 void sysprof_collector_init (void); @@ -86,4 +87,4 @@ void sysprof_collector_log_printf (int severity, const char *format, ...) G_GNUC_PRINTF (3, 4); -G_END_DECLS +SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-macros.h b/src/libsysprof-capture/sysprof-macros.h new file mode 100644 index 00000000..d5fd7325 --- /dev/null +++ b/src/libsysprof-capture/sysprof-macros.h @@ -0,0 +1,68 @@ +/* sysprof-macros.h + * + * Copyright 2020 Endless OS Foundation + * + * Author: + * - Philip Withnall + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Subject to the terms and conditions of this license, each copyright holder + * and contributor hereby grants to those receiving rights under this license + * a perpetual, worldwide, non-exclusive, no-charge, royalty-free, + * irrevocable (except for failure to satisfy the conditions of this license) + * patent license to make, have made, use, offer to sell, sell, import, and + * otherwise transfer this software, where such license applies only to those + * patent claims, already acquired or hereafter acquired, licensable by such + * copyright holder or contributor that are necessarily infringed by: + * + * (a) their Contribution(s) (the licensed copyrights of copyright holders + * and non-copyrightable additions of contributors, in source or binary + * form) alone; or + * + * (b) combination of their Contribution(s) with the work of authorship to + * which such Contribution(s) was added by such copyright holder or + * contributor, if, at the time the Contribution is added, such addition + * causes such combination to be necessarily infringed. The patent license + * shall not apply to any other combinations which include the + * Contribution. + * + * Except as expressly stated above, no rights or licenses from any copyright + * holder or contributor is granted under this license, whether expressly, by + * implication, estoppel or otherwise. + * + * DISCLAIMER + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * SPDX-License-Identifier: BSD-2-Clause-Patent + */ + +#pragma once + +#ifdef __cplusplus +#define SYSPROF_BEGIN_DECLS extern "C" { +#define SYSPROF_END_DECLS } +#else +#define SYSPROF_BEGIN_DECLS +#define SYSPROF_END_DECLS +#endif diff --git a/src/libsysprof-capture/sysprof-platform.h b/src/libsysprof-capture/sysprof-platform.h index 3f0b3768..44c3e964 100644 --- a/src/libsysprof-capture/sysprof-platform.h +++ b/src/libsysprof-capture/sysprof-platform.h @@ -60,12 +60,13 @@ #include #include "sysprof-version-macros.h" +#include "sysprof-macros.h" -G_BEGIN_DECLS +SYSPROF_BEGIN_DECLS SYSPROF_AVAILABLE_IN_ALL int sysprof_memfd_create (const char *desc); SYSPROF_AVAILABLE_IN_3_36 size_t sysprof_getpagesize (void); -G_END_DECLS +SYSPROF_END_DECLS From c9f54fcc68d2dc82a12b9e9a55043cd699fe4361 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 16:41:52 +0100 Subject: [PATCH 14/62] libsysprof-capture: Replace G_GNUC_INTERNAL with SYSPROF_INTERNAL It does the same thing for modern compilers. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.h | 22 +++++++++---------- .../sysprof-capture-writer.h | 11 +++++----- src/libsysprof-capture/sysprof-macros.h | 6 +++++ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.h b/src/libsysprof-capture/mapped-ring-buffer.h index 5e2df525..eb2c6ca4 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.h +++ b/src/libsysprof-capture/mapped-ring-buffer.h @@ -57,31 +57,31 @@ typedef bool (*MappedRingBufferCallback) (const void *data, size_t *length, void *user_data); -G_GNUC_INTERNAL +SYSPROF_INTERNAL MappedRingBuffer *mapped_ring_buffer_new_reader (size_t buffer_size); -G_GNUC_INTERNAL +SYSPROF_INTERNAL MappedRingBuffer *mapped_ring_buffer_new_readwrite (size_t buffer_size); -G_GNUC_INTERNAL +SYSPROF_INTERNAL MappedRingBuffer *mapped_ring_buffer_new_writer (int fd); -G_GNUC_INTERNAL +SYSPROF_INTERNAL int mapped_ring_buffer_get_fd (MappedRingBuffer *self); -G_GNUC_INTERNAL +SYSPROF_INTERNAL MappedRingBuffer *mapped_ring_buffer_ref (MappedRingBuffer *self); -G_GNUC_INTERNAL +SYSPROF_INTERNAL void mapped_ring_buffer_unref (MappedRingBuffer *self); -G_GNUC_INTERNAL +SYSPROF_INTERNAL void mapped_ring_buffer_clear (MappedRingBuffer *self); -G_GNUC_INTERNAL +SYSPROF_INTERNAL void *mapped_ring_buffer_allocate (MappedRingBuffer *self, size_t length); -G_GNUC_INTERNAL +SYSPROF_INTERNAL void mapped_ring_buffer_advance (MappedRingBuffer *self, size_t length); -G_GNUC_INTERNAL +SYSPROF_INTERNAL bool mapped_ring_buffer_drain (MappedRingBuffer *self, MappedRingBufferCallback callback, void *user_data); -G_GNUC_INTERNAL +SYSPROF_INTERNAL bool mapped_ring_buffer_is_empty (MappedRingBuffer *self); SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index cc430909..c6c31507 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -240,17 +240,16 @@ SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_writer_cat (SysprofCaptureWriter *self, SysprofCaptureReader *reader, GError **error); -G_GNUC_INTERNAL +SYSPROF_INTERNAL bool _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, const SysprofCaptureFrame *frame); -G_GNUC_INTERNAL +SYSPROF_INTERNAL bool _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, int fd, - GError **error) G_GNUC_INTERNAL; -G_GNUC_INTERNAL + GError **error) SYSPROF_INTERNAL; +SYSPROF_INTERNAL bool _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, int64_t start_time, - int64_t end_time) G_GNUC_INTERNAL; - + int64_t end_time) SYSPROF_INTERNAL; SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-macros.h b/src/libsysprof-capture/sysprof-macros.h index d5fd7325..241a41e0 100644 --- a/src/libsysprof-capture/sysprof-macros.h +++ b/src/libsysprof-capture/sysprof-macros.h @@ -66,3 +66,9 @@ #define SYSPROF_BEGIN_DECLS #define SYSPROF_END_DECLS #endif + +#if defined (__GNUC__) +#define SYSPROF_INTERNAL __attribute__((visibility("hidden"))) +#else +#define SYSPROF_INTERNAL +#endif From f925fab56441a7a7815f545122a2ca8cb78834dd Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 16:52:19 +0100 Subject: [PATCH 15/62] libsysprof-capture: Replace G_{UN,}LIKELY with SYSPROF_{UN,}LIKELY This does the same thing for modern compilers, but without the GLib dependency. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-condition.c | 2 +- .../sysprof-capture-reader.c | 26 +++++++++---------- src/libsysprof-capture/sysprof-capture-util.c | 3 ++- .../sysprof-capture-writer.c | 4 +-- src/libsysprof-capture/sysprof-clock.h | 2 +- src/libsysprof-capture/sysprof-collector.c | 10 +++---- src/libsysprof-capture/sysprof-macros.h | 8 ++++++ 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index ea3aea6e..febeeba6 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -340,7 +340,7 @@ sysprof_capture_condition_new_where_time_between (int64_t begin_time, { SysprofCaptureCondition *self; - if G_UNLIKELY (begin_time > end_time) + if SYSPROF_UNLIKELY (begin_time > end_time) { int64_t tmp = begin_time; begin_time = end_time; diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 59ce530c..52225472 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -282,7 +282,7 @@ sysprof_capture_reader_bswap_frame (SysprofCaptureReader *self, assert (self != NULL); assert (frame!= NULL); - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) { frame->len = GUINT16_SWAP_LE_BE (frame->len); frame->cpu = GUINT16_SWAP_LE_BE (frame->cpu); @@ -298,7 +298,7 @@ sysprof_capture_reader_bswap_file_chunk (SysprofCaptureReader *self, assert (self != NULL); assert (file_chunk != NULL); - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) file_chunk->len = GUINT16_SWAP_LE_BE (file_chunk->len); } @@ -309,7 +309,7 @@ sysprof_capture_reader_bswap_log (SysprofCaptureReader *self, assert (self != NULL); assert (log != NULL); - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) log->severity = GUINT16_SWAP_LE_BE (log->severity); } @@ -320,7 +320,7 @@ sysprof_capture_reader_bswap_map (SysprofCaptureReader *self, assert (self != NULL); assert (map != NULL); - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) { map->start = GUINT64_SWAP_LE_BE (map->start); map->end = GUINT64_SWAP_LE_BE (map->end); @@ -336,7 +336,7 @@ sysprof_capture_reader_bswap_mark (SysprofCaptureReader *self, assert (self != NULL); assert (mark != NULL); - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) mark->duration = GUINT64_SWAP_LE_BE (mark->duration); } @@ -347,7 +347,7 @@ sysprof_capture_reader_bswap_jitmap (SysprofCaptureReader *self, assert (self != NULL); assert (jitmap != NULL); - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) jitmap->n_jitmaps = GUINT64_SWAP_LE_BE (jitmap->n_jitmaps); } @@ -531,7 +531,7 @@ sysprof_capture_reader_read_fork (SysprofCaptureReader *self) if (fk != NULL) { - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) fk->child_pid = GUINT32_SWAP_LE_BE (fk->child_pid); } @@ -660,7 +660,7 @@ sysprof_capture_reader_read_mark (SysprofCaptureReader *self) ((char *)mark)[mark->frame.len - 1] = 0; /* Maybe update end-time */ - if G_UNLIKELY ((mark->frame.time + mark->duration) > self->end_time) + if SYSPROF_UNLIKELY ((mark->frame.time + mark->duration) > self->end_time) self->end_time = mark->frame.time + mark->duration; return mark; @@ -843,7 +843,7 @@ sysprof_capture_reader_read_sample (SysprofCaptureReader *self) sample = (SysprofCaptureSample *)(void *)&self->buf[self->pos]; - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) { unsigned int i; @@ -876,7 +876,7 @@ sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self) if (def->frame.len < sizeof *def) return NULL; - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) def->n_counters = GUINT16_SWAP_LE_BE (def->n_counters); if (def->frame.len < (sizeof *def + (sizeof (SysprofCaptureCounterDefine) * def->n_counters))) @@ -887,7 +887,7 @@ sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self) def = (SysprofCaptureCounterDefine *)(void *)&self->buf[self->pos]; - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) { unsigned int i; @@ -934,7 +934,7 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) set = (SysprofCaptureCounterSet *)(void *)&self->buf[self->pos]; - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) { unsigned int i; @@ -1424,7 +1424,7 @@ sysprof_capture_reader_read_allocation (SysprofCaptureReader *self) ma = (SysprofCaptureAllocation *)(void *)&self->buf[self->pos]; - if (G_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) { for (unsigned int i = 0; i < ma->n_addrs; i++) ma->addrs[i] = GUINT64_SWAP_LE_BE (ma->addrs[i]); diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index 3281172a..75f9b264 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -70,6 +70,7 @@ #endif #include "sysprof-capture-util-private.h" +#include "sysprof-macros.h" #ifdef G_OS_WIN32 static G_LOCK_DEFINE (_sysprof_io_sync); @@ -80,7 +81,7 @@ size_t { static size_t pgsz = 0; - if G_UNLIKELY (pgsz == 0) + if SYSPROF_UNLIKELY (pgsz == 0) { #ifdef G_OS_WIN32 SYSTEM_INFO system_info; diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 16bf7530..d4aa2fc8 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -433,7 +433,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, { SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i]; - if (G_LIKELY (bucket->str == NULL)) + if (SYSPROF_LIKELY (bucket->str == NULL)) { bucket->str = dst; bucket->addr = addr; @@ -447,7 +447,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, { SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i]; - if (G_LIKELY (bucket->str == NULL)) + if (SYSPROF_LIKELY (bucket->str == NULL)) { bucket->str = dst; bucket->addr = addr; diff --git a/src/libsysprof-capture/sysprof-clock.h b/src/libsysprof-capture/sysprof-clock.h index 54695cba..523b5968 100644 --- a/src/libsysprof-capture/sysprof-clock.h +++ b/src/libsysprof-capture/sysprof-clock.h @@ -80,7 +80,7 @@ sysprof_clock_get_current_time (void) struct timespec ts; SysprofClock clock = sysprof_clock; - if G_UNLIKELY (clock == -1) + if SYSPROF_UNLIKELY (clock == -1) clock = CLOCK_MONOTONIC; clock_gettime (clock, &ts); diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 938a9508..9c72d0bc 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -224,10 +224,10 @@ sysprof_collector_get (void) const SysprofCollector *collector = g_private_get (&collector_key); /* We might have gotten here recursively */ - if G_UNLIKELY (collector == COLLECTOR_INVALID) + if SYSPROF_UNLIKELY (collector == COLLECTOR_INVALID) return COLLECTOR_INVALID; - if G_LIKELY (collector != NULL) + if SYSPROF_LIKELY (collector != NULL) return collector; if (use_single_trace () && shared_collector != COLLECTOR_INVALID) @@ -278,9 +278,9 @@ sysprof_collector_init (void) #define COLLECTOR_BEGIN \ G_STMT_START { \ const SysprofCollector *collector = sysprof_collector_get (); \ - if G_LIKELY (collector->buffer) \ + if SYSPROF_LIKELY (collector->buffer) \ { \ - if G_UNLIKELY (collector->is_shared) \ + if SYSPROF_UNLIKELY (collector->is_shared) \ G_LOCK (control_fd); \ \ { @@ -288,7 +288,7 @@ sysprof_collector_init (void) #define COLLECTOR_END \ } \ \ - if G_UNLIKELY (collector->is_shared) \ + if SYSPROF_UNLIKELY (collector->is_shared) \ G_UNLOCK (control_fd); \ } \ } G_STMT_END diff --git a/src/libsysprof-capture/sysprof-macros.h b/src/libsysprof-capture/sysprof-macros.h index 241a41e0..a7bcc04f 100644 --- a/src/libsysprof-capture/sysprof-macros.h +++ b/src/libsysprof-capture/sysprof-macros.h @@ -72,3 +72,11 @@ #else #define SYSPROF_INTERNAL #endif + +#if defined(__GNUC__) +#define SYSPROF_LIKELY(expr) (__builtin_expect (!!(expr), 1)) +#define SYSPROF_UNLIKELY(expr) (__builtin_expect (!!(expr), 0)) +#else +#define SYSPROF_LIKELY(expr) (expr) +#define SYSPROF_UNLIKELY(expr) (expr) +#endif From b6fb2728655ba837ccdd4545d74cc71d9cc032c0 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 16:55:03 +0100 Subject: [PATCH 16/62] libsysprof-capture: Use fprintf() instead of g_printerr() This removes one more dependency on GLib. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index fb1890b3..1b0fff4d 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -251,35 +252,35 @@ mapped_ring_buffer_new_writer (int fd) /* Make our own copy of the FD */ if ((fd = dup (fd)) < 0) { - g_printerr ("Failed to dup() fd, cannot continue\n"); + fprintf (stderr, "Failed to dup() fd, cannot continue\n"); return NULL; } /* Seek to end to get buffer size */ if ((buffer_size = lseek (fd, 0, SEEK_END)) < 0) { - g_printerr ("Failed to seek to end of file. Cannot determine buffer size.\n"); + fprintf (stderr, "Failed to seek to end of file. Cannot determine buffer size.\n"); return NULL; } /* Ensure non-zero sized buffer */ if (buffer_size < (page_size + page_size)) { - g_printerr ("Buffer is too small, cannot continue.\n"); + fprintf (stderr, "Buffer is too small, cannot continue.\n"); return NULL; } /* Make sure it is less than our max size */ if ((buffer_size - page_size) > BUFFER_MAX_SIZE) { - g_printerr ("Buffer is too large, cannot continue.\n"); + fprintf (stderr, "Buffer is too large, cannot continue.\n"); return NULL; } /* Ensure we have page-aligned buffer */ if ((buffer_size % page_size) != 0) { - g_printerr ("Invalid buffer size, not page aligned.\n"); + fprintf (stderr, "Invalid buffer size, not page aligned.\n"); return NULL; } From 951b46fddf30ded3d396da6a3d89905adc24173f Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 17:00:36 +0100 Subject: [PATCH 17/62] libsysprof-capture: Use intrinsic atomics rather than g_atomic_*() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the intrinsic atomics provided by the compiler, instead of GLib’s wrapper around them. This should work for all modern compilers. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 25 +++++++++++-------- .../sysprof-capture-condition.c | 4 +-- .../sysprof-capture-cursor.c | 4 +-- .../sysprof-capture-reader.c | 4 +-- .../sysprof-capture-writer.c | 4 +-- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index 1b0fff4d..1c170c61 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -334,7 +334,7 @@ mapped_ring_buffer_unref (MappedRingBuffer *self) assert (self != NULL); assert (self->ref_count > 0); - if (g_atomic_int_dec_and_test (&self->ref_count)) + if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1) mapped_ring_buffer_finalize (self); } @@ -344,7 +344,7 @@ mapped_ring_buffer_ref (MappedRingBuffer *self) assert (self != NULL); assert (self->ref_count > 0); - g_atomic_int_inc (&self->ref_count); + __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST); return self; } @@ -395,8 +395,8 @@ mapped_ring_buffer_allocate (MappedRingBuffer *self, assert ((length & 0x7) == 0); header = get_header (self); - headpos = g_atomic_int_get (&header->head); - tailpos = g_atomic_int_get (&header->tail); + __atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST); + __atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST); /* We need to check that there is enough space for @length at the * current position in the write buffer. We cannot fully catch up @@ -463,7 +463,7 @@ mapped_ring_buffer_advance (MappedRingBuffer *self, * we just update the position as the only way the head could have * moved is forward. */ - g_atomic_int_set (&header->tail, tail); + __atomic_store (&header->tail, &tail, __ATOMIC_SEQ_CST); } /** @@ -494,8 +494,8 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, assert (callback != NULL); header = get_header (self); - headpos = g_atomic_int_get (&header->head); - tailpos = g_atomic_int_get (&header->tail); + __atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST); + __atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST); assert (headpos < self->body_size); assert (tailpos < self->body_size); @@ -515,6 +515,7 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, { const void *data = get_body_at_pos (self, headpos); size_t len = tailpos - headpos; + uint32_t new_headpos; if (!callback (data, &len, user_data)) return false; @@ -525,9 +526,11 @@ mapped_ring_buffer_drain (MappedRingBuffer *self, headpos += len; if (headpos >= self->body_size) - g_atomic_int_set (&header->head, headpos - self->body_size); + new_headpos = headpos - self->body_size; else - g_atomic_int_set (&header->head, headpos); + new_headpos = headpos; + + __atomic_store (&header->head, &new_headpos, __ATOMIC_SEQ_CST); } return true; @@ -552,8 +555,8 @@ mapped_ring_buffer_is_empty (MappedRingBuffer *self) header = get_header (self); - headpos = g_atomic_int_get (&header->head); - tailpos = g_atomic_int_get (&header->tail); + __atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST); + __atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST); return headpos == tailpos; } diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index febeeba6..99219060 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -303,7 +303,7 @@ sysprof_capture_condition_ref (SysprofCaptureCondition *self) assert (self != NULL); assert (self->ref_count > 0); - g_atomic_int_inc (&self->ref_count); + __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST); return self; } @@ -313,7 +313,7 @@ sysprof_capture_condition_unref (SysprofCaptureCondition *self) assert (self != NULL); assert (self->ref_count > 0); - if (g_atomic_int_dec_and_test (&self->ref_count)) + if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1) sysprof_capture_condition_finalize (self); } diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index 62b20722..a6af3845 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -110,7 +110,7 @@ sysprof_capture_cursor_ref (SysprofCaptureCursor *self) assert (self != NULL); assert (self->ref_count > 0); - g_atomic_int_inc (&self->ref_count); + __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST); return self; } @@ -126,7 +126,7 @@ sysprof_capture_cursor_unref (SysprofCaptureCursor *self) assert (self != NULL); assert (self->ref_count > 0); - if (g_atomic_int_dec_and_test (&self->ref_count)) + if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1) sysprof_capture_cursor_finalize (self); } diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 52225472..6c4862e9 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -973,7 +973,7 @@ sysprof_capture_reader_ref (SysprofCaptureReader *self) assert (self != NULL); assert (self->ref_count > 0); - g_atomic_int_inc (&self->ref_count); + __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST); return self; } @@ -984,7 +984,7 @@ sysprof_capture_reader_unref (SysprofCaptureReader *self) assert (self != NULL); assert (self->ref_count > 0); - if (g_atomic_int_dec_and_test (&self->ref_count)) + if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1) sysprof_capture_reader_finalize (self); } diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index d4aa2fc8..5036eda1 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -192,7 +192,7 @@ sysprof_capture_writer_ref (SysprofCaptureWriter *self) assert (self != NULL); assert (self->ref_count > 0); - g_atomic_int_inc (&self->ref_count); + __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST); return self; } @@ -203,7 +203,7 @@ sysprof_capture_writer_unref (SysprofCaptureWriter *self) assert (self != NULL); assert (self->ref_count > 0); - if (g_atomic_int_dec_and_test (&self->ref_count)) + if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1) sysprof_capture_writer_finalize (self); } From 6e281dca1f113ad6746a6f205633d38ff6c84ac6 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 17:08:16 +0100 Subject: [PATCH 18/62] libsysprof-capture: Use strlcpy() instead of g_strlcpy() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the system doesn’t provide `strlcpy()` (FreeBSD does, Linux doesn’t), use an inbuilt copy instead. Signed-off-by: Philip Withnall Helps: #40 --- meson.build | 2 ++ .../sysprof-capture-util-private.h | 9 ++++++++- src/libsysprof-capture/sysprof-capture-util.c | 20 +++++++++++++++++++ .../sysprof-capture-writer.c | 16 +++++++-------- src/libsysprof-capture/sysprof-collector.c | 8 ++++---- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/meson.build b/meson.build index 90dc5fbd..6550126a 100644 --- a/meson.build +++ b/meson.build @@ -91,6 +91,8 @@ if cc.has_header('execinfo.h') config_h.set10('HAVE_EXECINFO_H', true) endif +config_h.set('HAVE_STRLCPY', cc.has_function('strlcpy')) + if get_option('libunwind') libunwind_dep = dependency('libunwind-generic', required: false) if libunwind_dep.found() diff --git a/src/libsysprof-capture/sysprof-capture-util-private.h b/src/libsysprof-capture/sysprof-capture-util-private.h index 26ba79e1..44c7d0dc 100644 --- a/src/libsysprof-capture/sysprof-capture-util-private.h +++ b/src/libsysprof-capture/sysprof-capture-util-private.h @@ -89,5 +89,12 @@ ssize_t _sysprof_sendfile (int out_fd, int in_fd, off_t *offset, size_t count); - +#endif + +#ifdef HAVE_STRLCPY +# define _sysprof_strlcpy(d,s,ds) strlcpy(d,s,ds) +#else +size_t _sysprof_strlcpy (char *dest, + const char *src, + size_t dest_size); #endif diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index 75f9b264..d2e9835c 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -236,3 +236,23 @@ ssize_t errno = 0; return total; } + +size_t +(_sysprof_strlcpy) (char *dest, + const char *src, + size_t dest_size) +{ + size_t i = 0; + + if (dest_size > 0) + { + for (; i < dest_size - 1 && src[i] != '\0'; i++) + dest[i] = src[i]; + dest[i] = '\0'; + } + + for (; src[i] != '\0'; i++) + ; + + return i; +} diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 5036eda1..23f00243 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -519,7 +519,7 @@ sysprof_capture_writer_new_from_fd (int fd, header->little_endian = false; #endif header->padding = 0; - g_strlcpy (header->capture_time, nowstr, sizeof header->capture_time); + _sysprof_strlcpy (header->capture_time, now_str, sizeof header->capture_time); header->time = SYSPROF_CAPTURE_CURRENT_TIME; header->end_time = 0; memset (header->suffix, 0, sizeof header->suffix); @@ -599,7 +599,7 @@ sysprof_capture_writer_add_map (SysprofCaptureWriter *self, ev->offset = offset; ev->inode = inode; - g_strlcpy (ev->filename, filename, len - sizeof *ev); + _sysprof_strlcpy (ev->filename, filename, len - sizeof *ev); ev->filename[len - sizeof *ev - 1] = '\0'; self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MAP]++; @@ -642,8 +642,8 @@ sysprof_capture_writer_add_mark (SysprofCaptureWriter *self, SYSPROF_CAPTURE_FRAME_MARK); ev->duration = duration; - g_strlcpy (ev->group, group, sizeof ev->group); - g_strlcpy (ev->name, name, sizeof ev->name); + _sysprof_strlcpy (ev->group, group, sizeof ev->group); + _sysprof_strlcpy (ev->name, name, sizeof ev->name); memcpy (ev->message, message, message_len); self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MARK]++; @@ -687,7 +687,7 @@ sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self, time, SYSPROF_CAPTURE_FRAME_METADATA); - g_strlcpy (ev->id, id, sizeof ev->id); + _sysprof_strlcpy (ev->id, id, sizeof ev->id); memcpy (ev->metadata, metadata, metadata_len); ev->metadata[metadata_len] = 0; @@ -743,7 +743,7 @@ sysprof_capture_writer_add_process (SysprofCaptureWriter *self, time, SYSPROF_CAPTURE_FRAME_PROCESS); - g_strlcpy (ev->cmdline, cmdline, len - sizeof *ev); + _sysprof_strlcpy (ev->cmdline, cmdline, len - sizeof *ev); ev->cmdline[len - sizeof *ev - 1] = '\0'; self->stat.frame_count[SYSPROF_CAPTURE_FRAME_PROCESS]++; @@ -1409,7 +1409,7 @@ sysprof_capture_writer_add_log (SysprofCaptureWriter *self, ev->severity = severity & 0xFFFF; ev->padding1 = 0; ev->padding2 = 0; - g_strlcpy (ev->domain, domain, sizeof ev->domain); + _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain); memcpy (ev->message, message, message_len); self->stat.frame_count[SYSPROF_CAPTURE_FRAME_LOG]++; @@ -1447,7 +1447,7 @@ sysprof_capture_writer_add_file (SysprofCaptureWriter *self, ev->padding1 = 0; ev->is_last = !!is_last; ev->len = data_len; - g_strlcpy (ev->path, path, sizeof ev->path); + _sysprof_strlcpy (ev->path, path, sizeof ev->path); memcpy (ev->data, data, data_len); self->stat.frame_count[SYSPROF_CAPTURE_FRAME_FILE_CHUNK]++; diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 9c72d0bc..db177567 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -406,8 +406,8 @@ sysprof_collector_mark (int64_t time, ev->frame.pid = collector->pid; ev->frame.time = time; ev->duration = duration; - g_strlcpy (ev->group, group, sizeof ev->group); - g_strlcpy (ev->name, mark, sizeof ev->name); + _sysprof_strlcpy (ev->group, group, sizeof ev->group); + _sysprof_strlcpy (ev->name, mark, sizeof ev->name); memcpy (ev->message, message, sl); ev->message[sl] = 0; @@ -446,7 +446,7 @@ sysprof_collector_log (int severity, ev->severity = severity & 0xFFFF; ev->padding1 = 0; ev->padding2 = 0; - g_strlcpy (ev->domain, domain, sizeof ev->domain); + _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain); memcpy (ev->message, message, sl); ev->message[sl] = 0; @@ -489,7 +489,7 @@ sysprof_collector_log_printf (int severity, ev->severity = severity & 0xFFFF; ev->padding1 = 0; ev->padding2 = 0; - g_strlcpy (ev->domain, domain, sizeof ev->domain); + _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain); memcpy (ev->message, formatted, sl); ev->message[sl] = 0; From b0a5c4f7009f77d54a8e9bfee300c69218602427 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 17:24:50 +0100 Subject: [PATCH 19/62] libsysprof-capture: Use malloc() rather than g_new0() and friends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Another step away from GLib. This changes the OOM behaviour of the library — previously it would immediately `abort()` on OOM. However, it seems likely that given the small number of allocations libsysprof-capture does, it should be able to recover from an OOM situation more gracefully than larger libraries can — so the new implementation tries to do that. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 14 ++++++- .../sysprof-capture-condition.c | 8 +++- .../sysprof-capture-cursor.c | 8 +++- .../sysprof-capture-reader.c | 38 ++++++++++++++++--- .../sysprof-capture-util-private.h | 12 ++++++ .../sysprof-capture-writer.c | 16 ++++++-- src/libsysprof-capture/sysprof-collector.c | 8 +++- 7 files changed, 86 insertions(+), 18 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index 1c170c61..81a37f95 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -203,7 +203,10 @@ mapped_ring_buffer_new_reader (size_t buffer_size) header->offset = page_size; header->size = buffer_size - page_size; - self = g_slice_new0 (MappedRingBuffer); + self = sysprof_malloc0 (sizeof (MappedRingBuffer)); + if (self == NULL) + return NULL; + self->ref_count = 1; self->mode = MODE_READER; self->body_size = buffer_size - page_size; @@ -301,7 +304,14 @@ mapped_ring_buffer_new_writer (int fd) return NULL; } - self = g_slice_new0 (MappedRingBuffer); + self = sysprof_malloc0 (sizeof (MappedRingBuffer)); + if (self == NULL) + { + munmap (map, page_size + ((buffer_size - page_size) * 2)); + close (fd); + return NULL; + } + self->ref_count = 1; self->mode = MODE_WRITER; self->fd = fd; diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 99219060..1755e3db 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -63,6 +63,7 @@ #include #include "sysprof-capture-condition.h" +#include "sysprof-capture-util-private.h" #include "sysprof-macros-internal.h" /** @@ -204,7 +205,10 @@ sysprof_capture_condition_init (void) { SysprofCaptureCondition *self; - self = g_slice_new0 (SysprofCaptureCondition); + self = sysprof_malloc0 (sizeof (SysprofCaptureCondition)); + if (self == NULL) + return NULL; + self->ref_count = 1; return g_steal_pointer (&self); @@ -294,7 +298,7 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self) break; } - g_slice_free (SysprofCaptureCondition, self); + free (self); } SysprofCaptureCondition * diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index a6af3845..5a81d790 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -63,6 +63,7 @@ #include "sysprof-capture-condition.h" #include "sysprof-capture-cursor.h" #include "sysprof-capture-reader.h" +#include "sysprof-capture-util-private.h" #define READ_DELEGATE(f) ((ReadDelegate)(f)) @@ -81,7 +82,7 @@ sysprof_capture_cursor_finalize (SysprofCaptureCursor *self) { g_clear_pointer (&self->conditions, g_ptr_array_unref); g_clear_pointer (&self->reader, sysprof_capture_reader_unref); - g_slice_free (SysprofCaptureCursor, self); + free (self); } static SysprofCaptureCursor * @@ -89,7 +90,10 @@ sysprof_capture_cursor_init (void) { SysprofCaptureCursor *self; - self = g_slice_new0 (SysprofCaptureCursor); + self = sysprof_malloc0 (sizeof (SysprofCaptureCursor)); + if (self == NULL) + return NULL; + self->conditions = g_ptr_array_new_with_free_func ((GDestroyNotify) sysprof_capture_condition_unref); self->ref_count = 1; diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 6c4862e9..3553efc0 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -126,9 +126,9 @@ sysprof_capture_reader_finalize (SysprofCaptureReader *self) if (self != NULL) { close (self->fd); - g_free (self->buf); + free (self->buf); g_free (self->filename); - g_free (self); + free (self); } } @@ -216,10 +216,23 @@ sysprof_capture_reader_new_from_fd (int fd, assert (fd > -1); - self = g_new0 (SysprofCaptureReader, 1); + self = sysprof_malloc0 (sizeof (SysprofCaptureReader)); + if (self == NULL) + { + g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM, "No memory"); + return NULL; + } + self->ref_count = 1; self->bufsz = USHRT_MAX * 2; - self->buf = g_malloc (self->bufsz); + self->buf = sysprof_malloc0 (self->bufsz); + if (self->buf == NULL) + { + free (self); + g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM, "No memory"); + return NULL; + } + self->len = 0; self->pos = 0; self->fd = fd; @@ -1156,7 +1169,12 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self) if (-1 == (fd = dup (self->fd))) return NULL; - copy = g_new0 (SysprofCaptureReader, 1); + copy = sysprof_malloc0 (sizeof (SysprofCaptureReader)); + if (copy == NULL) + { + close (fd); + return NULL; + } *copy = *self; @@ -1167,7 +1185,15 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self) copy->st_buf = self->st_buf; copy->st_buf_set = self->st_buf_set; - copy->buf = g_malloc (self->bufsz); + copy->buf = malloc (self->bufsz); + if (copy->buf == NULL) + { + close (fd); + g_free (copy->filename); + free (copy); + return NULL; + } + memcpy (copy->buf, self->buf, self->bufsz); return copy; diff --git a/src/libsysprof-capture/sysprof-capture-util-private.h b/src/libsysprof-capture/sysprof-capture-util-private.h index 44c7d0dc..47c59d13 100644 --- a/src/libsysprof-capture/sysprof-capture-util-private.h +++ b/src/libsysprof-capture/sysprof-capture-util-private.h @@ -62,8 +62,20 @@ # include #endif +#include +#include #include +static inline void * +sysprof_malloc0 (size_t size) +{ + void *ptr = malloc (size); + if (ptr == NULL) + return NULL; + memset (ptr, 0, size); + return ptr; +} + #ifdef __linux__ # define _sysprof_getpagesize() getpagesize() # define _sysprof_pread(a,b,c,d) pread(a,b,c,d) diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 23f00243..e74e6c61 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -181,8 +181,8 @@ sysprof_capture_writer_finalize (SysprofCaptureWriter *self) self->fd = -1; } - g_free (self->buf); - g_free (self); + free (self->buf); + free (self); } } @@ -483,10 +483,18 @@ sysprof_capture_writer_new_from_fd (int fd, /* This is only useful on files, memfd, etc */ if (ftruncate (fd, 0) != 0) { /* Do Nothing */ } - self = g_new0 (SysprofCaptureWriter, 1); + self = sysprof_malloc0 (sizeof (SysprofCaptureWriter)); + if (self == NULL) + return NULL; + self->ref_count = 1; self->fd = fd; - self->buf = (guint8 *)g_malloc0 (buffer_size); + self->buf = (uint8_t *) sysprof_malloc0 (buffer_size); + if (self->buf == NULL) + { + free (self); + return NULL; + } self->len = buffer_size; self->next_counter_id = 1; diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index db177567..482d5362 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -75,6 +75,7 @@ #include "mapped-ring-buffer.h" +#include "sysprof-capture-util-private.h" #include "sysprof-collector.h" #define MAX_UNWIND_DEPTH 128 @@ -214,7 +215,7 @@ sysprof_collector_free (void *data) mapped_ring_buffer_unref (buffer); } - g_free (collector); + free (collector); } } @@ -238,9 +239,12 @@ sysprof_collector_get (void) g_private_replace (&collector_key, COLLECTOR_INVALID); + self = sysprof_malloc0 (sizeof (SysprofCollector)); + if (self == NULL) + return COLLECTOR_INVALID; + G_LOCK (control_fd); - self = g_new0 (SysprofCollector, 1); self->pid = getpid (); #ifdef __linux__ self->tid = syscall (__NR_gettid, 0); From 0e4b3f52b135d7e819776624ae169a885414f073 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 10:29:59 +0100 Subject: [PATCH 20/62] libsysprof-capture: Handle OOM conditions in SysprofCaptureCondition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This doesn’t change any of the sites which call `sysprof_capture_condition_*()` in other files, but does change `SysprofCaptureCondition` internally to handle OOM and return an error code. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-condition.c | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 1755e3db..33d45d0c 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -214,6 +214,7 @@ sysprof_capture_condition_init (void) return g_steal_pointer (&self); } +/* Returns NULL on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_copy (const SysprofCaptureCondition *self) { @@ -321,6 +322,7 @@ sysprof_capture_condition_unref (SysprofCaptureCondition *self) sysprof_capture_condition_finalize (self); } +/* Returns NULL on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_new_where_type_in (unsigned int n_types, const SysprofCaptureFrameType *types) @@ -330,6 +332,9 @@ sysprof_capture_condition_new_where_type_in (unsigned int n_ty assert (types != NULL); self = sysprof_capture_condition_init (); + if (self == NULL) + return NULL; + self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN; self->u.where_type_in = g_array_sized_new (FALSE, FALSE, sizeof (SysprofCaptureFrameType), n_types); g_array_set_size (self->u.where_type_in, n_types); @@ -338,6 +343,7 @@ sysprof_capture_condition_new_where_type_in (unsigned int n_ty return self; } +/* Returns NULL on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_new_where_time_between (int64_t begin_time, int64_t end_time) @@ -352,6 +358,9 @@ sysprof_capture_condition_new_where_time_between (int64_t begin_time, } self = sysprof_capture_condition_init (); + if (self == NULL) + return NULL; + self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN; self->u.where_time_between.begin = begin_time; self->u.where_time_between.end = end_time; @@ -359,6 +368,7 @@ sysprof_capture_condition_new_where_time_between (int64_t begin_time, return self; } +/* Returns NULL on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_new_where_pid_in (unsigned int n_pids, const int32_t *pids) @@ -368,6 +378,9 @@ sysprof_capture_condition_new_where_pid_in (unsigned int n_pids, assert (pids != NULL); self = sysprof_capture_condition_init (); + if (self == NULL) + return NULL; + self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN; self->u.where_pid_in = g_array_sized_new (FALSE, FALSE, sizeof (int32_t), n_pids); g_array_set_size (self->u.where_pid_in, n_pids); @@ -376,6 +389,7 @@ sysprof_capture_condition_new_where_pid_in (unsigned int n_pids, return self; } +/* Returns NULL on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_new_where_counter_in (unsigned int n_counters, const unsigned int *counters) @@ -385,6 +399,9 @@ sysprof_capture_condition_new_where_counter_in (unsigned int n_counters, assert (counters != NULL || n_counters == 0); self = sysprof_capture_condition_init (); + if (self == NULL) + return NULL; + self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN; self->u.where_counter_in = g_array_sized_new (FALSE, FALSE, sizeof (unsigned int), n_counters); @@ -405,7 +422,8 @@ sysprof_capture_condition_new_where_counter_in (unsigned int n_counters, * Creates a new #SysprofCaptureCondition that requires both left and right * to evaluate to %TRUE. * - * Returns: (transfer full): A new #SysprofCaptureCondition. + * Returns: (transfer full) (nullable): A new #SysprofCaptureCondition, or %NULL + * on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_new_and (SysprofCaptureCondition *left, @@ -417,6 +435,9 @@ sysprof_capture_condition_new_and (SysprofCaptureCondition *left, assert (right != NULL); self = sysprof_capture_condition_init (); + if (self == NULL) + return NULL; + self->type = SYSPROF_CAPTURE_CONDITION_AND; self->u.and.left = left; self->u.and.right = right; @@ -432,7 +453,8 @@ sysprof_capture_condition_new_and (SysprofCaptureCondition *left, * Creates a new #SysprofCaptureCondition that requires either left and right * to evaluate to %TRUE. * - * Returns: (transfer full): A new #SysprofCaptureCondition. + * Returns: (transfer full) (nullable): A new #SysprofCaptureCondition, or %NULL + * on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_new_or (SysprofCaptureCondition *left, @@ -444,6 +466,9 @@ sysprof_capture_condition_new_or (SysprofCaptureCondition *left, assert (right != NULL); self = sysprof_capture_condition_init (); + if (self == NULL) + return NULL; + self->type = SYSPROF_CAPTURE_CONDITION_OR; self->u.or.left = left; self->u.or.right = right; @@ -458,7 +483,8 @@ sysprof_capture_condition_new_or (SysprofCaptureCondition *left, * Creates a new condition that matches #SysprofCaptureFileChunk frames * which contain the path @path. * - * Returns: (transfer full): a new #SysprofCaptureCondition + * Returns: (transfer full) (nullable): a new #SysprofCaptureCondition, or %NULL + * on allocation failure. */ SysprofCaptureCondition * sysprof_capture_condition_new_where_file (const char *path) @@ -468,6 +494,9 @@ sysprof_capture_condition_new_where_file (const char *path) assert (path != NULL); self = sysprof_capture_condition_init (); + if (self == NULL) + return NULL; + self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE; self->u.where_file = g_strdup (path); From e96e35b6f16261aa9b4c0538a73c3edd64360de4 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 17:28:53 +0100 Subject: [PATCH 21/62] libsysprof-capture: Stop using GArray in SysprofCaptureCondition The code is well-suited to directly using `calloc()` instead, since the arrays never grow dynamically. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-condition.c | 82 ++++++++++++------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 33d45d0c..7e2ee2f9 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -60,6 +60,7 @@ #include #include +#include #include #include "sysprof-capture-condition.h" @@ -93,13 +94,22 @@ struct _SysprofCaptureCondition volatile int ref_count; SysprofCaptureConditionType type; union { - GArray *where_type_in; + struct { + SysprofCaptureFrameType *data; + size_t len; + } where_type_in; struct { int64_t begin; int64_t end; } where_time_between; - GArray *where_pid_in; - GArray *where_counter_in; + struct { + int32_t *data; + size_t len; + } where_pid_in; + struct { + unsigned int *data; + size_t len; + } where_counter_in; struct { SysprofCaptureCondition *left; SysprofCaptureCondition *right; @@ -126,9 +136,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, sysprof_capture_condition_match (self->u.or.right, frame); case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: - for (size_t i = 0; i < self->u.where_type_in->len; i++) + for (size_t i = 0; i < self->u.where_type_in.len; i++) { - if (frame->type == g_array_index (self->u.where_type_in, SysprofCaptureFrameType, i)) + if (frame->type == self->u.where_type_in.data[i]) return true; } return false; @@ -137,9 +147,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, return (frame->time >= self->u.where_time_between.begin && frame->time <= self->u.where_time_between.end); case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: - for (size_t i = 0; i < self->u.where_pid_in->len; i++) + for (size_t i = 0; i < self->u.where_pid_in.len; i++) { - if (frame->pid == g_array_index (self->u.where_pid_in, int32_t, i)) + if (frame->pid == self->u.where_pid_in.data[i]) return true; } return false; @@ -149,9 +159,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, { const SysprofCaptureCounterSet *set = (SysprofCaptureCounterSet *)frame; - for (size_t i = 0; i < self->u.where_counter_in->len; i++) + for (size_t i = 0; i < self->u.where_counter_in.len; i++) { - unsigned int counter = g_array_index (self->u.where_counter_in, unsigned int, i); + unsigned int counter = self->u.where_counter_in.data[i]; for (unsigned int j = 0; j < set->n_values; j++) { @@ -171,9 +181,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, { const SysprofCaptureCounterDefine *def = (SysprofCaptureCounterDefine *)frame; - for (size_t i = 0; i < self->u.where_counter_in->len; i++) + for (size_t i = 0; i < self->u.where_counter_in.len; i++) { - unsigned int counter = g_array_index (self->u.where_counter_in, unsigned int, i); + unsigned int counter = self->u.where_counter_in.data[i]; for (unsigned int j = 0; j < def->n_counters; j++) { @@ -232,8 +242,8 @@ sysprof_capture_condition_copy (const SysprofCaptureCondition *self) case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: return sysprof_capture_condition_new_where_type_in ( - self->u.where_type_in->len, - (const SysprofCaptureFrameType *)(void *)self->u.where_type_in->data); + self->u.where_type_in.len, + self->u.where_type_in.data); case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN: return sysprof_capture_condition_new_where_time_between ( @@ -242,13 +252,13 @@ sysprof_capture_condition_copy (const SysprofCaptureCondition *self) case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: return sysprof_capture_condition_new_where_pid_in ( - self->u.where_pid_in->len, - (const int32_t *)(void *)self->u.where_pid_in->data); + self->u.where_pid_in.len, + self->u.where_pid_in.data); case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN: return sysprof_capture_condition_new_where_counter_in ( - self->u.where_counter_in->len, - (const unsigned int *)(void *)self->u.where_counter_in->data); + self->u.where_counter_in.len, + self->u.where_counter_in.data); case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: return sysprof_capture_condition_new_where_file (self->u.where_file); @@ -276,18 +286,18 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self) break; case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: - g_array_free (self->u.where_type_in, TRUE); + free (self->u.where_type_in.data); break; case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN: break; case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: - g_array_free (self->u.where_pid_in, TRUE); + free (self->u.where_pid_in.data); break; case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN: - g_array_free (self->u.where_counter_in, TRUE); + free (self->u.where_counter_in.data); break; case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: @@ -336,9 +346,11 @@ sysprof_capture_condition_new_where_type_in (unsigned int n_ty return NULL; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN; - self->u.where_type_in = g_array_sized_new (FALSE, FALSE, sizeof (SysprofCaptureFrameType), n_types); - g_array_set_size (self->u.where_type_in, n_types); - memcpy (self->u.where_type_in->data, types, sizeof (SysprofCaptureFrameType) * n_types); + self->u.where_type_in.data = calloc (n_types, sizeof (SysprofCaptureFrameType)); + if (self->u.where_type_in.data == NULL) + return NULL; + self->u.where_type_in.len = n_types; + memcpy (self->u.where_type_in.data, types, sizeof (SysprofCaptureFrameType) * n_types); return self; } @@ -382,9 +394,14 @@ sysprof_capture_condition_new_where_pid_in (unsigned int n_pids, return NULL; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN; - self->u.where_pid_in = g_array_sized_new (FALSE, FALSE, sizeof (int32_t), n_pids); - g_array_set_size (self->u.where_pid_in, n_pids); - memcpy (self->u.where_pid_in->data, pids, sizeof (int32_t) * n_pids); + self->u.where_pid_in.data = calloc (n_pids, sizeof (int32_t)); + if (self->u.where_pid_in.data == NULL) + { + free (self); + return NULL; + } + self->u.where_pid_in.len = n_pids; + memcpy (self->u.where_pid_in.data, pids, sizeof (int32_t) * n_pids); return self; } @@ -403,13 +420,16 @@ sysprof_capture_condition_new_where_counter_in (unsigned int n_counters, return NULL; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN; - self->u.where_counter_in = g_array_sized_new (FALSE, FALSE, sizeof (unsigned int), n_counters); + self->u.where_counter_in.data = calloc (n_counters, sizeof (unsigned int)); + if (n_counters > 0 && self->u.where_counter_in.data == NULL) + { + free (self); + return NULL; + } + self->u.where_counter_in.len = n_counters; if (n_counters > 0) - { - g_array_set_size (self->u.where_counter_in, n_counters); - memcpy (self->u.where_counter_in->data, counters, sizeof (unsigned int) * n_counters); - } + memcpy (self->u.where_counter_in.data, counters, sizeof (unsigned int) * n_counters); return self; } From 1d865c5c8ea58725988ca7b06447b45ece66a588 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 17:41:03 +0100 Subject: [PATCH 22/62] libsysprof-capture: Use sysprof_{steal,clear}_pointer() instead of GLib They work exactly the same way as the GLib functions. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 5 +++-- src/libsysprof-capture/sysprof-capture-condition.c | 2 +- src/libsysprof-capture/sysprof-capture-cursor.c | 7 ++++--- src/libsysprof-capture/sysprof-capture-reader.c | 4 ++-- src/libsysprof-capture/sysprof-capture-writer-cat.c | 4 +++- src/libsysprof-capture/sysprof-capture-writer.c | 6 +++--- src/libsysprof-capture/sysprof-collector.c | 5 +++-- src/libsysprof-capture/sysprof-macros-internal.h | 11 +++++++++++ 8 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index 81a37f95..b2e4e6a8 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -30,6 +30,7 @@ #include #include "sysprof-capture-util-private.h" +#include "sysprof-macros-internal.h" #include "sysprof-platform.h" #include "mapped-ring-buffer.h" @@ -214,7 +215,7 @@ mapped_ring_buffer_new_reader (size_t buffer_size) self->map = map; self->page_size = page_size; - return g_steal_pointer (&self); + return sysprof_steal_pointer (&self); } MappedRingBuffer * @@ -319,7 +320,7 @@ mapped_ring_buffer_new_writer (int fd) self->map = map; self->page_size = page_size; - return g_steal_pointer (&self); + return sysprof_steal_pointer (&self); } static void diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 7e2ee2f9..debb189a 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -221,7 +221,7 @@ sysprof_capture_condition_init (void) self->ref_count = 1; - return g_steal_pointer (&self); + return sysprof_steal_pointer (&self); } /* Returns NULL on allocation failure. */ diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index 5a81d790..3786d67d 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -64,6 +64,7 @@ #include "sysprof-capture-cursor.h" #include "sysprof-capture-reader.h" #include "sysprof-capture-util-private.h" +#include "sysprof-macros-internal.h" #define READ_DELEGATE(f) ((ReadDelegate)(f)) @@ -80,8 +81,8 @@ struct _SysprofCaptureCursor static void sysprof_capture_cursor_finalize (SysprofCaptureCursor *self) { - g_clear_pointer (&self->conditions, g_ptr_array_unref); - g_clear_pointer (&self->reader, sysprof_capture_reader_unref); + sysprof_clear_pointer (&self->conditions, g_ptr_array_unref); + sysprof_clear_pointer (&self->reader, sysprof_capture_reader_unref); free (self); } @@ -97,7 +98,7 @@ sysprof_capture_cursor_init (void) self->conditions = g_ptr_array_new_with_free_func ((GDestroyNotify) sysprof_capture_condition_unref); self->ref_count = 1; - return g_steal_pointer (&self); + return sysprof_steal_pointer (&self); } /** diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 3553efc0..1ea372d1 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -820,7 +820,7 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) self->pos += jitmap->frame.len; - return g_steal_pointer (&ret); + return sysprof_steal_pointer (&ret); } const SysprofCaptureSample * @@ -1309,7 +1309,7 @@ sysprof_capture_reader_list_files (SysprofCaptureReader *self) g_ptr_array_add (ar, g_strdup (key)); g_ptr_array_add (ar, NULL); - return (char **)g_ptr_array_free (g_steal_pointer (&ar), FALSE); + return (char **)g_ptr_array_free (sysprof_steal_pointer (&ar), FALSE); } bool diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index d86ba7a7..b752b9c8 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -65,6 +65,8 @@ #include #include +#include "sysprof-macros-internal.h" + typedef struct { uint64_t src; @@ -81,7 +83,7 @@ static void translate_table_clear (GArray **tables, unsigned int table) { - g_clear_pointer (&tables[table], g_array_unref); + sysprof_clear_pointer (&tables[table], g_array_unref); } static int diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index e74e6c61..84cd86ef 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -171,7 +171,7 @@ sysprof_capture_writer_finalize (SysprofCaptureWriter *self) { if (self != NULL) { - g_clear_pointer (&self->periodic_flush, g_source_destroy); + sysprof_clear_pointer (&self->periodic_flush, g_source_destroy); sysprof_capture_writer_flush (self); @@ -1154,7 +1154,7 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, if ((ret = sysprof_capture_reader_new_from_fd (copy, error))) sysprof_capture_reader_set_stat (ret, &self->stat); - return g_steal_pointer (&ret); + return sysprof_steal_pointer (&ret); } /** @@ -1516,7 +1516,7 @@ sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self, g_return_if_fail (self != NULL); - g_clear_pointer (&self->periodic_flush, g_source_destroy); + sysprof_clear_pointer (&self->periodic_flush, g_source_destroy); if (timeout_seconds == 0) return; diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 482d5362..f398df03 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -77,6 +77,7 @@ #include "sysprof-capture-util-private.h" #include "sysprof-collector.h" +#include "sysprof-macros-internal.h" #define MAX_UNWIND_DEPTH 128 #define CREATRING "CreatRing\0" @@ -179,7 +180,7 @@ request_writer (void) } } - return g_steal_pointer (&buffer); + return sysprof_steal_pointer (&buffer); } static void @@ -207,7 +208,7 @@ sysprof_collector_free (void *data) if (collector != NULL && collector != COLLECTOR_INVALID) { - MappedRingBuffer *buffer = g_steal_pointer (&collector->buffer); + MappedRingBuffer *buffer = sysprof_steal_pointer (&collector->buffer); if (buffer != NULL) { diff --git a/src/libsysprof-capture/sysprof-macros-internal.h b/src/libsysprof-capture/sysprof-macros-internal.h index f227a230..eacec0da 100644 --- a/src/libsysprof-capture/sysprof-macros-internal.h +++ b/src/libsysprof-capture/sysprof-macros-internal.h @@ -60,3 +60,14 @@ #pragma once #define sysprof_assert_not_reached() assert (false) + +#define sysprof_steal_pointer(pp) __extension__ ({__typeof(*(pp)) _p = *(pp); *(pp) = NULL; _p;}) + +#define sysprof_clear_pointer(pp, destroy) \ + do { \ + __typeof((pp)) _pp = (pp); \ + __typeof(*(pp)) _p = *_pp; \ + *_pp = NULL; \ + if (_p != NULL) \ + (destroy) (_p); \ + } while (0) From e26eae5bcf31720d873febabbb7f387c2d137fd3 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 1 Jul 2020 17:45:09 +0100 Subject: [PATCH 23/62] libsysprof-capture: Use libc string functions rather than GLib ones Use `strcmp()` and `strdup()` rather than `g_strcmp0()` and `g_strdup()`. In the latter case, this makes no difference. In the former case it means we potentially need to do some additional `NULL` checks before calling it; although most of the call sites use fixed-length arrays, so no `NULL` check is needed. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-condition.c | 14 +++++++++++--- src/libsysprof-capture/sysprof-capture-reader.c | 14 +++++++------- src/libsysprof-capture/sysprof-capture-writer.c | 17 +++++++++++++++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index debb189a..27141db5 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -199,7 +199,10 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self, if (frame->type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK) return false; - return g_strcmp0 (((const SysprofCaptureFileChunk *)frame)->path, self->u.where_file) == 0; + if (self->u.where_file == NULL) + return false; + + return strcmp (((const SysprofCaptureFileChunk *)frame)->path, self->u.where_file) == 0; default: break; @@ -301,7 +304,7 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self) break; case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: - g_free (self->u.where_file); + free (self->u.where_file); break; default: @@ -518,7 +521,12 @@ sysprof_capture_condition_new_where_file (const char *path) return NULL; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE; - self->u.where_file = g_strdup (path); + self->u.where_file = strdup (path); + if (self->u.where_file == NULL) + { + free (self); + return NULL; + } return self; } diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 1ea372d1..3243f803 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -127,7 +127,7 @@ sysprof_capture_reader_finalize (SysprofCaptureReader *self) { close (self->fd); free (self->buf); - g_free (self->filename); + free (self->filename); free (self); } } @@ -283,7 +283,7 @@ sysprof_capture_reader_new (const char *filename, return NULL; } - self->filename = g_strdup (filename); + self->filename = strdup (filename); return self; } @@ -1086,7 +1086,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, } if (self->filename == NULL) - self->filename = g_strdup (filename); + self->filename = strdup (filename); close (fd); @@ -1179,7 +1179,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self) *copy = *self; copy->ref_count = 1; - copy->filename = g_strdup (self->filename); + copy->filename = strdup (self->filename); copy->fd = fd; copy->end_time = self->end_time; copy->st_buf = self->st_buf; @@ -1189,7 +1189,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self) if (copy->buf == NULL) { close (fd); - g_free (copy->filename); + free (copy->filename); free (copy); return NULL; } @@ -1337,7 +1337,7 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, if (!(file = sysprof_capture_reader_read_file (self))) return false; - if (g_strcmp0 (path, file->path) != 0) + if (strcmp (path, file->path) != 0) goto skip; buf = file->data; @@ -1399,7 +1399,7 @@ sysprof_capture_reader_find_file (SysprofCaptureReader *self, if (!(fc = sysprof_capture_reader_read_file (self))) break; - if (g_strcmp0 (path, fc->path) == 0) + if (strcmp (path, fc->path) == 0) return fc; continue; diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 84cd86ef..cb8c90a0 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -333,6 +333,19 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self) return true; } +/* djb hash */ +static unsigned int +str_hash (const char *str) +{ + const uint8_t *p; + uint32_t h = 5381; + + for (p = (const uint8_t *) str; *p != '\0'; p++) + h = (h << 5) + h + *p; + + return h; +} + static bool sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, const char *name, @@ -345,7 +358,7 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, assert (name != NULL); assert (addr != NULL); - hash = g_str_hash (name) % G_N_ELEMENTS (self->addr_hash); + hash = str_hash (name) % G_N_ELEMENTS (self->addr_hash); for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++) { @@ -426,7 +439,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, assert (self->addr_buf_pos <= sizeof self->addr_buf); /* Now place the address into the hashtable */ - hash = g_str_hash (str) % G_N_ELEMENTS (self->addr_hash); + hash = str_hash (str) % G_N_ELEMENTS (self->addr_hash); /* Start from the current hash bucket and go forward */ for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++) From e4813cd7292807961259de6c2321f5fbd7950ef9 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 09:54:16 +0100 Subject: [PATCH 24/62] libsysprof-capture: Assert rather than calling g_warning() Calling this function without having registered the counter beforehand seems reasonable to call a programmer error. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-writer.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index cb8c90a0..8cd88984 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -1224,12 +1224,8 @@ sysprof_capture_writer_define_counters (SysprofCaptureWriter *self, for (i = 0; i < n_counters; i++) { - if (counters[i].id >= self->next_counter_id) - { - g_warning ("Counter %u has not been registered.", counters[i].id); - continue; - } - + /* Has the counter been registered? */ + assert (counters[i].id < self->next_counter_id); def->counters[i] = counters[i]; } From 607af7367668ae4f2be5c721e4368abb5826beb4 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 09:56:27 +0100 Subject: [PATCH 25/62] libsysprof-capture: Stop defining G_LOG_DOMAIN libsysprof-capture no longer calls any of the GLib logging functions which make use of it. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/mapped-ring-buffer.c | 2 -- src/libsysprof-capture/sysprof-capture-condition.c | 2 -- src/libsysprof-capture/sysprof-capture-cursor.c | 2 -- src/libsysprof-capture/sysprof-capture-reader.c | 2 -- src/libsysprof-capture/sysprof-capture-util.c | 2 -- src/libsysprof-capture/sysprof-capture-writer-cat.c | 2 -- src/libsysprof-capture/sysprof-capture-writer.c | 2 -- src/libsysprof-capture/sysprof-clock.c | 2 -- src/libsysprof-capture/sysprof-collector.c | 2 -- src/libsysprof-capture/sysprof-platform.c | 2 -- 10 files changed, 20 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c index b2e4e6a8..b7b56796 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.c +++ b/src/libsysprof-capture/mapped-ring-buffer.c @@ -18,8 +18,6 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -#define G_LOG_DOMAIN "mapped-ring-buffer" - #include "config.h" #include diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 27141db5..5322e091 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-capture-condition" - #include "config.h" #include diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index 3786d67d..085e1fa0 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-capture-cursor" - #include "config.h" #include diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 3243f803..94de783b 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-capture-reader" - #include "config.h" #include diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index d2e9835c..f1563074 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-capture-util" - #include "config.h" #include diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index b752b9c8..2cd71f4b 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-cat" - #include "config.h" #include diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 8cd88984..d85b5f5a 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-capture-writer" - #include "config.h" #ifndef _GNU_SOURCE diff --git a/src/libsysprof-capture/sysprof-clock.c b/src/libsysprof-capture/sysprof-clock.c index e65b025d..7543e027 100644 --- a/src/libsysprof-capture/sysprof-clock.c +++ b/src/libsysprof-capture/sysprof-clock.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-clock" - #include "config.h" #include diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index f398df03..2b7ffec7 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-collector" - #ifndef _GNU_SOURCE # define _GNU_SOURCE #endif diff --git a/src/libsysprof-capture/sysprof-platform.c b/src/libsysprof-capture/sysprof-platform.c index 51464d19..3449cfaa 100644 --- a/src/libsysprof-capture/sysprof-platform.c +++ b/src/libsysprof-capture/sysprof-platform.c @@ -54,8 +54,6 @@ * SPDX-License-Identifier: BSD-2-Clause-Patent */ -#define G_LOG_DOMAIN "sysprof-platform" - #include "config.h" #include From b509daaa42c9e4208d977959483f1bb15a301363 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 09:59:08 +0100 Subject: [PATCH 26/62] libsysprof-capture: Use reallocarray() instead of GPtrArray in cursor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are typically 0 or 1 conditions applied to a cursor, and they’re only applied at construction time, so it makes sense to only grow the conditions array linearly when it’s used. This means the array may stay as `NULL` throughout the lifetime of the cursor. There’s currently no way to return an error from `sysprof_capture_cursor_add_condition()`, so an allocation failure results in an abort. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-cursor.c | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index 085e1fa0..24563f0a 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -57,6 +57,7 @@ #include "config.h" #include +#include #include "sysprof-capture-condition.h" #include "sysprof-capture-cursor.h" @@ -71,7 +72,8 @@ typedef const SysprofCaptureFrame *(*ReadDelegate) (SysprofCaptureReader *); struct _SysprofCaptureCursor { volatile int ref_count; - GPtrArray *conditions; + SysprofCaptureCondition **conditions; /* (nullable) (owned) */ + size_t n_conditions; SysprofCaptureReader *reader; unsigned int reversed : 1; }; @@ -79,7 +81,9 @@ struct _SysprofCaptureCursor static void sysprof_capture_cursor_finalize (SysprofCaptureCursor *self) { - sysprof_clear_pointer (&self->conditions, g_ptr_array_unref); + for (size_t i = 0; i < self->n_conditions; i++) + sysprof_capture_condition_unref (self->conditions[i]); + sysprof_clear_pointer (&self->conditions, free); sysprof_clear_pointer (&self->reader, sysprof_capture_reader_unref); free (self); } @@ -93,7 +97,8 @@ sysprof_capture_cursor_init (void) if (self == NULL) return NULL; - self->conditions = g_ptr_array_new_with_free_func ((GDestroyNotify) sysprof_capture_condition_unref); + self->conditions = NULL; + self->n_conditions = 0; self->ref_count = 1; return sysprof_steal_pointer (&self); @@ -229,16 +234,16 @@ sysprof_capture_cursor_foreach (SysprofCaptureCursor *self, if (NULL == (frame = delegate (self->reader))) return; - if (self->conditions->len == 0) + if (self->n_conditions == 0) { if (!callback (frame, user_data)) return; } else { - for (size_t i = 0; i < self->conditions->len; i++) + for (size_t i = 0; i < self->n_conditions; i++) { - const SysprofCaptureCondition *condition = g_ptr_array_index (self->conditions, i); + const SysprofCaptureCondition *condition = self->conditions[i]; if (sysprof_capture_condition_match (condition, frame)) { @@ -283,7 +288,15 @@ sysprof_capture_cursor_add_condition (SysprofCaptureCursor *self, assert (self != NULL); assert (condition != NULL); - g_ptr_array_add (self->conditions, condition); + /* Grow the array linearly to keep the code simple: there are typically 0 or 1 + * conditions applied to a given cursor, just after it’s constructed. + * + * FIXME: There’s currently no error reporting from this function, so ENOMEM + * results in an abort. */ + self->conditions = reallocarray (self->conditions, ++self->n_conditions, sizeof (*self->conditions)); + assert (self->conditions != NULL); + + self->conditions[self->n_conditions - 1] = sysprof_steal_pointer (&condition); } /** From 0785f32ce5413719d991a40e10a16adafe0f9358 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 10:13:26 +0100 Subject: [PATCH 27/62] libsysprof-capture: Use SYSPROF_N_ELEMENTS macro instead of G_N_ELEMENTS It does the same thing. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-reader.c | 2 +- .../sysprof-capture-writer-cat.c | 2 +- .../sysprof-capture-writer.c | 20 +++++++++---------- src/libsysprof-capture/sysprof-clock.c | 2 +- .../sysprof-macros-internal.h | 2 ++ 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 94de783b..537c3a94 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -953,7 +953,7 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) { unsigned int j; - for (j = 0; j < G_N_ELEMENTS (set->values[0].values); j++) + for (j = 0; j < SYSPROF_N_ELEMENTS (set->values[0].values); j++) { set->values[i].ids[j] = GUINT32_SWAP_LE_BE (set->values[i].ids[j]); set->values[i].values[j].v64 = GUINT64_SWAP_LE_BE (set->values[i].values[j].v64); diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 2cd71f4b..3fbd5858 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -446,7 +446,7 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, { const SysprofCaptureCounterValues *v = &frame->values[z]; - for (unsigned int y = 0; y < G_N_ELEMENTS (v->ids); y++) + for (unsigned int y = 0; y < SYSPROF_N_ELEMENTS (v->ids); y++) { if (v->ids[y]) { diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index d85b5f5a..67713063 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -356,9 +356,9 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, assert (name != NULL); assert (addr != NULL); - hash = str_hash (name) % G_N_ELEMENTS (self->addr_hash); + hash = str_hash (name) % SYSPROF_N_ELEMENTS (self->addr_hash); - for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++) + for (i = hash; i < SYSPROF_N_ELEMENTS (self->addr_hash); i++) { SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i]; @@ -405,7 +405,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, len = sizeof addr + strlen (str) + 1; - if ((self->addr_hash_size == G_N_ELEMENTS (self->addr_hash)) || + if ((self->addr_hash_size == SYSPROF_N_ELEMENTS (self->addr_hash)) || ((sizeof self->addr_buf - self->addr_buf_pos) < len)) { if (!sysprof_capture_writer_flush_jitmap (self)) @@ -415,7 +415,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, assert (self->addr_buf_pos == 0); } - assert (self->addr_hash_size < G_N_ELEMENTS (self->addr_hash)); + assert (self->addr_hash_size < SYSPROF_N_ELEMENTS (self->addr_hash)); assert (len > sizeof addr); /* Allocate the next unique address */ @@ -437,10 +437,10 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self, assert (self->addr_buf_pos <= sizeof self->addr_buf); /* Now place the address into the hashtable */ - hash = str_hash (str) % G_N_ELEMENTS (self->addr_hash); + hash = str_hash (str) % SYSPROF_N_ELEMENTS (self->addr_hash); /* Start from the current hash bucket and go forward */ - for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++) + for (i = hash; i < SYSPROF_N_ELEMENTS (self->addr_hash); i++) { SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i]; @@ -1256,8 +1256,8 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, return true; /* Determine how many value groups we need */ - n_groups = n_counters / G_N_ELEMENTS (set->values[0].values); - if ((n_groups * G_N_ELEMENTS (set->values[0].values)) != n_counters) + n_groups = n_counters / SYSPROF_N_ELEMENTS (set->values[0].values); + if ((n_groups * SYSPROF_N_ELEMENTS (set->values[0].values)) != n_counters) n_groups++; len = sizeof *set + (n_groups * sizeof (SysprofCaptureCounterValues)); @@ -1285,7 +1285,7 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter *self, field++; - if (field == G_N_ELEMENTS (set->values[0].values)) + if (field == SYSPROF_N_ELEMENTS (set->values[0].values)) { field = 0; group++; @@ -1659,7 +1659,7 @@ _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, memcpy (begin, fr, fr->len); - if (fr->type < G_N_ELEMENTS (self->stat.frame_count)) + if (fr->type < SYSPROF_N_ELEMENTS (self->stat.frame_count)) self->stat.frame_count[fr->type]++; return true; diff --git a/src/libsysprof-capture/sysprof-clock.c b/src/libsysprof-capture/sysprof-clock.c index 7543e027..cbaafa1c 100644 --- a/src/libsysprof-capture/sysprof-clock.c +++ b/src/libsysprof-capture/sysprof-clock.c @@ -80,7 +80,7 @@ sysprof_clock_init (void) if (sysprof_clock != -1) return; - for (unsigned int i = 0; i < G_N_ELEMENTS (clock_ids); i++) + for (unsigned int i = 0; i < SYSPROF_N_ELEMENTS (clock_ids); i++) { struct timespec ts; int clock_id = clock_ids [i]; diff --git a/src/libsysprof-capture/sysprof-macros-internal.h b/src/libsysprof-capture/sysprof-macros-internal.h index eacec0da..9fed4652 100644 --- a/src/libsysprof-capture/sysprof-macros-internal.h +++ b/src/libsysprof-capture/sysprof-macros-internal.h @@ -61,6 +61,8 @@ #define sysprof_assert_not_reached() assert (false) +#define SYSPROF_N_ELEMENTS(a) (sizeof (a) / sizeof (*a)) + #define sysprof_steal_pointer(pp) __extension__ ({__typeof(*(pp)) _p = *(pp); *(pp) = NULL; _p;}) #define sysprof_clear_pointer(pp, destroy) \ From c64dfe9cacedec483eac0396e427f154a0114741 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 10:22:46 +0100 Subject: [PATCH 28/62] libsysprof-capture: Add missing header to define NULL Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-address.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsysprof-capture/sysprof-address.c b/src/libsysprof-capture/sysprof-address.c index 0ddb68ce..1b125015 100644 --- a/src/libsysprof-capture/sysprof-address.c +++ b/src/libsysprof-capture/sysprof-address.c @@ -62,6 +62,7 @@ # include "sysprof-address-fallback.h" #endif #include +#include #include "sysprof-address.h" From 9493fa2e0336aa9bc685c4f46e4718a4c6f57206 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 10:51:35 +0100 Subject: [PATCH 29/62] libsysprof-capture: Use endianness macros from libc rather than GLib They should be equivalent. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-reader.c | 96 ++++++++++--------- .../sysprof-capture-types.h | 3 +- .../sysprof-capture-writer.c | 3 +- 3 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 537c3a94..0cd80f93 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -57,6 +57,8 @@ #include "config.h" #include +#include +#include #include #include #include @@ -243,9 +245,9 @@ sysprof_capture_reader_new_from_fd (int fd, } if (self->header.little_endian) - self->endian = G_LITTLE_ENDIAN; + self->endian = __LITTLE_ENDIAN; else - self->endian = G_BIG_ENDIAN; + self->endian = __BIG_ENDIAN; /* If we detect a capture file that did not get an end time, or an erroneous * end time, then we need to take a performance hit here and scan the file @@ -293,12 +295,12 @@ sysprof_capture_reader_bswap_frame (SysprofCaptureReader *self, assert (self != NULL); assert (frame!= NULL); - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) { - frame->len = GUINT16_SWAP_LE_BE (frame->len); - frame->cpu = GUINT16_SWAP_LE_BE (frame->cpu); - frame->pid = GUINT32_SWAP_LE_BE (frame->pid); - frame->time = GUINT64_SWAP_LE_BE (frame->time); + frame->len = bswap_16 (frame->len); + frame->cpu = bswap_16 (frame->cpu); + frame->pid = bswap_32 (frame->pid); + frame->time = bswap_64 (frame->time); } } @@ -309,8 +311,8 @@ sysprof_capture_reader_bswap_file_chunk (SysprofCaptureReader *self, assert (self != NULL); assert (file_chunk != NULL); - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) - file_chunk->len = GUINT16_SWAP_LE_BE (file_chunk->len); + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) + file_chunk->len = bswap_16 (file_chunk->len); } static inline void @@ -320,8 +322,8 @@ sysprof_capture_reader_bswap_log (SysprofCaptureReader *self, assert (self != NULL); assert (log != NULL); - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) - log->severity = GUINT16_SWAP_LE_BE (log->severity); + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) + log->severity = bswap_16 (log->severity); } static inline void @@ -331,12 +333,12 @@ sysprof_capture_reader_bswap_map (SysprofCaptureReader *self, assert (self != NULL); assert (map != NULL); - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) { - map->start = GUINT64_SWAP_LE_BE (map->start); - map->end = GUINT64_SWAP_LE_BE (map->end); - map->offset = GUINT64_SWAP_LE_BE (map->offset); - map->inode = GUINT64_SWAP_LE_BE (map->inode); + map->start = bswap_64 (map->start); + map->end = bswap_64 (map->end); + map->offset = bswap_64 (map->offset); + map->inode = bswap_64 (map->inode); } } @@ -347,8 +349,8 @@ sysprof_capture_reader_bswap_mark (SysprofCaptureReader *self, assert (self != NULL); assert (mark != NULL); - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) - mark->duration = GUINT64_SWAP_LE_BE (mark->duration); + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) + mark->duration = bswap_64 (mark->duration); } static inline void @@ -358,8 +360,8 @@ sysprof_capture_reader_bswap_jitmap (SysprofCaptureReader *self, assert (self != NULL); assert (jitmap != NULL); - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) - jitmap->n_jitmaps = GUINT64_SWAP_LE_BE (jitmap->n_jitmaps); + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) + jitmap->n_jitmaps = bswap_64 (jitmap->n_jitmaps); } static bool @@ -542,8 +544,8 @@ sysprof_capture_reader_read_fork (SysprofCaptureReader *self) if (fk != NULL) { - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) - fk->child_pid = GUINT32_SWAP_LE_BE (fk->child_pid); + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) + fk->child_pid = bswap_32 (fk->child_pid); } return fk; @@ -843,8 +845,8 @@ sysprof_capture_reader_read_sample (SysprofCaptureReader *self) if (sample->frame.len < sizeof *sample) return NULL; - if (self->endian != G_BYTE_ORDER) - sample->n_addrs = GUINT16_SWAP_LE_BE (sample->n_addrs); + if (self->endian != __BYTE_ORDER) + sample->n_addrs = bswap_16 (sample->n_addrs); if (sample->frame.len < (sizeof *sample + (sizeof(SysprofCaptureAddress) * sample->n_addrs))) return NULL; @@ -854,12 +856,12 @@ sysprof_capture_reader_read_sample (SysprofCaptureReader *self) sample = (SysprofCaptureSample *)(void *)&self->buf[self->pos]; - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) { unsigned int i; for (i = 0; i < sample->n_addrs; i++) - sample->addrs[i] = GUINT64_SWAP_LE_BE (sample->addrs[i]); + sample->addrs[i] = bswap_64 (sample->addrs[i]); } self->pos += sample->frame.len; @@ -887,8 +889,8 @@ sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self) if (def->frame.len < sizeof *def) return NULL; - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) - def->n_counters = GUINT16_SWAP_LE_BE (def->n_counters); + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) + def->n_counters = bswap_16 (def->n_counters); if (def->frame.len < (sizeof *def + (sizeof (SysprofCaptureCounterDefine) * def->n_counters))) return NULL; @@ -898,14 +900,14 @@ sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self) def = (SysprofCaptureCounterDefine *)(void *)&self->buf[self->pos]; - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) { unsigned int i; for (i = 0; i < def->n_counters; i++) { - def->counters[i].id = GUINT32_SWAP_LE_BE (def->counters[i].id); - def->counters[i].value.v64 = GUINT64_SWAP_LE_BE (def->counters[i].value.v64); + def->counters[i].id = bswap_32 (def->counters[i].id); + def->counters[i].value.v64 = bswap_64 (def->counters[i].value.v64); } } @@ -934,8 +936,8 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) if (set->frame.len < sizeof *set) return NULL; - if (self->endian != G_BYTE_ORDER) - set->n_values = GUINT16_SWAP_LE_BE (set->n_values); + if (self->endian != __BYTE_ORDER) + set->n_values = bswap_16 (set->n_values); if (set->frame.len < (sizeof *set + (sizeof (SysprofCaptureCounterValues) * set->n_values))) return NULL; @@ -945,7 +947,7 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) set = (SysprofCaptureCounterSet *)(void *)&self->buf[self->pos]; - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) { unsigned int i; @@ -955,8 +957,8 @@ sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self) for (j = 0; j < SYSPROF_N_ELEMENTS (set->values[0].values); j++) { - set->values[i].ids[j] = GUINT32_SWAP_LE_BE (set->values[i].ids[j]); - set->values[i].values[j].v64 = GUINT64_SWAP_LE_BE (set->values[i].values[j].v64); + set->values[i].ids[j] = bswap_32 (set->values[i].ids[j]); + set->values[i].values[j].v64 = bswap_64 (set->values[i].values[j].v64); } } } @@ -1107,8 +1109,8 @@ sysprof_capture_reader_get_start_time (SysprofCaptureReader *self) { assert (self != NULL); - if (self->endian != G_BYTE_ORDER) - return GUINT64_SWAP_LE_BE (self->header.time); + if (self->endian != __BYTE_ORDER) + return bswap_64 (self->header.time); return self->header.time; } @@ -1137,8 +1139,8 @@ sysprof_capture_reader_get_end_time (SysprofCaptureReader *self) if (self->header.end_time != 0) { - if (self->endian != G_BYTE_ORDER) - end_time = GUINT64_SWAP_LE_BE (self->header.end_time); + if (self->endian != __BYTE_ORDER) + end_time = bswap_64 (self->header.end_time); else end_time = self->header.end_time; } @@ -1432,12 +1434,12 @@ sysprof_capture_reader_read_allocation (SysprofCaptureReader *self) if (ma->frame.len < sizeof *ma) return NULL; - if (self->endian != G_BYTE_ORDER) + if (self->endian != __BYTE_ORDER) { - ma->n_addrs = GUINT16_SWAP_LE_BE (ma->n_addrs); - ma->alloc_size = GUINT64_SWAP_LE_BE (ma->alloc_size); - ma->alloc_addr = GUINT64_SWAP_LE_BE (ma->alloc_addr); - ma->tid = GUINT32_SWAP_LE_BE (ma->tid); + ma->n_addrs = bswap_16 (ma->n_addrs); + ma->alloc_size = bswap_64 (ma->alloc_size); + ma->alloc_addr = bswap_64 (ma->alloc_addr); + ma->tid = bswap_32 (ma->tid); } if (ma->frame.len < (sizeof *ma + (sizeof(SysprofCaptureAddress) * ma->n_addrs))) @@ -1448,10 +1450,10 @@ sysprof_capture_reader_read_allocation (SysprofCaptureReader *self) ma = (SysprofCaptureAllocation *)(void *)&self->buf[self->pos]; - if (SYSPROF_UNLIKELY (self->endian != G_BYTE_ORDER)) + if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER)) { for (unsigned int i = 0; i < ma->n_addrs; i++) - ma->addrs[i] = GUINT64_SWAP_LE_BE (ma->addrs[i]); + ma->addrs[i] = bswap_64 (ma->addrs[i]); } self->pos += ma->frame.len; diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index 109e7427..854ff838 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -57,6 +57,7 @@ #pragma once #include +#include #include #include #include @@ -67,7 +68,7 @@ SYSPROF_BEGIN_DECLS -#define SYSPROF_CAPTURE_MAGIC (GUINT32_TO_LE(0xFDCA975E)) +#define SYSPROF_CAPTURE_MAGIC (htole32(0xFDCA975E)) #define SYSPROF_CAPTURE_ALIGN (sizeof(SysprofCaptureAddress)) #if defined(_MSC_VER) diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 67713063..1e27729f 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -61,6 +61,7 @@ #endif #include +#include #include #include #include @@ -532,7 +533,7 @@ sysprof_capture_writer_new_from_fd (int fd, header->magic = SYSPROF_CAPTURE_MAGIC; header->version = 1; -#if G_BYTE_ORDER == G_LITTLE_ENDIAN +#if __BYTE_ORDER == __LITTLE_ENDIAN header->little_endian = true; #else header->little_endian = false; From 175c53aed8a6aa6590b9388406fab359e0ae8f76 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 10:56:02 +0100 Subject: [PATCH 30/62] libsysprof-capture: Use SYSPROF_PRINTF rather than G_GNUC_PRINTF It does the same thing for modern compilers. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-collector.h | 2 +- src/libsysprof-capture/sysprof-macros.h | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/libsysprof-capture/sysprof-collector.h b/src/libsysprof-capture/sysprof-collector.h index f59ca921..b91a5a7c 100644 --- a/src/libsysprof-capture/sysprof-collector.h +++ b/src/libsysprof-capture/sysprof-collector.h @@ -85,6 +85,6 @@ SYSPROF_AVAILABLE_IN_3_38 void sysprof_collector_log_printf (int severity, const char *domain, const char *format, - ...) G_GNUC_PRINTF (3, 4); + ...) SYSPROF_PRINTF (3, 4); SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-macros.h b/src/libsysprof-capture/sysprof-macros.h index a7bcc04f..1faa0022 100644 --- a/src/libsysprof-capture/sysprof-macros.h +++ b/src/libsysprof-capture/sysprof-macros.h @@ -80,3 +80,9 @@ #define SYSPROF_LIKELY(expr) (expr) #define SYSPROF_UNLIKELY(expr) (expr) #endif + +#if defined(__GNUC__) +#define SYSPROF_PRINTF(format_idx, arg_idx) __attribute__((format(printf, format_idx, arg_idx))) +#else +#define SYSPROF_PRINTF(format_idx, arg_idx) +#endif From 284b5fd7cffc5af758aa82588d054baf8841a45b Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 10:56:58 +0100 Subject: [PATCH 31/62] libsysprof-capture: Rework version macros to drop GLib dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They’re equivalent, using deprecation markers suitable for modern compilers. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-version-macros.h | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/libsysprof-capture/sysprof-version-macros.h b/src/libsysprof-capture/sysprof-version-macros.h index 8fa1df6f..8152735d 100644 --- a/src/libsysprof-capture/sysprof-version-macros.h +++ b/src/libsysprof-capture/sysprof-version-macros.h @@ -64,34 +64,48 @@ #define _SYSPROF_EXTERN extern #endif +#if defined(__GNUC__) || defined (__clang__) +#define _SYSPROF_DEPRECATED __attribute__((__deprecated__)) +#define _SYSPROF_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead"))) +#define _SYSPROF_UNAVAILABLE(maj,min) __attribute__((__deprecated__("Not available before " #maj "." #min))) +#elif defined(_MSC_VER) +#define _SYSPROF_DEPRECATED __declspec(deprecated) +#define _SYSPROF_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead")) +#define _SYSPROF_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min)) +#else +#define _SYSPROF_DEPRECATED +#define _SYSPROF_DEPRECATED_FOR(f) +#define _SYSPROF_UNAVAILABLE(maj,min) +#endif + #ifdef SYSPROF_DISABLE_DEPRECATION_WARNINGS #define SYSPROF_DEPRECATED _SYSPROF_EXTERN #define SYSPROF_DEPRECATED_FOR(f) _SYSPROF_EXTERN #define SYSPROF_UNAVAILABLE(maj,min) _SYSPROF_EXTERN #else -#define SYSPROF_DEPRECATED G_DEPRECATED _SYSPROF_EXTERN -#define SYSPROF_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _SYSPROF_EXTERN -#define SYSPROF_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _SYSPROF_EXTERN +#define SYSPROF_DEPRECATED _SYSPROF_DEPRECATED _SYSPROF_EXTERN +#define SYSPROF_DEPRECATED_FOR(f) _SYSPROF_DEPRECATED_FOR(f) _SYSPROF_EXTERN +#define SYSPROF_UNAVAILABLE(maj,min) _SYSPROF_UNAVAILABLE(maj,min) _SYSPROF_EXTERN #endif -#define SYSPROF_VERSION_3_34 (G_ENCODE_VERSION (3, 34)) -#define SYSPROF_VERSION_3_36 (G_ENCODE_VERSION (3, 36)) -#define SYSPROF_VERSION_3_38 (G_ENCODE_VERSION (3, 38)) +#define SYSPROF_VERSION_3_34 (SYSPROF_ENCODE_VERSION (3, 34, 0)) +#define SYSPROF_VERSION_3_36 (SYSPROF_ENCODE_VERSION (3, 36, 0)) +#define SYSPROF_VERSION_3_38 (SYSPROF_ENCODE_VERSION (3, 38, 0)) #if (SYSPROF_MINOR_VERSION == 99) -# define SYSPROF_VERSION_CUR_STABLE (G_ENCODE_VERSION (SYSPROF_MAJOR_VERSION + 1, 0)) +# define SYSPROF_VERSION_CUR_STABLE (SYSPROF_ENCODE_VERSION (SYSPROF_MAJOR_VERSION + 1, 0, 0)) #elif (SYSPROF_MINOR_VERSION % 2) -# define SYSPROF_VERSION_CUR_STABLE (G_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION + 1)) +# define SYSPROF_VERSION_CUR_STABLE (SYSPROF_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION + 1, 0)) #else -# define SYSPROF_VERSION_CUR_STABLE (G_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION)) +# define SYSPROF_VERSION_CUR_STABLE (SYSPROF_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION, 0)) #endif #if (SYSPROF_MINOR_VERSION == 99) -# define SYSPROF_VERSION_PREV_STABLE (G_ENCODE_VERSION (SYSPROF_MAJOR_VERSION + 1, 0)) +# define SYSPROF_VERSION_PREV_STABLE (SYSPROF_ENCODE_VERSION (SYSPROF_MAJOR_VERSION + 1, 0, 0)) #elif (SYSPROF_MINOR_VERSION % 2) -# define SYSPROF_VERSION_PREV_STABLE (G_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION - 1)) +# define SYSPROF_VERSION_PREV_STABLE (SYSPROF_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION - 1, 0)) #else -# define SYSPROF_VERSION_PREV_STABLE (G_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION - 2)) +# define SYSPROF_VERSION_PREV_STABLE (SYSPROF_ENCODE_VERSION (SYSPROF_MAJOR_VERSION, SYSPROF_MINOR_VERSION - 2, 0)) #endif /** From 3446628cecb2c341ab1ee620f25ce0d5384e5519 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 11:00:14 +0100 Subject: [PATCH 32/62] libsysprof-capture: Use offsetof() rather than G_STRUCT_OFFSET() It does the same for modern compilers. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-writer-cat.c | 2 +- src/libsysprof-capture/sysprof-capture-writer.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 3fbd5858..cec252a6 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -366,7 +366,7 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, frame->frame.pid, frame->id, frame->metadata, - frame->frame.len - G_STRUCT_OFFSET (SysprofCaptureMetadata, metadata)); + frame->frame.len - offsetof (SysprofCaptureMetadata, metadata)); break; } diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 1e27729f..953328e8 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -904,7 +904,7 @@ again: ret = _sysprof_pwrite (self->fd, &end_time, sizeof (end_time), - G_STRUCT_OFFSET (SysprofCaptureFileHeader, end_time)); + offsetof (SysprofCaptureFileHeader, end_time)); if (ret < 0 && errno == EAGAIN) goto again; @@ -1341,7 +1341,7 @@ do_start: ret = _sysprof_pwrite (self->fd, &start_time, sizeof (start_time), - G_STRUCT_OFFSET (SysprofCaptureFileHeader, time)); + offsetof (SysprofCaptureFileHeader, time)); if (ret < 0 && errno == EAGAIN) goto do_start; @@ -1350,7 +1350,7 @@ do_end: ret = _sysprof_pwrite (self->fd, &end_time, sizeof (end_time), - G_STRUCT_OFFSET (SysprofCaptureFileHeader, end_time)); + offsetof (SysprofCaptureFileHeader, end_time)); if (ret < 0 && errno == EAGAIN) goto do_end; From f1cb6400915e44137b226616a6dd1d6b0b524316 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 11:05:01 +0100 Subject: [PATCH 33/62] libsysprof-capture: Use reallocarray() instead of GArray in writer-cat.c This is more of a direct port than previous replacements of `GArray` (which have had linear growth): this one implements exponential growth of the allocated array, as `GArray` does. It uses `qsort()` to sort the array so that it can be binary searched as before. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-writer-cat.c | 65 ++++++++++++------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index cec252a6..459e0520 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -56,6 +56,7 @@ #include "config.h" +#include #include #include #include @@ -71,6 +72,13 @@ typedef struct uint64_t dst; } TranslateItem; +typedef struct +{ + TranslateItem *items; + size_t n_items; + size_t n_items_allocated; +} TranslateTable; + enum { TRANSLATE_ADDR, TRANSLATE_CTR, @@ -78,10 +86,14 @@ enum { }; static void -translate_table_clear (GArray **tables, - unsigned int table) +translate_table_clear (TranslateTable *tables, + unsigned int table) { - sysprof_clear_pointer (&tables[table], g_array_unref); + TranslateTable *table_ptr = &tables[table]; + + sysprof_clear_pointer (&table_ptr->items, free); + table_ptr->n_items = 0; + table_ptr->n_items_allocated = 0; } static int @@ -100,32 +112,41 @@ compare_by_src (const void *a, } static void -translate_table_sort (GArray **tables, - unsigned int table) +translate_table_sort (TranslateTable *tables, + unsigned int table) { - if (tables[table]) - g_array_sort (tables[table], compare_by_src); + TranslateTable *table_ptr = &tables[table]; + + if (table_ptr->items) + qsort (table_ptr->items, table_ptr->n_items, sizeof (*table_ptr->items), compare_by_src); } static void -translate_table_add (GArray **tables, - unsigned int table, - uint64_t src, - uint64_t dst) +translate_table_add (TranslateTable *tables, + unsigned int table, + uint64_t src, + uint64_t dst) { + TranslateTable *table_ptr = &tables[table]; const TranslateItem item = { src, dst }; - if (tables[table] == NULL) - tables[table] = g_array_new (FALSE, FALSE, sizeof (TranslateItem)); + if (table_ptr->n_items == table_ptr->n_items_allocated) + { + table_ptr->n_items_allocated = (table_ptr->n_items_allocated > 0) ? table_ptr->n_items_allocated * 2 : 4; + table_ptr->items = reallocarray (table_ptr->items, table_ptr->n_items_allocated, sizeof (*table_ptr->items)); + assert (table_ptr->items != NULL); + } - g_array_append_val (tables[table], item); + table_ptr->items[table_ptr->n_items++] = item; + assert (table_ptr->n_items <= table_ptr->n_items_allocated); } static uint64_t -translate_table_translate (GArray **tables, - unsigned int table, - uint64_t src) +translate_table_translate (TranslateTable *tables, + unsigned int table, + uint64_t src) { + TranslateTable *table_ptr = &tables[table]; const TranslateItem *item; TranslateItem key = { src, 0 }; @@ -135,13 +156,13 @@ translate_table_translate (GArray **tables, return src; } - if (tables[table] == NULL) + if (table_ptr->items == NULL) return src; item = bsearch (&key, - tables[table]->data, - tables[table]->len, - sizeof (TranslateItem), + table_ptr->items, + table_ptr->n_items, + sizeof (*table_ptr->items), compare_by_src); return item != NULL ? item->dst : src; @@ -152,7 +173,7 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, SysprofCaptureReader *reader, GError **error) { - GArray *tables[N_TRANSLATE] = { NULL }; + TranslateTable tables[N_TRANSLATE] = { 0, }; SysprofCaptureFrameType type; int64_t start_time; int64_t first_start_time = INT64_MAX; From 14078e6c4d506ab9f9ec27500af196e537ec325d Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 11:21:01 +0100 Subject: [PATCH 34/62] libsysprof-capture: Use SYSPROF_INT64_CONSTANT instead of GLib version Same for the unsigned version. They do the same thing as the GLib versions. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-types.h | 4 ++-- src/libsysprof-capture/sysprof-capture-writer.c | 3 ++- src/libsysprof-capture/sysprof-clock.h | 2 +- src/libsysprof-capture/sysprof-macros.h | 10 ++++++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index 854ff838..ae7f7ff8 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -82,9 +82,9 @@ SYSPROF_BEGIN_DECLS #define SYSPROF_CAPTURE_ADDRESS_FORMAT "0x%016" G_GINT64_MODIFIER "x" #if GLIB_SIZEOF_VOID_P == 8 -# define SYSPROF_CAPTURE_JITMAP_MARK G_GUINT64_CONSTANT(0xE000000000000000) +# define SYSPROF_CAPTURE_JITMAP_MARK SYSPROF_UINT64_CONSTANT(0xE000000000000000) #elif GLIB_SIZEOF_VOID_P == 4 -# define SYSPROF_CAPTURE_JITMAP_MARK G_GUINT64_CONSTANT(0xE0000000) +# define SYSPROF_CAPTURE_JITMAP_MARK SYSPROF_UINT64_CONSTANT(0xE0000000) #else #error Unknown GLIB_SIZEOF_VOID_P #endif diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 953328e8..88c5697a 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -76,9 +76,10 @@ #include "sysprof-capture-util-private.h" #include "sysprof-capture-writer.h" #include "sysprof-macros-internal.h" +#include "sysprof-macros.h" #define DEFAULT_BUFFER_SIZE (_sysprof_getpagesize() * 64L) -#define INVALID_ADDRESS (G_GUINT64_CONSTANT(0)) +#define INVALID_ADDRESS (SYSPROF_UINT64_CONSTANT(0)) #define MAX_COUNTERS ((1 << 24) - 1) #define MAX_UNWIND_DEPTH 64 diff --git a/src/libsysprof-capture/sysprof-clock.h b/src/libsysprof-capture/sysprof-clock.h index 523b5968..288a1fc0 100644 --- a/src/libsysprof-capture/sysprof-clock.h +++ b/src/libsysprof-capture/sysprof-clock.h @@ -69,7 +69,7 @@ typedef int SysprofClock; typedef int64_t SysprofTimeStamp; typedef int32_t SysprofTimeSysprofan; -#define SYSPROF_NSEC_PER_SEC G_GINT64_CONSTANT(1000000000) +#define SYSPROF_NSEC_PER_SEC SYSPROF_INT64_CONSTANT(1000000000) SYSPROF_AVAILABLE_IN_ALL SysprofClock sysprof_clock; diff --git a/src/libsysprof-capture/sysprof-macros.h b/src/libsysprof-capture/sysprof-macros.h index 1faa0022..be2afc1c 100644 --- a/src/libsysprof-capture/sysprof-macros.h +++ b/src/libsysprof-capture/sysprof-macros.h @@ -59,6 +59,16 @@ #pragma once +#include + +#if INT_MAX == LONG_MAX +#define SYSPROF_INT64_CONSTANT(x) x##ULL +#define SYSPROF_UINT64_CONSTANT(x) x##LL +#else +#define SYSPROF_INT64_CONSTANT(x) x##UL +#define SYSPROF_UINT64_CONSTANT(x) x##L +#endif + #ifdef __cplusplus #define SYSPROF_BEGIN_DECLS extern "C" { #define SYSPROF_END_DECLS } From 113d9d166a6d633a7f81264a02f0336d7c447254 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 11:23:51 +0100 Subject: [PATCH 35/62] libsysprof-capture: Use C-standard printf modifiers Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index ae7f7ff8..c5228395 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -79,7 +79,7 @@ SYSPROF_BEGIN_DECLS # define SYSPROF_ALIGNED_END(_N) __attribute__((aligned ((_N)))) #endif -#define SYSPROF_CAPTURE_ADDRESS_FORMAT "0x%016" G_GINT64_MODIFIER "x" +#define SYSPROF_CAPTURE_ADDRESS_FORMAT "0x%016" PRIx64 #if GLIB_SIZEOF_VOID_P == 8 # define SYSPROF_CAPTURE_JITMAP_MARK SYSPROF_UINT64_CONSTANT(0xE000000000000000) From 6d3ede2e9fdafe01f11ab609276106fe24c92205 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 11:32:09 +0100 Subject: [PATCH 36/62] libsysprof-capture: Avoid using GLIB_SIZEOF_VOID_P Use `UINTPTR_MAX` to calculate the size of a pointer instead. This assumes that all pointers are the same width. It also means that the sysprof-capture code uses the pointer size of the platform that libsysprof-capture was compiled on, rather than the pointer size of the platform that GLib was compiled on. They seem unlikely to differ. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-types.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index c5228395..c0d2e6e2 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -81,12 +81,14 @@ SYSPROF_BEGIN_DECLS #define SYSPROF_CAPTURE_ADDRESS_FORMAT "0x%016" PRIx64 -#if GLIB_SIZEOF_VOID_P == 8 +static_assert (sizeof (void *) == sizeof (uintptr_t), + "UINTPTR_MAX can’t be used to determine sizeof(void*) at compile time"); +#if UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu # define SYSPROF_CAPTURE_JITMAP_MARK SYSPROF_UINT64_CONSTANT(0xE000000000000000) -#elif GLIB_SIZEOF_VOID_P == 4 +#elif UINTPTR_MAX == 0xFFFFFFFF # define SYSPROF_CAPTURE_JITMAP_MARK SYSPROF_UINT64_CONSTANT(0xE0000000) #else -#error Unknown GLIB_SIZEOF_VOID_P +#error Unknown UINTPTR_MAX #endif #define SYSPROF_CAPTURE_CURRENT_TIME (sysprof_clock_get_current_time()) From e8a6474236f945d1d7675f0e25d68cfc5137eb84 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:15:04 +0100 Subject: [PATCH 37/62] libsysprof-capture: Use strftime() to format dates rather than GLib This means we lose support for local timezones other than UTC, but is otherwise equivalent. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-writer.c | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 88c5697a..ca3dd56c 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -478,8 +478,8 @@ SysprofCaptureWriter * sysprof_capture_writer_new_from_fd (int fd, size_t buffer_size) { - g_autofree gchar *nowstr = NULL; - g_autoptr(GDateTime) now = NULL; + time_t now; + char now_str[sizeof ("2020-06-30T14:34:00Z")]; SysprofCaptureWriter *self; SysprofCaptureFileHeader *header; size_t header_len = sizeof(*header); @@ -511,18 +511,14 @@ sysprof_capture_writer_new_from_fd (int fd, self->len = buffer_size; self->next_counter_id = 1; - now = g_date_time_new_now_local (); - -#if GLIB_CHECK_VERSION(2, 62, 0) - nowstr = g_date_time_format_iso8601 (now); -#else - { - GTimeVal tv; - - g_date_time_to_timeval (now, &tv); - nowstr = g_time_val_to_iso8601 (&tv); - } -#endif + /* Format the time as ISO 8601, in UTC */ + time (&now); + if (strftime (now_str, sizeof (now_str), "%FT%TZ", gmtime (&now)) == 0) + { + free (self->buf); + free (self); + return NULL; + } header = sysprof_capture_writer_allocate (self, &header_len); From f5cf12ae401da69ecf28b2743179b9ccb72f76db Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:18:24 +0100 Subject: [PATCH 38/62] libsysprof-capture: Use POSIX environment functions instead of GLib ones They are equivalent. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-writer.c | 2 +- src/libsysprof-capture/sysprof-collector.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index ca3dd56c..7926e6a4 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -1361,7 +1361,7 @@ sysprof_capture_writer_new_from_env (size_t buffer_size) const char *fdstr; int fd; - if (!(fdstr = g_getenv ("SYSPROF_TRACE_FD"))) + if (!(fdstr = getenv ("SYSPROF_TRACE_FD"))) return NULL; /* Make sure the clock is initialized */ diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 2b7ffec7..c00765b3 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -131,13 +131,13 @@ request_writer (void) if (conn == NULL) { - const char *fdstr = g_getenv ("SYSPROF_CONTROL_FD"); + const char *fdstr = getenv ("SYSPROF_CONTROL_FD"); int peer_fd = -1; if (fdstr != NULL) peer_fd = atoi (fdstr); - g_unsetenv ("SYSPROF_CONTROL_FD"); + unsetenv ("SYSPROF_CONTROL_FD"); if (peer_fd > 0) { @@ -251,7 +251,7 @@ sysprof_collector_get (void) self->tid = self->pid; #endif - if (g_getenv ("SYSPROF_CONTROL_FD") != NULL) + if (getenv ("SYSPROF_CONTROL_FD") != NULL) self->buffer = request_writer (); if (self->is_shared) From 43530e361339fd894d06a8367b0285d12ef2cf08 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:20:01 +0100 Subject: [PATCH 39/62] libsysprof-capture: Use vsnprintf() instead of g_strdup_printf() This imposes an arbitrary limit of 2048B on the length of printf-ed log messages, but is otherwise equivalent. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-collector.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index c00765b3..d36b7f47 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -65,7 +65,9 @@ #ifdef __linux__ # include #endif +#include #include +#include #include #include #include @@ -466,14 +468,14 @@ sysprof_collector_log_printf (int severity, ...) { COLLECTOR_BEGIN { - g_autofree char *formatted = NULL; + char formatted[2048]; SysprofCaptureLog *ev; va_list args; size_t len; size_t sl; va_start (args, format); - formatted = g_strdup_vprintf (format, args); + vsnprintf (formatted, sizeof (formatted), format, args); va_end (args); if (domain == NULL) From 621e7ea31476cfc191f6db2c2f276fb7255b789e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:23:28 +0100 Subject: [PATCH 40/62] libsysprof-capture: Use mkostemp() rather than g_file_open_tmp() Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-platform.c | 48 ++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/libsysprof-capture/sysprof-platform.c b/src/libsysprof-capture/sysprof-platform.c index 3449cfaa..1a344a52 100644 --- a/src/libsysprof-capture/sysprof-platform.c +++ b/src/libsysprof-capture/sysprof-platform.c @@ -58,12 +58,34 @@ #include #include +#include #include #include #include "sysprof-capture-util-private.h" #include "sysprof-platform.h" +#ifndef __NR_memfd_create +static const char * +get_tmpdir (void) +{ + const char *tmpdir = NULL; + + if (tmpdir == NULL || *tmpdir == '\0') + tmpdir = getenv ("TMPDIR"); + +#ifdef P_tmpdir + if (tmpdir == NULL || *tmpdir == '\0') + tmpdir = P_tmpdir; +#endif + + if (tmpdir == NULL || *tmpdir == '\0') + tmpdir = "/tmp"; + + return tmpdir; +} +#endif /* !__NR_memfd_create */ + /** * sysprof_memfd_create: * @name: (nullable): A descriptive name for the memfd or %NULL @@ -82,8 +104,10 @@ sysprof_memfd_create (const char *name) name = "[sysprof]"; return syscall (__NR_memfd_create, name, 0); #else - gchar *name_used = NULL; int fd; + const char *tmpdir; + char *template = NULL; + size_t template_len = 0; /* * TODO: It would be nice to ensure tmpfs @@ -93,14 +117,28 @@ sysprof_memfd_create (const char *name) * that the tmpfile we open is on tmpfs so that we get anonymous backed * pages after unlinking. */ - fd = g_file_open_tmp (NULL, &name_used, NULL); - if (name_used != NULL) + tmpdir = get_tmpdir (); + template_len = strlen (tmpdir) + 1 + strlen ("sysprof-XXXXXX") + 1; + template = sysprof_malloc0 (template_len); + if (template == NULL) { - g_unlink (name_used); - g_free (name_used); + errno = ENOMEM; + return -1; } + snprintf (template, template_len, "%s/sysprof-XXXXXX", tmpdir); + + fd = TEMP_FAILURE_RETRY (mkostemp (template, O_BINARY | O_CLOEXEC)); + if (fd < 0) + { + free (template); + return -1; + } + + unlink (template); + free (template); + return fd; #endif } From 8641789d87228570f56c50dee7784722c9046a35 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:49:44 +0100 Subject: [PATCH 41/62] libsysprof-capture: Use unlink() rather than g_unlink() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They’re equivalent. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-writer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 7926e6a4..01704880 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -988,7 +988,7 @@ handle_errno: if (fd != -1) { close (fd); - g_unlink (filename); + unlink (filename); } return false; From 8748db40910ed8908fccca7b82b5e24e40cfe174 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:46:17 +0100 Subject: [PATCH 42/62] libsysprof-capture: Use calloc() to replace GArray in writer-cat.c This is a straightforward replacement using a single allocation, as the number of array elements is always known ahead of time. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-writer-cat.c | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 459e0520..3411ba72 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -424,7 +424,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, goto panic; { - g_autoptr(GArray) counter = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounter)); + SysprofCaptureCounter *counters = calloc (frame->n_counters, sizeof (*counters)); + size_t n_counters = 0; + if (counters == NULL) + goto panic; for (unsigned int z = 0; z < frame->n_counters; z++) { @@ -436,15 +439,15 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, if (c.id != src) translate_table_add (tables, TRANSLATE_CTR, src, c.id); - g_array_append_val (counter, c); + counters[n_counters++] = c; } sysprof_capture_writer_define_counters (self, frame->frame.time, frame->frame.cpu, frame->frame.pid, - (gpointer)counter->data, - counter->len); + counters, + n_counters); translate_table_sort (tables, TRANSLATE_CTR); } @@ -460,8 +463,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, goto panic; { - g_autoptr(GArray) ids = g_array_new (FALSE, FALSE, sizeof (guint)); - g_autoptr(GArray) values = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounterValue)); + unsigned int *ids = NULL; + SysprofCaptureCounterValue *values = NULL; + size_t n_elements = 0; + size_t n_elements_allocated = 0; for (unsigned int z = 0; z < frame->n_values; z++) { @@ -474,21 +479,30 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, unsigned int dst = translate_table_translate (tables, TRANSLATE_CTR, v->ids[y]); SysprofCaptureCounterValue value = v->values[y]; - g_array_append_val (ids, dst); - g_array_append_val (values, value); + if (n_elements == n_elements_allocated) + { + n_elements_allocated = (n_elements_allocated > 0) ? n_elements_allocated * 2 : 4; + ids = reallocarray (ids, n_elements_allocated, sizeof (*ids)); + values = reallocarray (values, n_elements_allocated, sizeof (*values)); + if (ids == NULL || values == NULL) + goto panic; + } + + ids[n_elements] = dst; + values[n_elements] = value; + n_elements++; + assert (n_elements <= n_elements_allocated); } } } - assert (ids->len == values->len); - sysprof_capture_writer_set_counters (self, frame->frame.time, frame->frame.cpu, frame->frame.pid, - (const unsigned int *)(void *)ids->data, - (const SysprofCaptureCounterValue *)(void *)values->data, - ids->len); + ids, + values, + n_elements); } break; From d83adffd2db0b6683e6bf4cdf3d12ae8c5be954a Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:29:59 +0100 Subject: [PATCH 43/62] libsysprof-capture: Avoid using G_STMT_{START,END} from GLib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Open-code them instead, as they’re relatively simple. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-collector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index d36b7f47..0175073a 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -281,7 +281,7 @@ sysprof_collector_init (void) } #define COLLECTOR_BEGIN \ - G_STMT_START { \ + do { \ const SysprofCollector *collector = sysprof_collector_get (); \ if SYSPROF_LIKELY (collector->buffer) \ { \ @@ -296,7 +296,7 @@ sysprof_collector_init (void) if SYSPROF_UNLIKELY (collector->is_shared) \ G_UNLOCK (control_fd); \ } \ - } G_STMT_END + } while (0) void sysprof_collector_allocate (SysprofCaptureAddress alloc_addr, From b558e7b128ccaf9df3a4416f29724b4f72b707af Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 13:04:30 +0100 Subject: [PATCH 44/62] libsysprof-capture: Open-code MAX and CLAMP macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The macros don’t seem to make things sufficiently much clearer that it makes sense to provide them. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-reader.c | 4 ++-- src/libsysprof-capture/sysprof-collector.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 0cd80f93..12b3f878 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -165,7 +165,7 @@ sysprof_capture_reader_discover_end_time (SysprofCaptureReader *self) const SysprofCaptureMark *mark = NULL; if ((mark = sysprof_capture_reader_read_mark (self))) - end_time = frame.time + MAX (0, mark->duration); + end_time = frame.time + ((mark->duration > 0) ? mark->duration : 0); } break; @@ -1145,7 +1145,7 @@ sysprof_capture_reader_get_end_time (SysprofCaptureReader *self) end_time = self->header.end_time; } - return MAX (self->end_time, end_time); + return (self->end_time > end_time) ? self->end_time : end_time; } /** diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 0175073a..49489c15 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -327,7 +327,7 @@ sysprof_collector_allocate (SysprofCaptureAddress alloc_addr, else n_addrs = 0; - ev->n_addrs = CLAMP (n_addrs, 0, MAX_UNWIND_DEPTH); + ev->n_addrs = ((n_addrs < 0) ? 0 : (n_addrs > MAX_UNWIND_DEPTH) ? MAX_UNWIND_DEPTH : n_addrs); ev->frame.len = sizeof *ev + sizeof (SysprofCaptureAddress) * ev->n_addrs; ev->frame.type = SYSPROF_CAPTURE_FRAME_ALLOCATION; ev->frame.cpu = _do_getcpu (); @@ -364,7 +364,7 @@ sysprof_collector_sample (SysprofBacktraceFunc backtrace_func, else n_addrs = 0; - ev->n_addrs = CLAMP (n_addrs, 0, MAX_UNWIND_DEPTH); + ev->n_addrs = ((n_addrs < 0) ? 0 : (n_addrs > MAX_UNWIND_DEPTH) ? MAX_UNWIND_DEPTH : n_addrs); ev->frame.len = sizeof *ev + sizeof (SysprofCaptureAddress) * ev->n_addrs; ev->frame.type = SYSPROF_CAPTURE_FRAME_SAMPLE; ev->frame.cpu = _do_getcpu (); From 214ec21ce8a9ac184166f8f11c5e527c60ec3554 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 13:06:55 +0100 Subject: [PATCH 45/62] libsysprof-capture: Use an internal header in writer-cat.c This makes no functional difference, but does tidy up the code style a bit. All other internal headers are included using quotes in other files. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-writer-cat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 3411ba72..8ee52c7e 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -61,9 +61,9 @@ #include #include #include -#include #include +#include "sysprof-capture.h" #include "sysprof-macros-internal.h" typedef struct From 0d68b1afb57b8c868477da25d7b83bc76d39c268 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:30:43 +0100 Subject: [PATCH 46/62] libsysprof-capture: Use _WIN32 rather than G_OS_WIN32 They should be equivalent. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-util.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index f1563074..83ee8c69 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -61,7 +61,7 @@ #include #include -#ifdef G_OS_WIN32 +#ifdef _WIN32 # include # define WIN32_LEAN_AND_MEAN # include @@ -70,7 +70,7 @@ #include "sysprof-capture-util-private.h" #include "sysprof-macros.h" -#ifdef G_OS_WIN32 +#ifdef _WIN32 static G_LOCK_DEFINE (_sysprof_io_sync); #endif @@ -81,7 +81,7 @@ size_t if SYSPROF_UNLIKELY (pgsz == 0) { -#ifdef G_OS_WIN32 +#ifdef _WIN32 SYSTEM_INFO system_info; GetSystemInfo (&system_info); pgsz = system_info.dwPageSize; @@ -99,7 +99,7 @@ ssize_t size_t count, off_t offset) { -#ifdef G_OS_WIN32 +#ifdef _WIN32 ssize_t ret = -1; G_LOCK (_sysprof_io_sync); @@ -121,7 +121,7 @@ ssize_t size_t count, off_t offset) { -#ifdef G_OS_WIN32 +#ifdef _WIN32 ssize_t ret = -1; G_LOCK (_sysprof_io_sync); @@ -142,7 +142,7 @@ ssize_t const void *buf, size_t count) { -#ifdef G_OS_WIN32 +#ifdef _WIN32 ssize_t ret = -1; G_LOCK (_sysprof_io_sync); @@ -160,7 +160,7 @@ ssize_t int32_t (_sysprof_getpid) (void) { -#ifdef G_OS_WIN32 +#ifdef _WIN32 return _getpid (); #else return getpid (); From c89a47939e2f8848e89a8006b1c95720571504c7 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 13:05:22 +0100 Subject: [PATCH 47/62] libsysprof-capture: Move SysprofBacktraceFunc to a different header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So it’s next to the definition of `SysprofCaptureAddress`, which it uses. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-capture-types.h | 13 +++++++++++++ src/libsysprof-capture/sysprof-capture-writer.h | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index c0d2e6e2..f249be5e 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -370,4 +370,17 @@ sysprof_capture_address_compare (SysprofCaptureAddress a, return 0; } +/** + * SysprofBacktraceFunc: + * @addrs: (inout) (array length=n_addrs): an array to place addresses + * into the capture frame + * @n_addrs: the length of @addrs + * @user_data: (scope call): closure data for the callback + * + * Returns: the number of addresses filled in @addrs + */ +typedef int (*SysprofBacktraceFunc) (SysprofCaptureAddress *addrs, + unsigned int n_addrs, + void *user_data); + SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index c6c31507..b69cd740 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -67,19 +67,6 @@ SYSPROF_BEGIN_DECLS typedef struct _SysprofCaptureWriter SysprofCaptureWriter; -/** - * SysprofBacktraceFunc: - * @addrs: (inout) (array length=n_addrs): an array to place addresses - * into the capture frame - * @n_addrs: the length of @addrs - * @user_data: (scope call): closure data for the callback - * - * Returns: the number of addresses filled in @addrs - */ -typedef int (*SysprofBacktraceFunc) (SysprofCaptureAddress *addrs, - unsigned int n_addrs, - void *user_data); - SYSPROF_AVAILABLE_IN_ALL SysprofCaptureWriter *sysprof_capture_writer_new_from_env (size_t buffer_size); SYSPROF_AVAILABLE_IN_ALL From 5a2144e254fd8a24a60e6ea47c1be0cf954de2ba Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:28:08 +0100 Subject: [PATCH 48/62] libsysprof-capture: Port from GLib to pthreads for locking and once-init Another step towards dropping the GLib dependency. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/meson.build | 1 + src/libsysprof-capture/sysprof-capture-util.c | 14 ++-- src/libsysprof-capture/sysprof-collector.c | 76 +++++++++++++------ 3 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/libsysprof-capture/meson.build b/src/libsysprof-capture/meson.build index 77a12586..233e3314 100644 --- a/src/libsysprof-capture/meson.build +++ b/src/libsysprof-capture/meson.build @@ -40,6 +40,7 @@ libsysprof_capture_deps = [ glib_dep, gio_dep, gio_unix_dep, + dependency('threads'), ] libsysprof_capture = static_library( diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index 83ee8c69..b731d030 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -71,7 +71,7 @@ #include "sysprof-macros.h" #ifdef _WIN32 -static G_LOCK_DEFINE (_sysprof_io_sync); +static void *_sysprof_io_sync_lock = SRWLOCK_INIT; #endif size_t @@ -102,11 +102,11 @@ ssize_t #ifdef _WIN32 ssize_t ret = -1; - G_LOCK (_sysprof_io_sync); + AcquireSRWLockExclusive (_sysprof_io_sync_lock); errno = 0; if (lseek (fd, offset, SEEK_SET) != -1) ret = read (fd, buf, count); - G_UNLOCK (_sysprof_io_sync); + ReleaseSRWLockExclusive (_sysprof_io_sync_lock); return ret; #else @@ -124,11 +124,11 @@ ssize_t #ifdef _WIN32 ssize_t ret = -1; - G_LOCK (_sysprof_io_sync); + AcquireSRWLockExclusive (_sysprof_io_sync_lock); errno = 0; if (lseek (fd, offset, SEEK_SET) != -1) ret = write (fd, buf, count); - G_UNLOCK (_sysprof_io_sync); + ReleaseSRWLockExclusive (_sysprof_io_sync_lock); return ret; #else @@ -145,10 +145,10 @@ ssize_t #ifdef _WIN32 ssize_t ret = -1; - G_LOCK (_sysprof_io_sync); + AcquireSRWLockExclusive (_sysprof_io_sync_lock); errno = 0; ret = write (fd, buf, count); - G_UNLOCK (_sysprof_io_sync); + ReleaseSRWLockExclusive (_sysprof_io_sync_lock); return ret; #else diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 49489c15..2919d8b7 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -62,6 +62,7 @@ #include #include #include +#include #ifdef __linux__ # include #endif @@ -97,16 +98,17 @@ static MappedRingBuffer *request_writer (void); static void sysprof_collector_free (void *data); static const SysprofCollector *sysprof_collector_get (void); -static G_LOCK_DEFINE (control_fd); -static GPrivate collector_key = G_PRIVATE_INIT (sysprof_collector_free); -static GPrivate single_trace_key = G_PRIVATE_INIT (NULL); +static pthread_mutex_t control_fd_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_key_t collector_key; /* initialised in sysprof_collector_init() */ +static pthread_key_t single_trace_key; /* initialised in sysprof_collector_init() */ +static pthread_once_t collector_init = PTHREAD_ONCE_INIT; static SysprofCollector *shared_collector; static SysprofCollector invalid; static inline bool use_single_trace (void) { - return GPOINTER_TO_INT (g_private_get (&single_trace_key)); + return (bool) pthread_getspecific (single_trace_key); } static inline int @@ -223,7 +225,10 @@ sysprof_collector_free (void *data) static const SysprofCollector * sysprof_collector_get (void) { - const SysprofCollector *collector = g_private_get (&collector_key); + const SysprofCollector *collector; + + sysprof_collector_init (); + collector = pthread_getspecific (collector_key); /* We might have gotten here recursively */ if SYSPROF_UNLIKELY (collector == COLLECTOR_INVALID) @@ -236,16 +241,12 @@ sysprof_collector_get (void) return shared_collector; { - SysprofCollector *self; - - g_private_replace (&collector_key, COLLECTOR_INVALID); + SysprofCollector *self, *old_collector; self = sysprof_malloc0 (sizeof (SysprofCollector)); if (self == NULL) return COLLECTOR_INVALID; - G_LOCK (control_fd); - self->pid = getpid (); #ifdef __linux__ self->tid = syscall (__NR_gettid, 0); @@ -253,31 +254,56 @@ sysprof_collector_get (void) self->tid = self->pid; #endif + pthread_mutex_lock (&control_fd_lock); + if (getenv ("SYSPROF_CONTROL_FD") != NULL) self->buffer = request_writer (); - if (self->is_shared) - shared_collector = self; - else - g_private_replace (&collector_key, self); + /* Update the stored collector */ + old_collector = pthread_getspecific (collector_key); - G_UNLOCK (control_fd); + if (self->is_shared) + { + if (pthread_setspecific (collector_key, COLLECTOR_INVALID) != 0) + goto fail; + sysprof_collector_free (old_collector); + shared_collector = self; + } + else + { + if (pthread_setspecific (collector_key, self) != 0) + goto fail; + sysprof_collector_free (old_collector); + } + + pthread_mutex_unlock (&control_fd_lock); return self; + +fail: + pthread_mutex_unlock (&control_fd_lock); + sysprof_collector_free (self); + + return COLLECTOR_INVALID; } } +static void +collector_init_cb (void) +{ + if (SYSPROF_UNLIKELY (pthread_key_create (&collector_key, sysprof_collector_free) != 0)) + abort (); + if (SYSPROF_UNLIKELY (pthread_key_create (&single_trace_key, NULL) != 0)) + abort (); + + sysprof_clock_init (); +} + void sysprof_collector_init (void) { - static size_t once_init; - - if (g_once_init_enter (&once_init)) - { - sysprof_clock_init (); - (void)sysprof_collector_get (); - g_once_init_leave (&once_init, TRUE); - } + if (pthread_once (&collector_init, collector_init_cb) != 0) + abort (); } #define COLLECTOR_BEGIN \ @@ -286,7 +312,7 @@ sysprof_collector_init (void) if SYSPROF_LIKELY (collector->buffer) \ { \ if SYSPROF_UNLIKELY (collector->is_shared) \ - G_LOCK (control_fd); \ + pthread_mutex_lock (&control_fd_lock); \ \ { @@ -294,7 +320,7 @@ sysprof_collector_init (void) } \ \ if SYSPROF_UNLIKELY (collector->is_shared) \ - G_UNLOCK (control_fd); \ + pthread_mutex_unlock (&control_fd_lock); \ } \ } while (0) From 13b1e799018a5b3a46c5efd4930b5ae4f3d25f36 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:34:14 +0100 Subject: [PATCH 49/62] libsysprof-capture: Use POSIX socket functions rather than GSocket This should be equivalent. Signed-off-by: Philip Withnall Helps: #40 --- src/libsysprof-capture/sysprof-collector.c | 225 ++++++++++++++++++--- 1 file changed, 196 insertions(+), 29 deletions(-) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 2919d8b7..1d0c57b7 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -59,9 +59,8 @@ #endif #include -#include -#include -#include +#include +#include #include #ifdef __linux__ # include @@ -71,7 +70,9 @@ #include #include #include +#include #include +#include #include #include "mapped-ring-buffer.h" @@ -84,6 +85,14 @@ #define CREATRING "CreatRing\0" #define CREATRING_LEN 10 +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0 +#endif + +#ifndef MSG_CMSG_CLOEXEC +#define MSG_CMSG_CLOEXEC 0 +#endif + typedef struct { MappedRingBuffer *buffer; @@ -127,16 +136,186 @@ realign (size_t size) return (size + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1); } +static bool +set_fd_blocking (int fd) +{ +#ifdef F_GETFL + long fcntl_flags; + fcntl_flags = fcntl (peer_fd, F_GETFL); + + if (fcntl_flags == -1) + return false; + +#ifdef O_NONBLOCK + fcntl_flags &= ~O_NONBLOCK; +#else + fcntl_flags &= ~O_NDELAY; +#endif + + if (fcntl (peer_fd, F_SETFL, fcntl_flags) == -1) + return false; + + return true; +#else + return false; +#endif +} + +static bool +block_on_poll (int fd, + int condition) +{ + struct pollfd poll_fd; + + poll_fd.fd = fd; + poll_fd.events = condition; + + return (TEMP_FAILURE_RETRY (poll (&poll_fd, 1, -1)) == 1); +} + +static ssize_t +send_blocking (int fd, + const uint8_t *buffer, + size_t buffer_len) +{ + ssize_t res; + + while ((res = TEMP_FAILURE_RETRY (send (fd, buffer, buffer_len, MSG_NOSIGNAL))) < 0) + { + int errsv = errno; + + if (errsv == EWOULDBLOCK || + errsv == EAGAIN) + { + if (!block_on_poll (fd, POLLOUT)) + return -1; + } + else + { + return -1; + } + } + + return res; +} + +static bool +send_all_blocking (int fd, + const uint8_t *buffer, + size_t buffer_len, + size_t *bytes_written) +{ + size_t _bytes_written; + + _bytes_written = 0; + while (_bytes_written < buffer_len) + { + ssize_t res = send_blocking (fd, buffer + _bytes_written, buffer_len - _bytes_written); + if (res == -1) + { + if (bytes_written != NULL) + *bytes_written = _bytes_written; + return false; + } + assert (res > 0); + + _bytes_written += res; + } + + if (bytes_written != NULL) + *bytes_written = _bytes_written; + return true; +} + +static int +receive_fd_blocking (int peer_fd) +{ + ssize_t res; + struct msghdr msg; + struct iovec one_vector; + char one_byte; + uint8_t cmsg_buffer[CMSG_SPACE (sizeof (int))]; + struct cmsghdr *cmsg; + const int *fds = NULL; + size_t n_fds = 0; + + one_vector.iov_base = &one_byte; + one_vector.iov_len = 1; + + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_iov = &one_vector; + msg.msg_iovlen = 1; + msg.msg_flags = MSG_CMSG_CLOEXEC; + msg.msg_control = &cmsg_buffer; + msg.msg_controllen = sizeof (cmsg_buffer); + + while ((res = TEMP_FAILURE_RETRY (recvmsg (peer_fd, &msg, msg.msg_flags))) < 0) + { + int errsv = errno; + + if (errsv == EWOULDBLOCK || + errsv == EAGAIN) + { + if (!block_on_poll (peer_fd, POLLIN)) + return -1; + } + else + { + return -1; + } + } + + /* Decode the returned control message */ + cmsg = CMSG_FIRSTHDR (&msg); + if (cmsg == NULL) + return -1; + + if (cmsg->cmsg_level != SOL_SOCKET || + cmsg->cmsg_type != SCM_RIGHTS) + return -1; + + /* non-integer number of FDs */ + if ((cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg)) % 4 != 0) + return -1; + + fds = (const int *) CMSG_DATA (cmsg); + n_fds = (cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg)) / sizeof (*fds); + + /* only expecting one FD */ + if (n_fds != 1) + goto close_fds_err; + + for (size_t i = 0; i < n_fds; i++) + { + if (fds[i] < 0) + goto close_fds_err; + } + + /* only expecting one control message */ + cmsg = CMSG_NXTHDR (&msg, cmsg); + if (cmsg != NULL) + goto close_fds_err; + + return fds[0]; + +close_fds_err: + for (size_t i = 0; i < n_fds; i++) + close (fds[i]); + + return -1; +} + +/* Called with @control_fd_lock held. */ static MappedRingBuffer * request_writer (void) { - static GUnixConnection *conn; + static int peer_fd = -1; MappedRingBuffer *buffer = NULL; - if (conn == NULL) + if (peer_fd == -1) { const char *fdstr = getenv ("SYSPROF_CONTROL_FD"); - int peer_fd = -1; if (fdstr != NULL) peer_fd = atoi (fdstr); @@ -145,36 +324,24 @@ request_writer (void) if (peer_fd > 0) { - g_autoptr(GSocket) sock = NULL; + (void) set_fd_blocking (peer_fd); - g_unix_set_fd_nonblocking (peer_fd, FALSE, NULL); - - if ((sock = g_socket_new_from_fd (peer_fd, NULL))) - { - g_autoptr(GSocketConnection) scon = NULL; - - g_socket_set_blocking (sock, TRUE); - - if ((scon = g_socket_connection_factory_create_connection (sock)) && - G_IS_UNIX_CONNECTION (scon)) - conn = g_object_ref (G_UNIX_CONNECTION (scon)); - } +#ifdef SO_NOSIGPIPE + { + int opt_value = 1; + (void) setsockopt (peer_fd, SOL_SOCKET, SO_NOSIGPIPE, &opt_value, sizeof (opt_value)); + } +#endif } } - if (conn != NULL) + if (peer_fd >= 0) { - GOutputStream *out_stream; - gsize len; - - out_stream = g_io_stream_get_output_stream (G_IO_STREAM (conn)); - - if (g_output_stream_write_all (G_OUTPUT_STREAM (out_stream), CREATRING, CREATRING_LEN, &len, NULL, NULL) && - len == CREATRING_LEN) + if (send_all_blocking (peer_fd, (const uint8_t *) CREATRING, CREATRING_LEN, NULL)) { - int ring_fd = g_unix_connection_receive_fd (conn, NULL, NULL); + int ring_fd = receive_fd_blocking (peer_fd); - if (ring_fd > -1) + if (ring_fd >= 0) { buffer = mapped_ring_buffer_new_writer (ring_fd); close (ring_fd); From 75b69d0a896bfd75da33681a7061e03563518c1e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:38:32 +0100 Subject: [PATCH 50/62] libsysprof-capture: Rewrite list_files() to avoid GHashTable/GPtrArray This changes its API and ABI: it now returns an allocated array of const strings, rather than an allocated array of allocated strings. The call sites in the source tree have been adjusted accordingly. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-reader.c | 82 +++++++++++++++---- .../sysprof-capture-reader.h | 2 +- src/tests/test-capture.c | 2 +- src/tools/sysprof-dump.c | 4 +- 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 12b3f878..1166ae58 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -62,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -1273,20 +1274,60 @@ sysprof_capture_reader_read_file (SysprofCaptureReader *self) return file_chunk; } -char ** +static bool +array_append (const char ***files, + size_t *n_files, + size_t *n_files_allocated, + const char *new_element) +{ + if (*n_files == *n_files_allocated) + { + const char **new_files; + + *n_files_allocated = (*n_files_allocated > 0) ? 2 * *n_files_allocated : 4; + new_files = reallocarray (*files, *n_files_allocated, sizeof (**files)); + if (new_files == NULL) + return false; + *files = new_files; + } + + (*files)[*n_files] = new_element; + *n_files = *n_files + 1; + assert (*n_files <= *n_files_allocated); + + return true; +} + +static void +array_deduplicate (const char **files, + size_t *n_files) +{ + size_t last_written, next_to_read; + + if (*n_files == 0) + return; + + for (last_written = 0, next_to_read = 1; last_written <= next_to_read && next_to_read < *n_files;) + { + if (strcmp (files[next_to_read], files[last_written]) == 0) + next_to_read++; + else + files[++last_written] = files[next_to_read++]; + } + + assert (last_written + 1 <= *n_files); + *n_files = last_written + 1; +} + +const char ** sysprof_capture_reader_list_files (SysprofCaptureReader *self) { - g_autoptr(GHashTable) files = NULL; - g_autoptr(GPtrArray) ar = NULL; + const char **files = NULL; + size_t n_files = 0, n_files_allocated = 0; SysprofCaptureFrameType type; - GHashTableIter iter; - const gchar *key; assert (self != NULL); - ar = g_ptr_array_new_with_free_func (g_free); - files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); - while (sysprof_capture_reader_peek_type (self, &type)) { const SysprofCaptureFileChunk *file; @@ -1300,16 +1341,27 @@ sysprof_capture_reader_list_files (SysprofCaptureReader *self) if (!(file = sysprof_capture_reader_read_file (self))) break; - if (!g_hash_table_contains (files, file->path)) - g_hash_table_insert (files, g_strdup (file->path), NULL); + if (!array_append (&files, &n_files, &n_files_allocated, file->path)) + { + free (files); + errno = ENOMEM; + return NULL; + } } - g_hash_table_iter_init (&iter, files); - while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL)) - g_ptr_array_add (ar, g_strdup (key)); - g_ptr_array_add (ar, NULL); + /* Sort and deduplicate the files array. */ + qsort (files, n_files, sizeof (*files), (int (*)(const void *, const void *)) strcmp); + array_deduplicate (files, &n_files); - return (char **)g_ptr_array_free (sysprof_steal_pointer (&ar), FALSE); + /* Add a null terminator */ + if (!array_append (&files, &n_files, &n_files_allocated, NULL)) + { + free (files); + errno = ENOMEM; + return NULL; + } + + return sysprof_steal_pointer (&files); } bool diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index c417f9b6..0fffa6a4 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -144,7 +144,7 @@ SYSPROF_AVAILABLE_IN_ALL const SysprofCaptureFileChunk *sysprof_capture_reader_find_file (SysprofCaptureReader *self, const char *path); SYSPROF_AVAILABLE_IN_ALL -char **sysprof_capture_reader_list_files (SysprofCaptureReader *self); +const char **sysprof_capture_reader_list_files (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self, const char *path, diff --git a/src/tests/test-capture.c b/src/tests/test-capture.c index 895fdfcb..dda2ab19 100644 --- a/src/tests/test-capture.c +++ b/src/tests/test-capture.c @@ -747,7 +747,7 @@ test_reader_writer_file (void) { g_autofree gchar *data = NULL; GByteArray *buf = g_byte_array_new (); - g_auto(GStrv) files = NULL; + g_autofree const gchar **files = NULL; SysprofCaptureWriter *writer; SysprofCaptureReader *reader; SysprofCaptureFrameType type; diff --git a/src/tools/sysprof-dump.c b/src/tools/sysprof-dump.c index 24394909..17bd3a9f 100644 --- a/src/tools/sysprof-dump.c +++ b/src/tools/sysprof-dump.c @@ -64,9 +64,9 @@ main (gint argc, if (list_files) { - g_auto(GStrv) files = sysprof_capture_reader_list_files (reader); + g_autofree const gchar **files = sysprof_capture_reader_list_files (reader); - for (guint i = 0; files[i]; i++) + for (gsize i = 0; files[i]; i++) g_print ("%s\n", files[i]); return EXIT_SUCCESS; From 6a45f020f7e5f8d6a442028869a1e51a689c26fc Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:43:15 +0100 Subject: [PATCH 51/62] libsysprof-capture: Add SysprofCaptureJitmapIter to replace GHashTable Change `sysprof_capture_reader_read_jitmap()` to return a `const SysprofCaptureJitmap *` (like the other `read` functions), and add a new `SysprofCaptureJitmapIter` type to allow easy iteration over the jitmap. This allows a use of `GHashTable` to be removed from the API. It breaks the libsysprof-capture API and ABI. All the callers iterate over the jitmap rather than looking up elements by key. If that functionality is needed in future, additional API can be added to allow it on `SysprofCaptureJitmap`. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-reader.c | 65 +++++++++++++++---- .../sysprof-capture-reader.h | 18 ++++- .../sysprof-capture-writer-cat.c | 10 +-- .../sysprof-jitmap-symbol-resolver.c | 8 +-- src/tests/test-capture.c | 27 ++++---- src/tools/sysprof-dump.c | 10 +-- 6 files changed, 99 insertions(+), 39 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 1166ae58..cfa42a6c 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -758,10 +758,9 @@ sysprof_capture_reader_read_process (SysprofCaptureReader *self) return process; } -GHashTable * +const SysprofCaptureJitmap * sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) { - g_autoptr(GHashTable) ret = NULL; SysprofCaptureJitmap *jitmap; uint8_t *buf; uint8_t *endptr; @@ -787,17 +786,15 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) if (!sysprof_capture_reader_ensure_space_for (self, jitmap->frame.len)) return NULL; - jitmap = (SysprofCaptureJitmap *)(gpointer)&self->buf[self->pos]; - - ret = g_hash_table_new_full (NULL, NULL, NULL, g_free); + jitmap = (SysprofCaptureJitmap *)(void *)&self->buf[self->pos]; buf = jitmap->data; endptr = &self->buf[self->pos + jitmap->frame.len]; + /* Check the strings are all nul-terminated. */ for (i = 0; i < jitmap->n_jitmaps; i++) { SysprofCaptureAddress addr; - const char *str; if (buf + sizeof addr >= endptr) return NULL; @@ -805,23 +802,19 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self) memcpy (&addr, buf, sizeof addr); buf += sizeof addr; - str = (char *)buf; - buf = memchr (buf, '\0', (endptr - buf)); if (buf == NULL) return NULL; buf++; - - g_hash_table_insert (ret, GSIZE_TO_POINTER (addr), g_strdup (str)); } sysprof_capture_reader_bswap_jitmap (self, jitmap); self->pos += jitmap->frame.len; - return sysprof_steal_pointer (&ret); + return jitmap; } const SysprofCaptureSample * @@ -1512,3 +1505,53 @@ sysprof_capture_reader_read_allocation (SysprofCaptureReader *self) return ma; } + +typedef struct { + const SysprofCaptureJitmap *jitmap; + const uint8_t *buf; + unsigned int i; + + void *padding1; + void *padding2; +} RealSysprofCaptureJitmapIter; + +void +sysprof_capture_jitmap_iter_init (SysprofCaptureJitmapIter *iter, + const SysprofCaptureJitmap *jitmap) +{ + RealSysprofCaptureJitmapIter *real_iter = (RealSysprofCaptureJitmapIter *) iter; + + assert (iter != NULL); + assert (jitmap != NULL); + + real_iter->jitmap = jitmap; + real_iter->buf = jitmap->data; + real_iter->i = 0; +} + +bool +sysprof_capture_jitmap_iter_next (SysprofCaptureJitmapIter *iter, + SysprofCaptureAddress *addr, + const char **name) +{ + RealSysprofCaptureJitmapIter *real_iter = (RealSysprofCaptureJitmapIter *) iter; + const char *_name; + + assert (iter != NULL); + + if (real_iter->i >= real_iter->jitmap->n_jitmaps) + return false; + + if (addr != NULL) + memcpy (addr, real_iter->buf, sizeof (*addr)); + real_iter->buf += sizeof (*addr); + + _name = (const char *) real_iter->buf; + if (name != NULL) + *name = _name; + real_iter->buf += strlen (_name) + 1; + + real_iter->i++; + + return true; +} diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index 0fffa6a4..18561ada 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -115,7 +115,7 @@ const SysprofCaptureProcess *sysprof_capture_reader_read_process ( SYSPROF_AVAILABLE_IN_ALL const SysprofCaptureSample *sysprof_capture_reader_read_sample (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL -GHashTable *sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self); +const SysprofCaptureJitmap *sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL const SysprofCaptureCounterDefine *sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL @@ -150,5 +150,21 @@ bool sysprof_capture_reader_read_file_fd ( const char *path, int fd); +typedef struct { + /*< private >*/ + void *p1; + void *p2; + unsigned int u1; + void *p3; + void *p4; +} SysprofCaptureJitmapIter; + +SYSPROF_AVAILABLE_IN_3_38 +void sysprof_capture_jitmap_iter_init (SysprofCaptureJitmapIter *iter, + const SysprofCaptureJitmap *jitmap); +SYSPROF_AVAILABLE_IN_3_38 +bool sysprof_capture_jitmap_iter_next (SysprofCaptureJitmapIter *iter, + SysprofCaptureAddress *addr, + const char **path); SYSPROF_END_DECLS diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 8ee52c7e..d33b4bb8 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -197,10 +197,10 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, */ while (sysprof_capture_reader_peek_type (reader, &type)) { - g_autoptr(GHashTable) jitmap = NULL; - GHashTableIter iter; + const SysprofCaptureJitmap *jitmap; + SysprofCaptureJitmapIter iter; + SysprofCaptureAddress addr; const char *name; - uint64_t addr; if (type != SYSPROF_CAPTURE_FRAME_JITMAP) { @@ -212,8 +212,8 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, if (!(jitmap = sysprof_capture_reader_read_jitmap (reader))) goto panic; - g_hash_table_iter_init (&iter, jitmap); - while (g_hash_table_iter_next (&iter, (gpointer *)&addr, (gpointer *)&name)) + sysprof_capture_jitmap_iter_init (&iter, jitmap); + while (sysprof_capture_jitmap_iter_next (&iter, &addr, &name)) { uint64_t replace = sysprof_capture_writer_add_jitmap (self, name); /* We need to keep a table of replacement addresses so that diff --git a/src/libsysprof/sysprof-jitmap-symbol-resolver.c b/src/libsysprof/sysprof-jitmap-symbol-resolver.c index ef63e8fe..2015eb50 100644 --- a/src/libsysprof/sysprof-jitmap-symbol-resolver.c +++ b/src/libsysprof/sysprof-jitmap-symbol-resolver.c @@ -74,8 +74,8 @@ sysprof_jitmap_symbol_resolver_load (SysprofSymbolResolver *resolver, while (sysprof_capture_reader_peek_type (reader, &type)) { - g_autoptr(GHashTable) jitmap = NULL; - GHashTableIter iter; + const SysprofCaptureJitmap *jitmap; + SysprofCaptureJitmapIter iter; SysprofCaptureAddress addr; const gchar *str; @@ -89,8 +89,8 @@ sysprof_jitmap_symbol_resolver_load (SysprofSymbolResolver *resolver, if (!(jitmap = sysprof_capture_reader_read_jitmap (reader))) return; - g_hash_table_iter_init (&iter, jitmap); - while (g_hash_table_iter_next (&iter, (gpointer *)&addr, (gpointer *)&str)) + sysprof_capture_jitmap_iter_init (&iter, jitmap); + while (sysprof_capture_jitmap_iter_next (&iter, &addr, &str)) g_hash_table_insert (self->jitmap, GSIZE_TO_POINTER (addr), g_strdup (str)); } } diff --git a/src/tests/test-capture.c b/src/tests/test-capture.c index dda2ab19..cb88f018 100644 --- a/src/tests/test-capture.c +++ b/src/tests/test-capture.c @@ -32,15 +32,16 @@ #include "sysprof-capture-util-private.h" static void -copy_into (GHashTable *src, - GHashTable *dst) +copy_into (const SysprofCaptureJitmap *src, + GHashTable *dst) { - GHashTableIter iter; - gpointer k, v; + SysprofCaptureJitmapIter iter; + SysprofCaptureAddress addr; + const char *name; - g_hash_table_iter_init (&iter, src); - while (g_hash_table_iter_next (&iter, &k, &v)) - g_hash_table_insert (dst, k, g_strdup ((gchar *)v)); + sysprof_capture_jitmap_iter_init (&iter, src); + while (sysprof_capture_jitmap_iter_next (&iter, &addr, &name)) + g_hash_table_insert (dst, GSIZE_TO_POINTER (addr), g_strdup (name)); } static void @@ -318,19 +319,19 @@ test_reader_basic (void) for (;;) { SysprofCaptureFrameType type = -1; - g_autoptr(GHashTable) ret = NULL; + const SysprofCaptureJitmap *jitmap; if (sysprof_capture_reader_peek_type (reader, &type)) g_assert_cmpint (type, ==, SYSPROF_CAPTURE_FRAME_JITMAP); else break; - ret = sysprof_capture_reader_read_jitmap (reader); - g_assert (ret != NULL); + jitmap = sysprof_capture_reader_read_jitmap (reader); + g_assert_nonnull (jitmap); - i += g_hash_table_size (ret); + i += jitmap->n_jitmaps; - copy_into (ret, jmap); + copy_into (jitmap, jmap); } g_assert_cmpint (1000, ==, i); @@ -887,7 +888,7 @@ test_reader_writer_cat_jitmap (void) reader = sysprof_capture_writer_create_reader (res, &error); g_assert_no_error (error); g_assert_nonnull (reader); - g_hash_table_unref (sysprof_capture_reader_read_jitmap (reader)); + sysprof_capture_reader_read_jitmap (reader); sample = sysprof_capture_reader_read_sample (reader); g_assert_cmpint (sample->frame.pid, ==, getpid ()); g_assert_cmpint (sample->n_addrs, ==, G_N_ELEMENTS (addrs)); diff --git a/src/tools/sysprof-dump.c b/src/tools/sysprof-dump.c index 17bd3a9f..d90963dc 100644 --- a/src/tools/sysprof-dump.c +++ b/src/tools/sysprof-dump.c @@ -115,18 +115,18 @@ main (gint argc, case SYSPROF_CAPTURE_FRAME_JITMAP: { - g_autoptr(GHashTable) ret = sysprof_capture_reader_read_jitmap (reader); - GHashTableIter iter; + const SysprofCaptureJitmap *jitmap = sysprof_capture_reader_read_jitmap (reader); + SysprofCaptureJitmapIter iter; SysprofCaptureAddress addr; const gchar *str; - if (ret == NULL) + if (jitmap == NULL) return EXIT_FAILURE; g_print ("JITMAP:\n"); - g_hash_table_iter_init (&iter, ret); - while (g_hash_table_iter_next (&iter, (gpointer *)&addr, (gpointer *)&str)) + sysprof_capture_jitmap_iter_init (&iter, jitmap); + while (sysprof_capture_jitmap_iter_next (&iter, &addr, &str)) g_print (" " SYSPROF_CAPTURE_ADDRESS_FORMAT " : %s\n", addr, str); break; From cef698e6585c0ea2d86dbee415836525b631185f Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 12:16:47 -0700 Subject: [PATCH 52/62] libsysprof-capture: wrap strdup to be NULL-safe --- src/libsysprof-capture/sysprof-capture-condition.c | 2 +- src/libsysprof-capture/sysprof-capture-reader.c | 6 +++--- src/libsysprof-capture/sysprof-macros-internal.h | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-condition.c b/src/libsysprof-capture/sysprof-capture-condition.c index 5322e091..8f891577 100644 --- a/src/libsysprof-capture/sysprof-capture-condition.c +++ b/src/libsysprof-capture/sysprof-capture-condition.c @@ -519,7 +519,7 @@ sysprof_capture_condition_new_where_file (const char *path) return NULL; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE; - self->u.where_file = strdup (path); + self->u.where_file = sysprof_strdup (path); if (self->u.where_file == NULL) { free (self); diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index cfa42a6c..2b103b57 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -284,7 +284,7 @@ sysprof_capture_reader_new (const char *filename, return NULL; } - self->filename = strdup (filename); + self->filename = sysprof_strdup (filename); return self; } @@ -1080,7 +1080,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, } if (self->filename == NULL) - self->filename = strdup (filename); + self->filename = sysprof_strdup (filename); close (fd); @@ -1173,7 +1173,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self) *copy = *self; copy->ref_count = 1; - copy->filename = strdup (self->filename); + copy->filename = sysprof_strdup (self->filename); copy->fd = fd; copy->end_time = self->end_time; copy->st_buf = self->st_buf; diff --git a/src/libsysprof-capture/sysprof-macros-internal.h b/src/libsysprof-capture/sysprof-macros-internal.h index 9fed4652..cf814dad 100644 --- a/src/libsysprof-capture/sysprof-macros-internal.h +++ b/src/libsysprof-capture/sysprof-macros-internal.h @@ -73,3 +73,5 @@ if (_p != NULL) \ (destroy) (_p); \ } while (0) + +#define sysprof_strdup(s) ((s) ? strdup(s) : NULL) From fde278fb7f4cf6c23af01331364261a591a3de20 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:48:51 +0100 Subject: [PATCH 53/62] libsysprof-capture: Drop sysprof_capture_writer_set_flush_delay() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was unused anywhere within sysprof.git, and couldn’t be modified to drop its GLib dependency while still retaining its functionality. If it’s still needed, it’ll have to be reimplemented in libsysprof. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-writer.c | 41 ------------------- .../sysprof-capture-writer.h | 4 -- 2 files changed, 45 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 01704880..65df4ac5 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -137,9 +137,6 @@ struct _SysprofCaptureWriter size_t pos; size_t len; - /* GSource for periodic flush */ - GSource *periodic_flush; - /* counter id sequence */ int next_counter_id; @@ -171,8 +168,6 @@ sysprof_capture_writer_finalize (SysprofCaptureWriter *self) { if (self != NULL) { - sysprof_clear_pointer (&self->periodic_flush, g_source_destroy); - sysprof_capture_writer_flush (self); if (self->fd != -1) @@ -1502,42 +1497,6 @@ sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self, return true; } -static gboolean -sysprof_capture_writer_auto_flush_cb (SysprofCaptureWriter *self) -{ - g_assert (self != NULL); - - sysprof_capture_writer_flush (self); - - return G_SOURCE_CONTINUE; -} - -void -sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self, - GMainContext *main_context, - guint timeout_seconds) -{ - GSource *source; - - g_return_if_fail (self != NULL); - - sysprof_clear_pointer (&self->periodic_flush, g_source_destroy); - - if (timeout_seconds == 0) - return; - - source = g_timeout_source_new_seconds (timeout_seconds); - g_source_set_name (source, "[sysprof-capture-writer-flush]"); - g_source_set_priority (source, G_PRIORITY_LOW + 100); - g_source_set_callback (source, - (GSourceFunc) sysprof_capture_writer_auto_flush_cb, - self, NULL); - - self->periodic_flush = g_steal_pointer (&source); - - g_source_attach (self->periodic_flush, main_context); -} - bool sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self, int64_t time, diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index b69cd740..bf39f0f6 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -85,10 +85,6 @@ SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_writer_stat (SysprofCaptureWriter *self, SysprofCaptureStat *stat); SYSPROF_AVAILABLE_IN_ALL -void sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self, - GMainContext *main_context, - unsigned int timeout_seconds); -SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_writer_add_file (SysprofCaptureWriter *self, int64_t time, int cpu, From e19d70bca018319ce3d9b3a04e306d810f7f3d07 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:56:12 +0100 Subject: [PATCH 54/62] libsysprof-capture: Drop GError usage from SysprofCaptureReader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use `errno` instead, which is icky, but given that all of the failure modes are from POSIX I/O functions, it’s at least in keeping with them. This is a major API break. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-reader.c | 76 +++++++++---------- .../sysprof-capture-reader.h | 12 +-- src/libsysprof/sysprof-proxy-source.c | 8 +- src/libsysprof/sysprof-tracefd-source.c | 2 +- src/tests/allocs-by-size.c | 5 +- src/tests/allocs-within-mark.c | 5 +- src/tests/cross-thread-frees.c | 6 +- src/tests/find-temp-allocs.c | 6 +- src/tests/memory-stack-stash.c | 5 +- src/tests/show-page-usage.c | 6 +- src/tests/test-addr-decode.c | 6 +- src/tests/test-addr-map.c | 5 +- src/tests/test-capture-cursor.c | 8 +- src/tests/test-capture.c | 68 +++++++---------- src/tests/test-resolvers.c | 8 +- src/tools/list-threads.c | 4 +- src/tools/sysprof-cat.c | 5 +- src/tools/sysprof-cli.c | 5 +- src/tools/sysprof-dump.c | 5 +- 19 files changed, 114 insertions(+), 131 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 2b103b57..67c6b28b 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -90,29 +90,24 @@ struct _SysprofCaptureReader unsigned int st_buf_set : 1; }; +/* Sets @errno on failure. Sets @errno to EBADMSG if the file magic doesn’t + * match, and otherwise can return any error as for read(). */ static bool -sysprof_capture_reader_read_file_header (SysprofCaptureReader *self, - SysprofCaptureFileHeader *header, - GError **error) +sysprof_capture_reader_read_file_header (SysprofCaptureReader *self, + SysprofCaptureFileHeader *header) { assert (self != NULL); assert (header != NULL); if (sizeof *header != _sysprof_pread (self->fd, header, sizeof *header, 0L)) { - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); + /* errno is propagated */ return false; } if (header->magic != SYSPROF_CAPTURE_MAGIC) { - g_set_error (error, - G_FILE_ERROR, - G_FILE_ERROR_FAILED, - "Capture file magic does not match"); + errno = EBADMSG; return false; } @@ -201,17 +196,17 @@ sysprof_capture_reader_discover_end_time (SysprofCaptureReader *self) /** * sysprof_capture_reader_new_from_fd: * @fd: an fd to take ownership from - * @error: a location for a #GError or %NULL * * Creates a new reader using the file-descriptor. * * This is useful if you don't necessarily have access to the filename itself. * + * If this function fails, `errno` is set. + * * Returns: (transfer full): an #SysprofCaptureReader or %NULL upon failure. */ SysprofCaptureReader * -sysprof_capture_reader_new_from_fd (int fd, - GError **error) +sysprof_capture_reader_new_from_fd (int fd) { SysprofCaptureReader *self; @@ -220,7 +215,7 @@ sysprof_capture_reader_new_from_fd (int fd, self = sysprof_malloc0 (sizeof (SysprofCaptureReader)); if (self == NULL) { - g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM, "No memory"); + errno = ENOMEM; return NULL; } @@ -230,7 +225,7 @@ sysprof_capture_reader_new_from_fd (int fd, if (self->buf == NULL) { free (self); - g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM, "No memory"); + errno = ENOMEM; return NULL; } @@ -239,9 +234,11 @@ sysprof_capture_reader_new_from_fd (int fd, self->fd = fd; self->fd_off = sizeof (SysprofCaptureFileHeader); - if (!sysprof_capture_reader_read_file_header (self, &self->header, error)) + if (!sysprof_capture_reader_read_file_header (self, &self->header)) { + int errsv = errno; sysprof_capture_reader_finalize (self); + errno = errsv; return NULL; } @@ -261,8 +258,7 @@ sysprof_capture_reader_new_from_fd (int fd, } SysprofCaptureReader * -sysprof_capture_reader_new (const char *filename, - GError **error) +sysprof_capture_reader_new (const char *filename) { SysprofCaptureReader *self; int fd; @@ -271,16 +267,15 @@ sysprof_capture_reader_new (const char *filename, if (-1 == (fd = open (filename, O_RDONLY, 0))) { - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); + /* errno is propagated */ return NULL; } - if (NULL == (self = sysprof_capture_reader_new_from_fd (fd, error))) + if (NULL == (self = sysprof_capture_reader_new_from_fd (fd))) { + int errsv = errno; close (fd); + errno = errsv; return NULL; } @@ -996,9 +991,8 @@ sysprof_capture_reader_unref (SysprofCaptureReader *self) } bool -sysprof_capture_reader_splice (SysprofCaptureReader *self, - SysprofCaptureWriter *dest, - GError **error) +sysprof_capture_reader_splice (SysprofCaptureReader *self, + SysprofCaptureWriter *dest) { assert (self != NULL); assert (self->fd != -1); @@ -1007,10 +1001,7 @@ sysprof_capture_reader_splice (SysprofCaptureReader *self, /* Flush before writing anything to ensure consistency */ if (!sysprof_capture_writer_flush (dest)) { - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); + /* errno is propagated */ return false; } @@ -1019,30 +1010,32 @@ sysprof_capture_reader_splice (SysprofCaptureReader *self, * track the current position to avoid reseting it. */ - /* Perform the splice */ - return _sysprof_capture_writer_splice_from_fd (dest, self->fd, error); + /* Perform the splice; errno is propagated on failure */ + return _sysprof_capture_writer_splice_from_fd (dest, self->fd); } /** * sysprof_capture_reader_save_as: * @self: An #SysprofCaptureReader * @filename: the file to save the capture as - * @error: a location for a #GError or %NULL. * * This is a convenience function for copying a capture file for which * you may have already discarded the writer for. * - * Returns: %TRUE on success; otherwise %FALSE and @error is set. + * `errno` is set on failure. It may be any of the errors returned by + * `open()`, `fstat()`, `ftruncate()`, `lseek()` or `sendfile()`. + * + * Returns: %TRUE on success; otherwise %FALSE. */ bool -sysprof_capture_reader_save_as (SysprofCaptureReader *self, - const char *filename, - GError **error) +sysprof_capture_reader_save_as (SysprofCaptureReader *self, + const char *filename) { struct stat stbuf; off_t in_off; size_t to_write; int fd = -1; + int errsv; assert (self != NULL); assert (filename != NULL); @@ -1087,13 +1080,12 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self, return true; handle_errno: + errsv = errno; + if (fd != -1) close (fd); - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); + errno = errsv; return false; } diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index 18561ada..c802e0a5 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -67,11 +67,9 @@ SYSPROF_BEGIN_DECLS typedef struct _SysprofCaptureReader SysprofCaptureReader; SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureReader *sysprof_capture_reader_new (const char *filename, - GError **error); +SysprofCaptureReader *sysprof_capture_reader_new (const char *filename); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureReader *sysprof_capture_reader_new_from_fd (int fd, - GError **error); +SysprofCaptureReader *sysprof_capture_reader_new_from_fd (int fd); SYSPROF_AVAILABLE_IN_ALL SysprofCaptureReader *sysprof_capture_reader_copy (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL @@ -128,12 +126,10 @@ SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_reader_reset (SysprofCaptureReader *self); SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_reader_splice (SysprofCaptureReader *self, - SysprofCaptureWriter *dest, - GError **error); + SysprofCaptureWriter *dest); SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_reader_save_as (SysprofCaptureReader *self, - const char *filename, - GError **error); + const char *filename); SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_reader_get_stat (SysprofCaptureReader *self, SysprofCaptureStat *st_buf); diff --git a/src/libsysprof/sysprof-proxy-source.c b/src/libsysprof/sysprof-proxy-source.c index 967ae232..d290c8bf 100644 --- a/src/libsysprof/sysprof-proxy-source.c +++ b/src/libsysprof/sysprof-proxy-source.c @@ -464,14 +464,16 @@ sysprof_proxy_source_complete_monitor (SysprofProxySource *self, Monitor *monitor) { g_autoptr(SysprofCaptureReader) reader = NULL; - g_autoptr(GError) error = NULL; g_assert (SYSPROF_IS_PROXY_SOURCE (self)); g_assert (monitor != NULL); g_assert (monitor->self == self); - if (!(reader = sysprof_capture_reader_new_from_fd (steal_fd (&monitor->fd), &error))) - g_warning ("Failed to load reader from peer FD: %s", error->message); + if (!(reader = sysprof_capture_reader_new_from_fd (steal_fd (&monitor->fd)))) + { + int errsv = errno; + g_warning ("Failed to load reader from peer FD: %s", g_strerror (errsv)); + } else sysprof_proxy_source_cat (self, reader); } diff --git a/src/libsysprof/sysprof-tracefd-source.c b/src/libsysprof/sysprof-tracefd-source.c index a59cd1d0..061f9132 100644 --- a/src/libsysprof/sysprof-tracefd-source.c +++ b/src/libsysprof/sysprof-tracefd-source.c @@ -259,7 +259,7 @@ sysprof_tracefd_source_stop (SysprofSource *source) { g_autoptr(SysprofCaptureReader) reader = NULL; - if ((reader = sysprof_capture_reader_new_from_fd (priv->tracefd, 0))) + if ((reader = sysprof_capture_reader_new_from_fd (priv->tracefd))) sysprof_capture_writer_cat (priv->writer, reader, NULL); priv->tracefd = -1; diff --git a/src/tests/allocs-by-size.c b/src/tests/allocs-by-size.c index 6260d4b4..4f80414b 100644 --- a/src/tests/allocs-by-size.c +++ b/src/tests/allocs-by-size.c @@ -134,9 +134,10 @@ main (gint argc, bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); - if (!(reader = sysprof_capture_reader_new (filename, &error))) + if (!(reader = sysprof_capture_reader_new (filename))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tests/allocs-within-mark.c b/src/tests/allocs-within-mark.c index 10d12287..9e2bfaab 100644 --- a/src/tests/allocs-within-mark.c +++ b/src/tests/allocs-within-mark.c @@ -205,9 +205,10 @@ main (gint argc, bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); - if (!(reader = sysprof_capture_reader_new (filename, &error))) + if (!(reader = sysprof_capture_reader_new (filename))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tests/cross-thread-frees.c b/src/tests/cross-thread-frees.c index f4ec8921..6279e214 100644 --- a/src/tests/cross-thread-frees.c +++ b/src/tests/cross-thread-frees.c @@ -116,7 +116,6 @@ main (gint argc, { SysprofCaptureReader *reader; const gchar *filename = argv[1]; - g_autoptr(GError) error = NULL; if (argc < 2) { @@ -124,9 +123,10 @@ main (gint argc, return EXIT_FAILURE; } - if (!(reader = sysprof_capture_reader_new (filename, &error))) + if (!(reader = sysprof_capture_reader_new (filename))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tests/find-temp-allocs.c b/src/tests/find-temp-allocs.c index 3df3c2c9..f4d35331 100644 --- a/src/tests/find-temp-allocs.c +++ b/src/tests/find-temp-allocs.c @@ -141,7 +141,6 @@ main (gint argc, { SysprofCaptureReader *reader; const gchar *filename = argv[1]; - g_autoptr(GError) error = NULL; if (argc < 2) { @@ -149,9 +148,10 @@ main (gint argc, return EXIT_FAILURE; } - if (!(reader = sysprof_capture_reader_new (filename, &error))) + if (!(reader = sysprof_capture_reader_new (filename))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tests/memory-stack-stash.c b/src/tests/memory-stack-stash.c index 7b6675c6..7562c677 100644 --- a/src/tests/memory-stack-stash.c +++ b/src/tests/memory-stack-stash.c @@ -77,9 +77,10 @@ main (gint argc, bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); - if (!(reader = sysprof_capture_reader_new (filename, &error))) + if (!(reader = sysprof_capture_reader_new (filename))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tests/show-page-usage.c b/src/tests/show-page-usage.c index 0a6a1c21..9eb22ec5 100644 --- a/src/tests/show-page-usage.c +++ b/src/tests/show-page-usage.c @@ -168,7 +168,6 @@ main (gint argc, SysprofCaptureReader *reader; const gchar *filename = argv[1]; g_autoptr(SysprofProfile) memprof = NULL; - g_autoptr(GError) error = NULL; if (argc < 2) { @@ -178,9 +177,10 @@ main (gint argc, main_loop = g_main_loop_new (NULL, FALSE); - if (!(reader = sysprof_capture_reader_new (filename, &error))) + if (!(reader = sysprof_capture_reader_new (filename))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tests/test-addr-decode.c b/src/tests/test-addr-decode.c index 8757bed9..dae7769e 100644 --- a/src/tests/test-addr-decode.c +++ b/src/tests/test-addr-decode.c @@ -11,7 +11,6 @@ main (gint argc, { g_autoptr(SysprofCaptureReader) reader = NULL; g_autoptr(SysprofSymbolResolver) resolver = NULL; - g_autoptr(GError) error = NULL; SysprofCaptureFrameType type; if (argc != 2) @@ -20,9 +19,10 @@ main (gint argc, return 1; } - if (!(reader = sysprof_capture_reader_new (argv[1], &error))) + if (!(reader = sysprof_capture_reader_new (argv[1]))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return 1; } diff --git a/src/tests/test-addr-map.c b/src/tests/test-addr-map.c index 022e8149..918ba7ae 100644 --- a/src/tests/test-addr-map.c +++ b/src/tests/test-addr-map.c @@ -92,9 +92,10 @@ main (gint argc, return 1; } - if (!(reader = sysprof_capture_reader_new (argv[1], &error))) + if (!(reader = sysprof_capture_reader_new (argv[1]))) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return 1; } diff --git a/src/tests/test-capture-cursor.c b/src/tests/test-capture-cursor.c index dd884160..9dc2e6c6 100644 --- a/src/tests/test-capture-cursor.c +++ b/src/tests/test-capture-cursor.c @@ -37,20 +37,18 @@ test_cursor_basic (void) SysprofCaptureReader *reader; SysprofCaptureWriter *writer; SysprofCaptureCursor *cursor; - GError *error = NULL; gint64 t = SYSPROF_CAPTURE_CURRENT_TIME; guint i; gint r; gint count = 0; writer = sysprof_capture_writer_new ("capture-cursor-file", 0); - g_assert (writer != NULL); + g_assert_nonnull (writer); sysprof_capture_writer_flush (writer); - reader = sysprof_capture_reader_new ("capture-cursor-file", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("capture-cursor-file"); + g_assert_nonnull (reader); for (i = 0; i < 100; i++) { diff --git a/src/tests/test-capture.c b/src/tests/test-capture.c index cb88f018..8f2d4f3c 100644 --- a/src/tests/test-capture.c +++ b/src/tests/test-capture.c @@ -61,9 +61,8 @@ test_reader_basic (void) sysprof_capture_writer_flush (writer); - reader = sysprof_capture_reader_new ("capture-file", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("capture-file"); + g_assert_nonnull (reader); for (i = 0; i < 100; i++) { @@ -430,9 +429,8 @@ test_reader_basic (void) g_clear_pointer (&writer, sysprof_capture_writer_unref); g_clear_pointer (&reader, sysprof_capture_reader_unref); - reader = sysprof_capture_reader_new ("capture-file.bak", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("capture-file.bak"); + g_assert_nonnull (reader); for (i = 0; i < 2; i++) { @@ -481,9 +479,8 @@ test_writer_splice (void) g_clear_pointer (&writer1, sysprof_capture_writer_unref); g_clear_pointer (&writer2, sysprof_capture_writer_unref); - reader = sysprof_capture_reader_new ("writer2.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("writer2.syscap"); + g_assert_nonnull (reader); for (i = 0; i < 1000; i++) { @@ -511,7 +508,6 @@ test_reader_splice (void) SysprofCaptureWriter *writer2; SysprofCaptureReader *reader; SysprofCaptureFrameType type; - GError *error = NULL; guint i; guint count; gint r; @@ -527,9 +523,8 @@ test_reader_splice (void) g_clear_pointer (&writer1, sysprof_capture_writer_unref); - reader = sysprof_capture_reader_new ("writer1.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("writer1.syscap"); + g_assert_nonnull (reader); /* advance to the end of the reader to non-start boundary for fd */ @@ -546,14 +541,14 @@ test_reader_splice (void) r = sysprof_capture_reader_peek_type (reader, &type); g_assert_cmpint (r, ==, FALSE); - r = sysprof_capture_reader_splice (reader, writer2, &error); - g_assert_no_error (error); - g_assert_cmpint (r, ==, TRUE); + r = sysprof_capture_reader_splice (reader, writer2); + g_assert_true (r); g_clear_pointer (&reader, sysprof_capture_reader_unref); g_clear_pointer (&writer2, sysprof_capture_writer_unref); - reader = sysprof_capture_reader_new ("writer2.syscap", 0); + reader = sysprof_capture_reader_new ("writer2.syscap"); + g_assert_nonnull (reader); for (i = 0; i < 1000; i++) { @@ -570,19 +565,16 @@ test_reader_splice (void) g_clear_pointer (&reader, sysprof_capture_reader_unref); - reader = sysprof_capture_reader_new ("writer2.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("writer2.syscap"); + g_assert_nonnull (reader); - r = sysprof_capture_reader_save_as (reader, "writer3.syscap", &error); - g_assert_no_error (error); - g_assert_cmpint (r, ==, TRUE); + r = sysprof_capture_reader_save_as (reader, "writer3.syscap"); + g_assert_true (r); g_clear_pointer (&reader, sysprof_capture_reader_unref); - reader = sysprof_capture_reader_new ("writer3.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("writer3.syscap"); + g_assert_nonnull (reader); count = 0; while (sysprof_capture_reader_skip (reader)) @@ -603,7 +595,6 @@ test_reader_writer_log (void) SysprofCaptureReader *reader; const SysprofCaptureLog *log; SysprofCaptureFrameType type; - GError *error = NULL; gint r; writer = sysprof_capture_writer_new ("log1.syscap", 0); @@ -614,9 +605,8 @@ test_reader_writer_log (void) g_clear_pointer (&writer, sysprof_capture_writer_unref); - reader = sysprof_capture_reader_new ("log1.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("log1.syscap"); + g_assert_nonnull (reader); log = sysprof_capture_reader_read_log (reader); g_assert_nonnull (log); @@ -657,7 +647,6 @@ test_reader_writer_mark (void) SysprofCaptureReader *reader; const SysprofCaptureMark *mark; SysprofCaptureFrameType type; - GError *error = NULL; gint r; writer = sysprof_capture_writer_new ("mark1.syscap", 0); @@ -667,9 +656,8 @@ test_reader_writer_mark (void) g_clear_pointer (&writer, sysprof_capture_writer_unref); - reader = sysprof_capture_reader_new ("mark1.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("mark1.syscap"); + g_assert_nonnull (reader); mark = sysprof_capture_reader_read_mark (reader); g_assert_nonnull (mark); @@ -704,7 +692,6 @@ test_reader_writer_metadata (void) SysprofCaptureReader *reader; const SysprofCaptureMetadata *metadata; SysprofCaptureFrameType type; - GError *error = NULL; gint r; writer = sysprof_capture_writer_new ("metadata1.syscap", 0); @@ -717,9 +704,8 @@ test_reader_writer_metadata (void) g_clear_pointer (&writer, sysprof_capture_writer_unref); - reader = sysprof_capture_reader_new ("metadata1.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("metadata1.syscap"); + g_assert_nonnull (reader); metadata = sysprof_capture_reader_read_metadata (reader); g_assert_nonnull (metadata); @@ -752,7 +738,6 @@ test_reader_writer_file (void) SysprofCaptureWriter *writer; SysprofCaptureReader *reader; SysprofCaptureFrameType type; - GError *error = NULL; gsize data_len; guint count = 0; gint fd; @@ -775,9 +760,8 @@ test_reader_writer_file (void) g_clear_pointer (&writer, sysprof_capture_writer_unref); - reader = sysprof_capture_reader_new ("file1.syscap", &error); - g_assert_no_error (error); - g_assert (reader != NULL); + reader = sysprof_capture_reader_new ("file1.syscap"); + g_assert_nonnull (reader); while (count < 2) { diff --git a/src/tests/test-resolvers.c b/src/tests/test-resolvers.c index 8ba0e34b..7cb85154 100644 --- a/src/tests/test-resolvers.c +++ b/src/tests/test-resolvers.c @@ -43,8 +43,12 @@ main (gint argc, g_ptr_array_add (resolvers, sysprof_jitmap_symbol_resolver_new ()); g_ptr_array_add (resolvers, sysprof_kernel_symbol_resolver_new ()); - if (!(reader = sysprof_capture_reader_new (filename, &error))) - g_error ("%s", error->message); + if (!(reader = sysprof_capture_reader_new (filename))) + { + int errsv = errno; + g_error ("%s", g_strerror (errsv)); + return 1; + } for (guint r = 0; r < resolvers->len; r++) { diff --git a/src/tools/list-threads.c b/src/tools/list-threads.c index 30837c57..2a60540c 100644 --- a/src/tools/list-threads.c +++ b/src/tools/list-threads.c @@ -59,9 +59,9 @@ main (gint argc, } if (g_strcmp0 ("-", argv[1]) == 0) - reader = sysprof_capture_reader_new_from_fd (dup (STDIN_FILENO), 0); + reader = sysprof_capture_reader_new_from_fd (dup (STDIN_FILENO)); else - reader = sysprof_capture_reader_new (argv[1], 0); + reader = sysprof_capture_reader_new (argv[1]); if (reader == NULL) { diff --git a/src/tools/sysprof-cat.c b/src/tools/sysprof-cat.c index eeca1399..b49e2005 100644 --- a/src/tools/sysprof-cat.c +++ b/src/tools/sysprof-cat.c @@ -73,10 +73,11 @@ main (gint argc, g_autoptr(SysprofCaptureReader) reader = NULL; g_autoptr(GError) error = NULL; - if (!(reader = sysprof_capture_reader_new (argv[i], &error))) + if (!(reader = sysprof_capture_reader_new (argv[i]))) { + int errsv = errno; g_printerr ("Failed to create reader for \"%s\": %s\n", - argv[i], error->message); + argv[i], g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tools/sysprof-cli.c b/src/tools/sysprof-cli.c index 32f3c428..b5d02a95 100644 --- a/src/tools/sysprof-cli.c +++ b/src/tools/sysprof-cli.c @@ -136,10 +136,11 @@ merge_files (gint argc, g_autoptr(SysprofCaptureReader) reader = NULL; g_autoptr(GError) error = NULL; - if (!(reader = sysprof_capture_reader_new (argv[i], &error))) + if (!(reader = sysprof_capture_reader_new (argv[i]))) { + int errsv = errno; g_printerr ("Failed to create reader for \"%s\": %s\n", - argv[i], error->message); + argv[i], g_strerror (errsv)); return EXIT_FAILURE; } diff --git a/src/tools/sysprof-dump.c b/src/tools/sysprof-dump.c index d90963dc..cb85880d 100644 --- a/src/tools/sysprof-dump.c +++ b/src/tools/sysprof-dump.c @@ -56,9 +56,10 @@ main (gint argc, return EXIT_FAILURE; } - if ((reader = sysprof_capture_reader_new (argv[1], &error)) == NULL) + if ((reader = sysprof_capture_reader_new (argv[1])) == NULL) { - g_printerr ("%s\n", error->message); + int errsv = errno; + g_printerr ("%s\n", g_strerror (errsv)); return EXIT_FAILURE; } From 45c8c95706f08b26bbbd8cee62edc72122e75e10 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 12:59:53 +0100 Subject: [PATCH 55/62] libsysprof-capture: Drop GError usage from SysprofCaptureWriter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use `errno` instead, which is icky, but given that all of the failure modes are from POSIX I/O functions, it’s at least in keeping with them. This is a major API break. Signed-off-by: Philip Withnall Helps: #40 --- .../sysprof-capture-writer-cat.c | 12 +-- .../sysprof-capture-writer.c | 97 ++++++++++--------- .../sysprof-capture-writer.h | 15 +-- src/libsysprof/sysprof-local-profiler.c | 3 +- src/libsysprof/sysprof-proxy-source.c | 9 +- src/libsysprof/sysprof-tracefd-source.c | 3 +- src/tests/test-capture.c | 31 ++---- src/tools/sysprof-cat.c | 5 +- src/tools/sysprof-cli.c | 6 +- 9 files changed, 82 insertions(+), 99 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index d33b4bb8..7b8bac54 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -57,6 +57,7 @@ #include "config.h" #include +#include #include #include #include @@ -169,9 +170,8 @@ translate_table_translate (TranslateTable *tables, } bool -sysprof_capture_writer_cat (SysprofCaptureWriter *self, - SysprofCaptureReader *reader, - GError **error) +sysprof_capture_writer_cat (SysprofCaptureWriter *self, + SysprofCaptureReader *reader) { TranslateTable tables[N_TRANSLATE] = { 0, }; SysprofCaptureFrameType type; @@ -552,13 +552,9 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self, return true; panic: - g_set_error (error, - G_FILE_ERROR, - G_FILE_ERROR_FAILED, - "Failed to write data"); - translate_table_clear (tables, TRANSLATE_ADDR); translate_table_clear (tables, TRANSLATE_CTR); + errno = EIO; return false; } diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 65df4ac5..444ce5f0 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -918,24 +918,26 @@ sysprof_capture_writer_flush (SysprofCaptureWriter *self) * sysprof_capture_writer_save_as: * @self: A #SysprofCaptureWriter * @filename: the file to save the capture as - * @error: a location for a #GError or %NULL. * * Saves the captured data as the file @filename. * * This is primarily useful if the writer was created with a memory-backed * file-descriptor such as a memfd or tmpfs file on Linux. * - * Returns: %TRUE if successful, otherwise %FALSE and @error is set. + * `errno` is set on error, to any of the errors returned by `open()`, + * sysprof_capture_writer_flush(), `lseek()` or `sendfile()`. + * + * Returns: %TRUE if successful, otherwise %FALSE and `errno` is set. */ bool -sysprof_capture_writer_save_as (SysprofCaptureWriter *self, - const char *filename, - GError **error) +sysprof_capture_writer_save_as (SysprofCaptureWriter *self, + const char *filename) { size_t to_write; off_t in_off; off_t pos; int fd = -1; + int errsv; assert (self != NULL); assert (self->fd != -1); @@ -975,10 +977,7 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter *self, return true; handle_errno: - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); + errsv = errno; if (fd != -1) { @@ -986,6 +985,8 @@ handle_errno: unlink (filename); } + errno = errsv; + return false; } @@ -993,7 +994,6 @@ handle_errno: * _sysprof_capture_writer_splice_from_fd: * @self: An #SysprofCaptureWriter * @fd: the fd to read from. - * @error: A location for a #GError, or %NULL. * * This is internal API for SysprofCaptureWriter and SysprofCaptureReader to * communicate when splicing a reader into a writer. @@ -1003,12 +1003,14 @@ handle_errno: * * This will not advance the position of @fd. * - * Returns: %TRUE if successful; otherwise %FALSE and @error is set. + * `errno` is set on error, to any of the errors returned by `fstat()` or + * `sendfile()`, or `EBADMSG` if the file is corrupt. + * + * Returns: %TRUE if successful; otherwise %FALSE and `errno` is set. */ bool -_sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, - int fd, - GError **error) +_sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, + int fd) { struct stat stbuf; off_t in_off; @@ -1022,10 +1024,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, if (stbuf.st_size < 256) { - g_set_error (error, - G_FILE_ERROR, - G_FILE_ERROR_INVAL, - "Cannot splice, possibly corrupt file."); + errno = EBADMSG; return false; } @@ -1052,11 +1051,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, return true; handle_errno: - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); - + /* errno is propagated */ return false; } @@ -1064,22 +1059,25 @@ handle_errno: * sysprof_capture_writer_splice: * @self: An #SysprofCaptureWriter * @dest: An #SysprofCaptureWriter - * @error: A location for a #GError, or %NULL. * * This function will copy the capture @self into the capture @dest. This * tries to be semi-efficient by using sendfile() to copy the contents between * the captures. @self and @dest will be flushed before the contents are copied * into the @dest file-descriptor. * - * Returns: %TRUE if successful, otherwise %FALSE and and @error is set. + * `errno` is set on error, to any of the errors returned by + * sysprof_capture_writer_flush(), `lseek()` or + * _sysprof_capture_writer_splice_from_fd(). + * + * Returns: %TRUE if successful, otherwise %FALSE and and `errno` is set. */ bool -sysprof_capture_writer_splice (SysprofCaptureWriter *self, - SysprofCaptureWriter *dest, - GError **error) +sysprof_capture_writer_splice (SysprofCaptureWriter *self, + SysprofCaptureWriter *dest) { bool ret; off_t pos; + int errsv; assert (self != NULL); assert (self->fd != -1); @@ -1095,30 +1093,25 @@ sysprof_capture_writer_splice (SysprofCaptureWriter *self, goto handle_errno; /* Perform the splice */ - ret = _sysprof_capture_writer_splice_from_fd (dest, self->fd, error); + ret = _sysprof_capture_writer_splice_from_fd (dest, self->fd); + errsv = errno; /* Now reset or file-descriptor position (it should be the same */ if (pos != lseek (self->fd, pos, SEEK_SET)) - { - ret = false; - goto handle_errno; - } + goto handle_errno; + if (!ret) + errno = errsv; return ret; handle_errno: - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); - + /* errno is propagated */ return false; } /** * sysprof_capture_writer_create_reader: * @self: A #SysprofCaptureWriter - * @error: a location for a #GError, or %NULL * * Creates a new reader for the writer. * @@ -1127,11 +1120,14 @@ handle_errno: * also consuming from the reader, you could get transient failures unless you * synchronize the operations. * + * `errno` is set on error, to any of the errors returned by + * sysprof_capture_writer_flush(), `dup()` or + * sysprof_capture_reader_new_from_fd(). + * * Returns: (transfer full): A #SysprofCaptureReader. */ SysprofCaptureReader * -sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, - GError **error) +sysprof_capture_writer_create_reader (SysprofCaptureWriter *self) { SysprofCaptureReader *ret; int copy; @@ -1141,10 +1137,7 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, if (!sysprof_capture_writer_flush (self)) { - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (errno), - "%s", g_strerror (errno)); + /* errno is propagated */ return NULL; } @@ -1153,10 +1146,18 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, * uses positioned reads. */ if (-1 == (copy = dup (self->fd))) - return NULL; + { + /* errno is propagated */ + return NULL; + } - if ((ret = sysprof_capture_reader_new_from_fd (copy, error))) - sysprof_capture_reader_set_stat (ret, &self->stat); + if (!(ret = sysprof_capture_reader_new_from_fd (copy))) + { + /* errno is propagated */ + return NULL; + } + + sysprof_capture_reader_set_stat (ret, &self->stat); return sysprof_steal_pointer (&ret); } diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h index bf39f0f6..e92007df 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.h +++ b/src/libsysprof-capture/sysprof-capture-writer.h @@ -207,29 +207,24 @@ SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_writer_flush (SysprofCaptureWriter *self); SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_writer_save_as (SysprofCaptureWriter *self, - const char *filename, - GError **error); + const char *filename); SYSPROF_AVAILABLE_IN_ALL unsigned int sysprof_capture_writer_request_counter (SysprofCaptureWriter *self, unsigned int n_counters); SYSPROF_AVAILABLE_IN_ALL -SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self, - GError **error); +SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self); SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_writer_splice (SysprofCaptureWriter *self, - SysprofCaptureWriter *dest, - GError **error); + SysprofCaptureWriter *dest); SYSPROF_AVAILABLE_IN_ALL bool sysprof_capture_writer_cat (SysprofCaptureWriter *self, - SysprofCaptureReader *reader, - GError **error); + SysprofCaptureReader *reader); SYSPROF_INTERNAL bool _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self, const SysprofCaptureFrame *frame); SYSPROF_INTERNAL bool _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self, - int fd, - GError **error) SYSPROF_INTERNAL; + int fd) SYSPROF_INTERNAL; SYSPROF_INTERNAL bool _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self, int64_t start_time, diff --git a/src/libsysprof/sysprof-local-profiler.c b/src/libsysprof/sysprof-local-profiler.c index 0fc32aaf..9aecaa7b 100644 --- a/src/libsysprof/sysprof-local-profiler.c +++ b/src/libsysprof/sysprof-local-profiler.c @@ -198,7 +198,8 @@ sysprof_local_profiler_finish_stopping (SysprofLocalProfiler *self) g_assert (priv->is_stopping == TRUE); g_assert (priv->stopping->len == 0); - reader = sysprof_capture_writer_create_reader (priv->writer, 0); + reader = sysprof_capture_writer_create_reader (priv->writer); + g_assert (reader != NULL); for (guint i = 0; i < priv->sources->len; i++) { diff --git a/src/libsysprof/sysprof-proxy-source.c b/src/libsysprof/sysprof-proxy-source.c index d290c8bf..61e6d59b 100644 --- a/src/libsysprof/sysprof-proxy-source.c +++ b/src/libsysprof/sysprof-proxy-source.c @@ -450,12 +450,11 @@ sysprof_proxy_source_cat (SysprofProxySource *self, g_assert (SYSPROF_IS_PROXY_SOURCE (self)); g_assert (reader != NULL); - if (self->writer != NULL) + if (self->writer != NULL && + !sysprof_capture_writer_cat (self->writer, reader)) { - g_autoptr(GError) error = NULL; - - if (!sysprof_capture_writer_cat (self->writer, reader, &error)) - g_warning ("Failed to join reader: %s\n", error->message); + int errsv = errno; + g_warning ("Failed to join reader: %s", g_strerror (errsv)); } } diff --git a/src/libsysprof/sysprof-tracefd-source.c b/src/libsysprof/sysprof-tracefd-source.c index 061f9132..72c9fb05 100644 --- a/src/libsysprof/sysprof-tracefd-source.c +++ b/src/libsysprof/sysprof-tracefd-source.c @@ -259,8 +259,9 @@ sysprof_tracefd_source_stop (SysprofSource *source) { g_autoptr(SysprofCaptureReader) reader = NULL; + /* FIXME: Error handling is ignored here */ if ((reader = sysprof_capture_reader_new_from_fd (priv->tracefd))) - sysprof_capture_writer_cat (priv->writer, reader, NULL); + sysprof_capture_writer_cat (priv->writer, reader); priv->tracefd = -1; } diff --git a/src/tests/test-capture.c b/src/tests/test-capture.c index 8f2d4f3c..bfa56950 100644 --- a/src/tests/test-capture.c +++ b/src/tests/test-capture.c @@ -399,9 +399,8 @@ test_reader_basic (void) g_assert_not_reached (); } - r = sysprof_capture_writer_save_as (writer, "capture-file.bak", &error); - g_assert_no_error (error); - g_assert (r); + r = sysprof_capture_writer_save_as (writer, "capture-file.bak"); + g_assert_true (r); g_assert (g_file_test ("capture-file.bak", G_FILE_TEST_IS_REGULAR)); /* make sure contents are equal */ @@ -462,7 +461,6 @@ test_writer_splice (void) SysprofCaptureWriter *writer2; SysprofCaptureReader *reader; SysprofCaptureFrameType type; - GError *error = NULL; guint i; gint r; @@ -472,9 +470,8 @@ test_writer_splice (void) for (i = 0; i < 1000; i++) sysprof_capture_writer_add_timestamp (writer1, SYSPROF_CAPTURE_CURRENT_TIME, -1, -1); - r = sysprof_capture_writer_splice (writer1, writer2, &error); - g_assert_no_error (error); - g_assert (r == TRUE); + r = sysprof_capture_writer_splice (writer1, writer2); + g_assert_true (r); g_clear_pointer (&writer1, sysprof_capture_writer_unref); g_clear_pointer (&writer2, sysprof_capture_writer_unref); @@ -815,7 +812,6 @@ test_reader_writer_cat_jitmap (void) SysprofCaptureWriter *res; SysprofCaptureReader *reader; const SysprofCaptureSample *sample; - GError *error = NULL; SysprofCaptureAddress addrs[20]; gboolean r; @@ -851,26 +847,21 @@ test_reader_writer_cat_jitmap (void) addrs, G_N_ELEMENTS (addrs)); - reader = sysprof_capture_writer_create_reader (writer1, &error); - g_assert_no_error (error); + reader = sysprof_capture_writer_create_reader (writer1); g_assert_nonnull (reader); - r = sysprof_capture_writer_cat (res, reader, &error); - g_assert_no_error (error); + r = sysprof_capture_writer_cat (res, reader); g_assert_true (r); sysprof_capture_writer_unref (writer1); sysprof_capture_reader_unref (reader); - reader = sysprof_capture_writer_create_reader (writer2, &error); - g_assert_no_error (error); + reader = sysprof_capture_writer_create_reader (writer2); g_assert_nonnull (reader); - r = sysprof_capture_writer_cat (res, reader, &error); - g_assert_no_error (error); + r = sysprof_capture_writer_cat (res, reader); g_assert_true (r); sysprof_capture_writer_unref (writer2); sysprof_capture_reader_unref (reader); - reader = sysprof_capture_writer_create_reader (res, &error); - g_assert_no_error (error); + reader = sysprof_capture_writer_create_reader (res); g_assert_nonnull (reader); sysprof_capture_reader_read_jitmap (reader); sample = sysprof_capture_reader_read_sample (reader); @@ -891,7 +882,6 @@ test_writer_memory_alloc_free (void) { SysprofCaptureWriter *writer; SysprofCaptureReader *reader; - GError *error = NULL; SysprofCaptureAddress addrs[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, @@ -916,8 +906,7 @@ test_writer_memory_alloc_free (void) sysprof_capture_writer_flush (writer); - reader = sysprof_capture_writer_create_reader (writer, &error); - g_assert_no_error (error); + reader = sysprof_capture_writer_create_reader (writer); g_assert_nonnull (reader); for (guint i = 0; i < 20; i++) diff --git a/src/tools/sysprof-cat.c b/src/tools/sysprof-cat.c index b49e2005..d023d82d 100644 --- a/src/tools/sysprof-cat.c +++ b/src/tools/sysprof-cat.c @@ -81,10 +81,11 @@ main (gint argc, return EXIT_FAILURE; } - if (!sysprof_capture_writer_cat (writer, reader, &error)) + if (!sysprof_capture_writer_cat (writer, reader)) { + int errsv = errno; g_printerr ("Failed to join \"%s\": %s\n", - argv[i], error->message); + argv[i], g_strerror (errsv)); return EXIT_FAILURE; } } diff --git a/src/tools/sysprof-cli.c b/src/tools/sysprof-cli.c index b5d02a95..d4ead58a 100644 --- a/src/tools/sysprof-cli.c +++ b/src/tools/sysprof-cli.c @@ -134,7 +134,6 @@ merge_files (gint argc, for (guint i = 1; i < argc; i++) { g_autoptr(SysprofCaptureReader) reader = NULL; - g_autoptr(GError) error = NULL; if (!(reader = sysprof_capture_reader_new (argv[i]))) { @@ -144,10 +143,11 @@ merge_files (gint argc, return EXIT_FAILURE; } - if (!sysprof_capture_writer_cat (writer, reader, &error)) + if (!sysprof_capture_writer_cat (writer, reader)) { + int errsv = errno; g_printerr ("Failed to join \"%s\": %s\n", - argv[i], error->message); + argv[i], g_strerror (errsv)); return EXIT_FAILURE; } } From 608582d3c4d01da42d659abe1368910166acebc7 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 13:21:43 +0100 Subject: [PATCH 56/62] libsysprof-capture: Drop GLib dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit None of the code uses it any more. This means that `libsysprof-capture.a` can now be used within `libglib-2.0.so` for collecting main loop statistics. Brought to you by Opeth’s Deliverance on repeat. Signed-off-by: Philip Withnall Fixes: #40 --- src/libsysprof-capture/mapped-ring-buffer.h | 1 - src/libsysprof-capture/meson.build | 4 ---- src/libsysprof-capture/sysprof-capture-types.h | 1 - src/libsysprof-capture/sysprof-capture-util-private.h | 2 -- src/libsysprof-capture/sysprof-capture-util.c | 1 - src/libsysprof-capture/sysprof-capture-writer-cat.c | 1 - src/libsysprof-capture/sysprof-capture-writer.c | 1 - src/libsysprof-capture/sysprof-capture.h | 2 -- src/libsysprof-capture/sysprof-clock.h | 1 - src/libsysprof-capture/sysprof-platform.c | 2 -- src/libsysprof-capture/sysprof-version-macros.h | 2 -- 11 files changed, 18 deletions(-) diff --git a/src/libsysprof-capture/mapped-ring-buffer.h b/src/libsysprof-capture/mapped-ring-buffer.h index eb2c6ca4..6bf2bb7a 100644 --- a/src/libsysprof-capture/mapped-ring-buffer.h +++ b/src/libsysprof-capture/mapped-ring-buffer.h @@ -20,7 +20,6 @@ #pragma once -#include #include #include diff --git a/src/libsysprof-capture/meson.build b/src/libsysprof-capture/meson.build index 233e3314..c8cd6895 100644 --- a/src/libsysprof-capture/meson.build +++ b/src/libsysprof-capture/meson.build @@ -37,9 +37,6 @@ configure_file( ) libsysprof_capture_deps = [ - glib_dep, - gio_dep, - gio_unix_dep, dependency('threads'), ] @@ -70,7 +67,6 @@ pkgconfig.generate( filebase: 'sysprof-capture-@0@'.format(libsysprof_api_version), description: 'The static capture library for tools that generate profiling capture data', install_dir: join_paths(get_option('libdir'), 'pkgconfig'), - requires: [ 'glib-2.0' ], variables: [ 'datadir=' + datadir_for_pc_file, ], diff --git a/src/libsysprof-capture/sysprof-capture-types.h b/src/libsysprof-capture/sysprof-capture-types.h index f249be5e..2031fc7e 100644 --- a/src/libsysprof-capture/sysprof-capture-types.h +++ b/src/libsysprof-capture/sysprof-capture-types.h @@ -58,7 +58,6 @@ #include #include -#include #include #include #include diff --git a/src/libsysprof-capture/sysprof-capture-util-private.h b/src/libsysprof-capture/sysprof-capture-util-private.h index 47c59d13..1b1d93e9 100644 --- a/src/libsysprof-capture/sysprof-capture-util-private.h +++ b/src/libsysprof-capture/sysprof-capture-util-private.h @@ -56,8 +56,6 @@ #pragma once -#include - #ifdef __linux__ # include #endif diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c index b731d030..0bbea06a 100644 --- a/src/libsysprof-capture/sysprof-capture-util.c +++ b/src/libsysprof-capture/sysprof-capture-util.c @@ -58,7 +58,6 @@ #include #include -#include #include #ifdef _WIN32 diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c b/src/libsysprof-capture/sysprof-capture-writer-cat.c index 7b8bac54..66171b92 100644 --- a/src/libsysprof-capture/sysprof-capture-writer-cat.c +++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c @@ -58,7 +58,6 @@ #include #include -#include #include #include #include diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c index 444ce5f0..f890da98 100644 --- a/src/libsysprof-capture/sysprof-capture-writer.c +++ b/src/libsysprof-capture/sysprof-capture-writer.c @@ -64,7 +64,6 @@ #include #include #include -#include #include #include #include diff --git a/src/libsysprof-capture/sysprof-capture.h b/src/libsysprof-capture/sysprof-capture.h index e5b2dd5b..ac2f1c25 100644 --- a/src/libsysprof-capture/sysprof-capture.h +++ b/src/libsysprof-capture/sysprof-capture.h @@ -56,8 +56,6 @@ #pragma once -#include - #define SYSPROF_CAPTURE_INSIDE # include "sysprof-address.h" diff --git a/src/libsysprof-capture/sysprof-clock.h b/src/libsysprof-capture/sysprof-clock.h index 288a1fc0..c127c09d 100644 --- a/src/libsysprof-capture/sysprof-clock.h +++ b/src/libsysprof-capture/sysprof-clock.h @@ -56,7 +56,6 @@ #pragma once -#include #include #include diff --git a/src/libsysprof-capture/sysprof-platform.c b/src/libsysprof-capture/sysprof-platform.c index 1a344a52..a80ab890 100644 --- a/src/libsysprof-capture/sysprof-platform.c +++ b/src/libsysprof-capture/sysprof-platform.c @@ -56,8 +56,6 @@ #include "config.h" -#include -#include #include #include #include diff --git a/src/libsysprof-capture/sysprof-version-macros.h b/src/libsysprof-capture/sysprof-version-macros.h index 8152735d..43930127 100644 --- a/src/libsysprof-capture/sysprof-version-macros.h +++ b/src/libsysprof-capture/sysprof-version-macros.h @@ -56,8 +56,6 @@ #pragma once -#include - #include "sysprof-version.h" #ifndef _SYSPROF_EXTERN From 97ddf5a0cc771e1673eec674c6a933a665ab6db2 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 12:11:44 -0700 Subject: [PATCH 57/62] libsysprof: port UI to new ABI Some minor changes were necessary so that we could change the ABI in libsysprof-capture to be free from GLib (and therefore used by GLib). This also adds some wrappers for capture API in libsysprof so that we can continue to use GError from UI code. --- src/libsysprof-ui/sysprof-battery-aid.c | 2 +- src/libsysprof-ui/sysprof-callgraph-aid.c | 2 +- src/libsysprof-ui/sysprof-counters-aid.c | 2 +- src/libsysprof-ui/sysprof-cpu-aid.c | 2 +- src/libsysprof-ui/sysprof-depth-visualizer.c | 4 +- src/libsysprof-ui/sysprof-details-page.c | 2 +- src/libsysprof-ui/sysprof-diskstat-aid.c | 2 +- src/libsysprof-ui/sysprof-display.c | 6 +- src/libsysprof-ui/sysprof-duplex-visualizer.c | 4 +- src/libsysprof-ui/sysprof-line-visualizer.c | 4 +- src/libsysprof-ui/sysprof-log-model.c | 2 +- src/libsysprof-ui/sysprof-logs-aid.c | 2 +- src/libsysprof-ui/sysprof-marks-aid.c | 2 +- src/libsysprof-ui/sysprof-marks-model.c | 2 +- src/libsysprof-ui/sysprof-memprof-aid.c | 2 +- src/libsysprof-ui/sysprof-netdev-aid.c | 2 +- src/libsysprof-ui/sysprof-procs-visualizer.c | 4 +- src/libsysprof-ui/sysprof-rapl-aid.c | 2 +- src/libsysprof-ui/sysprof-time-visualizer.c | 2 +- src/libsysprof-ui/sysprof-visualizers-frame.c | 2 +- src/libsysprof/sysprof-capture-gobject.c | 67 ++++++++++++++++++- src/libsysprof/sysprof-capture-gobject.h | 14 ++++ src/libsysprof/sysprof.h | 1 + src/tests/test-capture-view.c | 2 +- 24 files changed, 107 insertions(+), 29 deletions(-) diff --git a/src/libsysprof-ui/sysprof-battery-aid.c b/src/libsysprof-ui/sysprof-battery-aid.c index ab44a281..8bef1979 100644 --- a/src/libsysprof-ui/sysprof-battery-aid.c +++ b/src/libsysprof-ui/sysprof-battery-aid.c @@ -81,7 +81,7 @@ sysprof_battery_aid_prepare (SysprofAid *self, #endif } -static gboolean +static bool collect_battery_counters (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-callgraph-aid.c b/src/libsysprof-ui/sysprof-callgraph-aid.c index 5b0ebb07..f12be766 100644 --- a/src/libsysprof-ui/sysprof-callgraph-aid.c +++ b/src/libsysprof-ui/sysprof-callgraph-aid.c @@ -114,7 +114,7 @@ sysprof_callgraph_aid_prepare (SysprofAid *self, #endif } -static gboolean +static bool discover_samples_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-counters-aid.c b/src/libsysprof-ui/sysprof-counters-aid.c index 2324569e..c1d855bb 100644 --- a/src/libsysprof-ui/sysprof-counters-aid.c +++ b/src/libsysprof-ui/sysprof-counters-aid.c @@ -123,7 +123,7 @@ build_title (const SysprofCaptureCounter *ctr) return g_string_free (str, FALSE); } -static gboolean +static bool collect_counters (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-cpu-aid.c b/src/libsysprof-ui/sysprof-cpu-aid.c index 39029576..56094888 100644 --- a/src/libsysprof-ui/sysprof-cpu-aid.c +++ b/src/libsysprof-ui/sysprof-cpu-aid.c @@ -85,7 +85,7 @@ sysprof_cpu_aid_prepare (SysprofAid *self, #endif } -static gboolean +static bool collect_info (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-depth-visualizer.c b/src/libsysprof-ui/sysprof-depth-visualizer.c index 118489d7..a3a4df5f 100644 --- a/src/libsysprof-ui/sysprof-depth-visualizer.c +++ b/src/libsysprof-ui/sysprof-depth-visualizer.c @@ -62,7 +62,7 @@ state_free (State *st) g_slice_free (State, st); } -static gboolean +static bool discover_max_n_addr (const SysprofCaptureFrame *frame, gpointer user_data) { @@ -78,7 +78,7 @@ discover_max_n_addr (const SysprofCaptureFrame *frame, return TRUE; } -static gboolean +static bool build_point_cache_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-details-page.c b/src/libsysprof-ui/sysprof-details-page.c index 3f9fcbda..0c41ec46 100644 --- a/src/libsysprof-ui/sysprof-details-page.c +++ b/src/libsysprof-ui/sysprof-details-page.c @@ -135,7 +135,7 @@ update_cpu_info_cb (GObject *object, gtk_label_set_label (self->cpu_label, str); } -static gboolean +static bool cpu_info_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-diskstat-aid.c b/src/libsysprof-ui/sysprof-diskstat-aid.c index aff40ecd..c87cb3e7 100644 --- a/src/libsysprof-ui/sysprof-diskstat-aid.c +++ b/src/libsysprof-ui/sysprof-diskstat-aid.c @@ -80,7 +80,7 @@ sysprof_diskstat_aid_prepare (SysprofAid *self, sysprof_profiler_add_source (profiler, source); } -static gboolean +static bool collect_diskstat_counters (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-display.c b/src/libsysprof-ui/sysprof-display.c index e9886d5a..6be5faf1 100644 --- a/src/libsysprof-ui/sysprof-display.c +++ b/src/libsysprof-ui/sysprof-display.c @@ -143,7 +143,7 @@ sysprof_display_profiler_stopped_cb (SysprofDisplay *self, g_autoptr(SysprofCaptureReader) reader = NULL; g_autoptr(GError) error = NULL; - if (!(reader = sysprof_capture_writer_create_reader (writer, &error))) + if (!(reader = sysprof_capture_writer_create_reader_with_error (writer, &error))) { g_warning ("Failed to create capture creader: %s\n", error->message); gtk_stack_set_visible_child (priv->stack, GTK_WIDGET (priv->failed_view)); @@ -1071,7 +1071,7 @@ sysprof_display_open (SysprofDisplay *self, g_set_object (&priv->file, file); - if (!(reader = sysprof_capture_reader_new (path, &error))) + if (!(reader = sysprof_capture_reader_new_with_error (path, &error))) { GtkWidget *dialog; GtkWidget *window; @@ -1229,7 +1229,7 @@ sysprof_display_save (SysprofDisplay *self) g_autofree gchar *path = g_file_get_path (file); g_autoptr(GError) error = NULL; - if (!sysprof_capture_reader_save_as (priv->reader, path, &error)) + if (!sysprof_capture_reader_save_as_with_error (priv->reader, path, &error)) { GtkWidget *msg; diff --git a/src/libsysprof-ui/sysprof-duplex-visualizer.c b/src/libsysprof-ui/sysprof-duplex-visualizer.c index 82bdca03..1901258a 100644 --- a/src/libsysprof-ui/sysprof-duplex-visualizer.c +++ b/src/libsysprof-ui/sysprof-duplex-visualizer.c @@ -73,7 +73,7 @@ typedef struct G_DEFINE_TYPE (SysprofDuplexVisualizer, sysprof_duplex_visualizer, SYSPROF_TYPE_VISUALIZER) -static gboolean +static bool collect_ranges_cb (const SysprofCaptureFrame *frame, gpointer data) { @@ -126,7 +126,7 @@ collect_ranges_cb (const SysprofCaptureFrame *frame, return TRUE; } -static gboolean +static bool collect_values_cb (const SysprofCaptureFrame *frame, gpointer data) { diff --git a/src/libsysprof-ui/sysprof-line-visualizer.c b/src/libsysprof-ui/sysprof-line-visualizer.c index 188b2e1f..5075a4f9 100644 --- a/src/libsysprof-ui/sysprof-line-visualizer.c +++ b/src/libsysprof-ui/sysprof-line-visualizer.c @@ -613,7 +613,7 @@ calc_y_int64 (gint64 lower, return (gdouble)(value - lower) / (gdouble)(upper - lower); } -static gboolean +static bool sysprof_line_visualizer_load_data_frame_cb (const SysprofCaptureFrame *frame, gpointer user_data) { @@ -655,7 +655,7 @@ sysprof_line_visualizer_load_data_frame_cb (const SysprofCaptureFrame *frame, return TRUE; } -static gboolean +static bool sysprof_line_visualizer_load_data_range_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-log-model.c b/src/libsysprof-ui/sysprof-log-model.c index 276ea063..8d67254f 100644 --- a/src/libsysprof-ui/sysprof-log-model.c +++ b/src/libsysprof-ui/sysprof-log-model.c @@ -289,7 +289,7 @@ sysprof_log_model_init (SysprofLogModel *self) self->items = g_array_new (FALSE, FALSE, sizeof (Item)); } -static gboolean +static bool cursor_foreach_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-logs-aid.c b/src/libsysprof-ui/sysprof-logs-aid.c index 466eab3e..8807cbe5 100644 --- a/src/libsysprof-ui/sysprof-logs-aid.c +++ b/src/libsysprof-ui/sysprof-logs-aid.c @@ -80,7 +80,7 @@ sysprof_logs_aid_new (void) return g_object_new (SYSPROF_TYPE_LOGS_AID, NULL); } -static gboolean +static bool find_marks_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-marks-aid.c b/src/libsysprof-ui/sysprof-marks-aid.c index a8f79b48..66b2c258 100644 --- a/src/libsysprof-ui/sysprof-marks-aid.c +++ b/src/libsysprof-ui/sysprof-marks-aid.c @@ -84,7 +84,7 @@ sysprof_marks_aid_new (void) return g_object_new (SYSPROF_TYPE_MARKS_AID, NULL); } -static gboolean +static bool find_marks_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-marks-model.c b/src/libsysprof-ui/sysprof-marks-model.c index fa5772af..08000641 100644 --- a/src/libsysprof-ui/sysprof-marks-model.c +++ b/src/libsysprof-ui/sysprof-marks-model.c @@ -301,7 +301,7 @@ sysprof_marks_model_init (SysprofMarksModel *self) self->items = g_array_new (FALSE, FALSE, sizeof (Item)); } -static gboolean +static bool cursor_foreach_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-memprof-aid.c b/src/libsysprof-ui/sysprof-memprof-aid.c index 6299b54f..21325177 100644 --- a/src/libsysprof-ui/sysprof-memprof-aid.c +++ b/src/libsysprof-ui/sysprof-memprof-aid.c @@ -87,7 +87,7 @@ sysprof_memprof_aid_prepare (SysprofAid *self, #endif } -static gboolean +static bool discover_samples_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-netdev-aid.c b/src/libsysprof-ui/sysprof-netdev-aid.c index f4a75a7a..c6970fd2 100644 --- a/src/libsysprof-ui/sysprof-netdev-aid.c +++ b/src/libsysprof-ui/sysprof-netdev-aid.c @@ -79,7 +79,7 @@ sysprof_netdev_aid_prepare (SysprofAid *self, sysprof_profiler_add_source (profiler, source); } -static gboolean +static bool collect_netdev_counters (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-procs-visualizer.c b/src/libsysprof-ui/sysprof-procs-visualizer.c index b4d34d29..c82007e5 100644 --- a/src/libsysprof-ui/sysprof-procs-visualizer.c +++ b/src/libsysprof-ui/sysprof-procs-visualizer.c @@ -65,7 +65,7 @@ discovery_ref (Discovery *d) return d; } -static gboolean +static bool discover_max_cb (const SysprofCaptureFrame *frame, gpointer user_data) { @@ -85,7 +85,7 @@ discover_max_cb (const SysprofCaptureFrame *frame, return TRUE; } -static gboolean +static bool calc_points_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-rapl-aid.c b/src/libsysprof-ui/sysprof-rapl-aid.c index 9da61d56..c5d11e61 100644 --- a/src/libsysprof-ui/sysprof-rapl-aid.c +++ b/src/libsysprof-ui/sysprof-rapl-aid.c @@ -69,7 +69,7 @@ sysprof_rapl_aid_new (void) return g_object_new (SYSPROF_TYPE_RAPL_AID, NULL); } -static gboolean +static bool collect_info (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-time-visualizer.c b/src/libsysprof-ui/sysprof-time-visualizer.c index cb1d01a7..01d32f23 100644 --- a/src/libsysprof-ui/sysprof-time-visualizer.c +++ b/src/libsysprof-ui/sysprof-time-visualizer.c @@ -382,7 +382,7 @@ calc_x (gint64 lower, return (gdouble)(value - lower) / (gdouble)(upper - lower); } -static gboolean +static bool sysprof_time_visualizer_load_data_frame_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof-ui/sysprof-visualizers-frame.c b/src/libsysprof-ui/sysprof-visualizers-frame.c index 95b990ca..96da31b9 100644 --- a/src/libsysprof-ui/sysprof-visualizers-frame.c +++ b/src/libsysprof-ui/sysprof-visualizers-frame.c @@ -608,7 +608,7 @@ compare_gint64 (const gint64 *a, return 0; } -static gboolean +static bool index_frame_times_frame_cb (const SysprofCaptureFrame *frame, gpointer user_data) { diff --git a/src/libsysprof/sysprof-capture-gobject.c b/src/libsysprof/sysprof-capture-gobject.c index dbd7cf2a..4e6f660d 100644 --- a/src/libsysprof/sysprof-capture-gobject.c +++ b/src/libsysprof/sysprof-capture-gobject.c @@ -20,10 +20,73 @@ #include "config.h" -#include "sysprof-capture-gobject.h" - +#include #include +#include "sysprof-capture-gobject.h" + G_DEFINE_BOXED_TYPE (SysprofCaptureReader, sysprof_capture_reader, (GBoxedCopyFunc)sysprof_capture_reader_ref, (GBoxedFreeFunc)sysprof_capture_reader_unref) G_DEFINE_BOXED_TYPE (SysprofCaptureWriter, sysprof_capture_writer, (GBoxedCopyFunc)sysprof_capture_writer_ref, (GBoxedFreeFunc)sysprof_capture_writer_unref) G_DEFINE_BOXED_TYPE (SysprofCaptureCursor, sysprof_capture_cursor, (GBoxedCopyFunc)sysprof_capture_cursor_ref, (GBoxedFreeFunc)sysprof_capture_cursor_unref) + +SysprofCaptureReader * +sysprof_capture_reader_new_with_error (const char *filename, + GError **error) +{ + SysprofCaptureReader *ret; + + if (!(ret = sysprof_capture_reader_new (filename))) + g_set_error_literal (error, + G_FILE_ERROR, + g_file_error_from_errno (errno), + g_strerror (errno)); + + return ret; +} + +SysprofCaptureReader * +sysprof_capture_reader_new_from_fd_with_error (int fd, + GError **error) +{ + SysprofCaptureReader *ret; + + if (!(ret = sysprof_capture_reader_new_from_fd (fd))) + g_set_error_literal (error, + G_FILE_ERROR, + g_file_error_from_errno (errno), + g_strerror (errno)); + + return ret; +} + +SysprofCaptureReader * +sysprof_capture_writer_create_reader_with_error (SysprofCaptureWriter *self, + GError **error) +{ + SysprofCaptureReader *ret; + + if (!(ret = sysprof_capture_writer_create_reader (self))) + g_set_error_literal (error, + G_FILE_ERROR, + g_file_error_from_errno (errno), + g_strerror (errno)); + + return ret; +} + +bool +sysprof_capture_reader_save_as_with_error (SysprofCaptureReader *self, + const char *filename, + GError **error) +{ + if (!sysprof_capture_reader_save_as (self, filename)) + { + g_set_error_literal (error, + G_FILE_ERROR, + g_file_error_from_errno (errno), + g_strerror (errno)); + return false; + } + + return true; +} diff --git a/src/libsysprof/sysprof-capture-gobject.h b/src/libsysprof/sysprof-capture-gobject.h index 80a2f4c2..ca6fbe23 100644 --- a/src/libsysprof/sysprof-capture-gobject.h +++ b/src/libsysprof/sysprof-capture-gobject.h @@ -41,4 +41,18 @@ GType sysprof_capture_writer_get_type (void); SYSPROF_AVAILABLE_IN_ALL GType sysprof_capture_cursor_get_type (void); +SYSPROF_AVAILABLE_IN_3_38 +SysprofCaptureReader *sysprof_capture_reader_new_with_error (const char *filename, + GError **error); +SYSPROF_AVAILABLE_IN_3_38 +SysprofCaptureReader *sysprof_capture_reader_new_from_fd_with_error (int fd, + GError **error); +SYSPROF_AVAILABLE_IN_3_38 +SysprofCaptureReader *sysprof_capture_writer_create_reader_with_error (SysprofCaptureWriter *self, + GError **error); +SYSPROF_AVAILABLE_IN_3_38 +bool sysprof_capture_reader_save_as_with_error (SysprofCaptureReader *self, + const char *filename, + GError **error); + G_END_DECLS diff --git a/src/libsysprof/sysprof.h b/src/libsysprof/sysprof.h index c9164f72..5abe64b6 100644 --- a/src/libsysprof/sysprof.h +++ b/src/libsysprof/sysprof.h @@ -18,6 +18,7 @@ #pragma once +#include #include G_BEGIN_DECLS diff --git a/src/tests/test-capture-view.c b/src/tests/test-capture-view.c index 1dca19aa..9768ca18 100644 --- a/src/tests/test-capture-view.c +++ b/src/tests/test-capture-view.c @@ -37,7 +37,7 @@ main (gint argc, return 1; } - if (!(reader = sysprof_capture_reader_new (argv[1], &error))) + if (!(reader = sysprof_capture_reader_new_with_error (argv[1], &error))) { g_printerr ("Failed to load reader: %s\n", error->message); return 1; From a9f136550b198dd33dff6f0ce2368af6b6baf28e Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 15:18:16 -0700 Subject: [PATCH 58/62] build: ensure libsysprof-memory-4.so is placed in libdir This makes it consistent with other preloads. --- src/libsysprof/preload/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsysprof/preload/meson.build b/src/libsysprof/preload/meson.build index 6906ec7e..a1a6e603 100644 --- a/src/libsysprof/preload/meson.build +++ b/src/libsysprof/preload/meson.build @@ -15,7 +15,7 @@ libsysprof_memory_preload = shared_library('sysprof-memory-@0@'.format(libsyspro ['sysprof-memory-collector.c'], dependencies: preload_deps, install: true, - install_dir: get_option('libexecdir'), + install_dir: get_option('libdir'), ) libsysprof_speedtrack_preload = shared_library('sysprof-speedtrack-@0@'.format(libsysprof_api_version), From 5d20c3f6cfe9014ecc01100d423a4473d8143276 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 15:19:16 -0700 Subject: [PATCH 59/62] collector: disable re-entrancy during initialization When creating a new collector, we need to prevent the following call to sysprof_malloc0() from re-entering us into the same position. --- src/libsysprof-capture/sysprof-collector.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index 1d0c57b7..b99cd1bb 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -410,6 +410,12 @@ sysprof_collector_get (void) { SysprofCollector *self, *old_collector; + /* First set the collector to invalid so anything recursive + * here fails instead of becoming re-entrant. + */ + pthread_setspecific (collector_key, COLLECTOR_INVALID); + + /* Now we can malloc without ending up here again */ self = sysprof_malloc0 (sizeof (SysprofCollector)); if (self == NULL) return COLLECTOR_INVALID; From 484bc328ee782487bb934fc3a3ac642f5d39fe8d Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 15:20:06 -0700 Subject: [PATCH 60/62] collector: set unlikely for pthread_once() --- src/libsysprof-capture/sysprof-collector.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index b99cd1bb..d95aee8c 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -475,7 +475,7 @@ collector_init_cb (void) void sysprof_collector_init (void) { - if (pthread_once (&collector_init, collector_init_cb) != 0) + if SYSPROF_UNLIKELY (pthread_once (&collector_init, collector_init_cb) != 0) abort (); } From 03326e82fa97ecc1b160b8dadb4d91550b9a5e9f Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 15:20:21 -0700 Subject: [PATCH 61/62] memory-collector: drop unused branch --- src/libsysprof/preload/sysprof-memory-collector.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libsysprof/preload/sysprof-memory-collector.c b/src/libsysprof/preload/sysprof-memory-collector.c index dd851ac9..ed6dc507 100644 --- a/src/libsysprof/preload/sysprof-memory-collector.c +++ b/src/libsysprof/preload/sysprof-memory-collector.c @@ -120,8 +120,6 @@ scratch_calloc (size_t nmemb, static void scratch_free (void *ptr) { - if ((char *)ptr >= scratch.buf && (char *)ptr < scratch.buf + scratch.off) - return; } static void From 760805c8b99a1750e2bb81715acd5d331c5979d1 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 2 Jul 2020 15:20:34 -0700 Subject: [PATCH 62/62] memory-collector: avoid tracking scratch memory free --- src/libsysprof/preload/sysprof-memory-collector.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libsysprof/preload/sysprof-memory-collector.c b/src/libsysprof/preload/sysprof-memory-collector.c index ed6dc507..72cf0515 100644 --- a/src/libsysprof/preload/sysprof-memory-collector.c +++ b/src/libsysprof/preload/sysprof-memory-collector.c @@ -197,8 +197,12 @@ realloc (void *ptr, void free (void *ptr) { - real_free (ptr); - track_free (ptr); + if G_LIKELY (ptr < (void *)scratch.buf || + ptr >= (void *)&scratch.buf[sizeof scratch.buf]) + { + real_free (ptr); + track_free (ptr); + } } void *