| 1 |
/* sysprof-tracer.c |
| 2 |
* |
| 3 |
* Copyright 2022 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <glib.h> |
| 24 |
#include <sysprof-capture.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#include "backtrace-helper.h" |
| 28 |
|
| 29 |
#include "gconstructor.h" |
| 30 |
|
| 31 |
#ifdef __GNUC__ |
| 32 |
# define GNUC_CHECK_VERSION(major, minor) \ |
| 33 |
((__GNUC__ > (major)) || \ |
| 34 |
((__GNUC__ == (major)) && \ |
| 35 |
(__GNUC_MINOR__ >= (minor)))) |
| 36 |
#else |
| 37 |
# define GNUC_CHECK_VERSION(major, minor) 0 |
| 38 |
#endif |
| 39 |
|
| 40 |
#if defined (G_HAS_CONSTRUCTORS) |
| 41 |
# ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA |
| 42 |
# pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(collector_init_ctor) |
| 43 |
# endif |
| 44 |
G_DEFINE_CONSTRUCTOR(collector_init_ctor) |
| 45 |
#else |
| 46 |
# error Your platform/compiler is missing constructor support |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifndef __APPLE__ |
| 50 |
# define profile_func_enter __cyg_profile_func_enter |
| 51 |
# define profile_func_exit __cyg_profile_func_exit |
| 52 |
#endif |
| 53 |
|
| 54 |
static void |
| 55 |
collector_init_ctor (void) |
| 56 |
{ |
| 57 |
backtrace_init (); |
| 58 |
sysprof_collector_init (); |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* What we would really want to do is to have a new frame type for enter/exit |
| 63 |
* tracing so that we can only push/pop the new address to the sample. Then |
| 64 |
* when decoding it can recreate stack traces if necessary. But for now, we |
| 65 |
* can emulate that by just adding a sample when we enter a function and |
| 66 |
* leave a function. The rest could be done in post-processing. |
| 67 |
*/ |
| 68 |
|
| 69 |
#if GNUC_CHECK_VERSION(3,0) |
| 70 |
__attribute__((no_instrument_function)) |
| 71 |
#endif |
| 72 |
void |
| 73 |
profile_func_enter (void *func, |
| 74 |
void *call_site) |
| 75 |
{ |
| 76 |
sysprof_collector_trace (backtrace_func, NULL, TRUE); |
| 77 |
} |
| 78 |
|
| 79 |
#if GNUC_CHECK_VERSION(3,0) |
| 80 |
__attribute__((no_instrument_function)) |
| 81 |
#endif |
| 82 |
void |
| 83 |
profile_func_exit (void *func, |
| 84 |
void *call_site) |
| 85 |
{ |
| 86 |
sysprof_collector_trace (backtrace_func, NULL, FALSE); |
| 87 |
} |