| 1 |
/* sysprof-categories.c |
| 2 |
* |
| 3 |
* Copyright 2023 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 "libsysprof-resources.h" |
| 24 |
|
| 25 |
#include "sysprof-callgraph-private.h" |
| 26 |
#include "sysprof-categories-private.h" |
| 27 |
#include "sysprof-enums.h" |
| 28 |
|
| 29 |
#include "line-reader-private.h" |
| 30 |
|
| 31 |
struct _SysprofCategories |
| 32 |
{ |
| 33 |
GHashTable *binary_nick_to_rules; |
| 34 |
}; |
| 35 |
|
| 36 |
enum { |
| 37 |
MATCH_EXACT, |
| 38 |
MATCH_PREFIX, |
| 39 |
MATCH_SUFFIX, |
| 40 |
}; |
| 41 |
|
| 42 |
typedef struct _Rule |
| 43 |
{ |
| 44 |
guint8 kind : 2; |
| 45 |
guint8 inherit : 1; |
| 46 |
guint8 category : 5; |
| 47 |
char *match; |
| 48 |
} Rule; |
| 49 |
|
| 50 |
static SysprofCallgraphCategory |
| 51 |
parse_category (const char *category) |
| 52 |
{ |
| 53 |
static GEnumClass *enum_class; |
| 54 |
const GEnumValue *value; |
| 55 |
|
| 56 |
if (enum_class == NULL) |
| 57 |
enum_class = g_type_class_ref (SYSPROF_TYPE_CALLGRAPH_CATEGORY); |
| 58 |
|
| 59 |
if (!(value = g_enum_get_value_by_nick (enum_class, category))) |
| 60 |
return 0; |
| 61 |
|
| 62 |
return value->value; |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
clear_rule (gpointer element) |
| 67 |
{ |
| 68 |
Rule *rule = element; |
| 69 |
|
| 70 |
g_clear_pointer (&rule->match, g_free); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
sysprof_categories_add_rule (SysprofCategories *categories, |
| 75 |
const char *binary_nick, |
| 76 |
const char *match, |
| 77 |
SysprofCallgraphCategory category, |
| 78 |
gboolean inherit) |
| 79 |
{ |
| 80 |
Rule rule; |
| 81 |
GArray *ar; |
| 82 |
|
| 83 |
g_assert (categories != NULL); |
| 84 |
g_assert (binary_nick != NULL); |
| 85 |
g_assert (match != NULL); |
| 86 |
|
| 87 |
if (match[0] == 0) |
| 88 |
return; |
| 89 |
|
| 90 |
if G_UNLIKELY (!(ar = g_hash_table_lookup (categories->binary_nick_to_rules, binary_nick))) |
| 91 |
{ |
| 92 |
ar = g_array_new (FALSE, FALSE, sizeof (Rule)); |
| 93 |
g_array_set_clear_func (ar, clear_rule); |
| 94 |
g_hash_table_insert (categories->binary_nick_to_rules, |
| 95 |
g_strdup (binary_nick), |
| 96 |
ar); |
| 97 |
} |
| 98 |
|
| 99 |
if (match[0] == '*') |
| 100 |
{ |
| 101 |
rule.kind = MATCH_SUFFIX; |
| 102 |
rule.match = g_strdup (&match[1]); |
| 103 |
rule.category = category; |
| 104 |
rule.inherit = inherit; |
| 105 |
|
| 106 |
g_array_append_val (ar, rule); |
| 107 |
} |
| 108 |
else if (match[strlen(match)-1] == '*') |
| 109 |
{ |
| 110 |
rule.kind = MATCH_PREFIX; |
| 111 |
rule.match = g_strndup (match, strlen (match)-1); |
| 112 |
rule.category = category; |
| 113 |
rule.inherit = inherit; |
| 114 |
|
| 115 |
g_array_append_val (ar, rule); |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
rule.kind = MATCH_EXACT; |
| 120 |
rule.match = g_strdup (match); |
| 121 |
rule.category = category; |
| 122 |
rule.inherit = inherit; |
| 123 |
|
| 124 |
g_array_append_val (ar, rule); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
SysprofCategories * |
| 129 |
sysprof_categories_new (void) |
| 130 |
{ |
| 131 |
SysprofCategories *categories; |
| 132 |
g_autoptr(GBytes) bytes = NULL; |
| 133 |
g_autofree char *binary_nick = NULL; |
| 134 |
const char *str; |
| 135 |
const char *lineptr; |
| 136 |
gsize line_len; |
| 137 |
gsize len; |
| 138 |
guint lineno = 0; |
| 139 |
LineReader reader; |
| 140 |
|
| 141 |
g_resources_register (libsysprof_get_resource ()); |
| 142 |
|
| 143 |
if (!(bytes = g_resources_lookup_data ("/org/gnome/libsysprof/categories.txt", 0, NULL))) |
| 144 |
return NULL; |
| 145 |
|
| 146 |
if (!(str = (const char *)g_bytes_get_data (bytes, &len))) |
| 147 |
return NULL; |
| 148 |
|
| 149 |
line_reader_init (&reader, (char *)str, len); |
| 150 |
|
| 151 |
categories = g_new0 (SysprofCategories, 1); |
| 152 |
categories->binary_nick_to_rules = g_hash_table_new_full (g_str_hash, |
| 153 |
g_str_equal, |
| 154 |
g_free, |
| 155 |
(GDestroyNotify)g_array_unref); |
| 156 |
|
| 157 |
while ((lineptr = line_reader_next (&reader, &line_len))) |
| 158 |
{ |
| 159 |
g_autofree char *line = g_strndup (lineptr, line_len); |
| 160 |
SysprofCallgraphCategory category_value; |
| 161 |
g_auto(GStrv) parts = NULL; |
| 162 |
const char *match = NULL; |
| 163 |
const char *category = NULL; |
| 164 |
const char *inherit = NULL; |
| 165 |
|
| 166 |
lineno++; |
| 167 |
g_strstrip (line); |
| 168 |
|
| 169 |
/* # starts comment line */ |
| 170 |
if (line[0] == 0 || line[0] == '#') |
| 171 |
continue; |
| 172 |
|
| 173 |
/* Group lines look like "binary nick:\n" */ |
| 174 |
if (g_str_has_suffix (line, ":")) |
| 175 |
{ |
| 176 |
line[line_len-1] = 0; |
| 177 |
g_set_str (&binary_nick, g_strstrip (line)); |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
parts = g_strsplit (line, " ", 0); |
| 182 |
|
| 183 |
for (guint i = 0; parts[i]; i++) |
| 184 |
{ |
| 185 |
g_strstrip (parts[i]); |
| 186 |
|
| 187 |
if (parts[i][0] == 0) |
| 188 |
continue; |
| 189 |
|
| 190 |
if (match == NULL) |
| 191 |
{ |
| 192 |
match = parts[i]; |
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
if (category == NULL) |
| 197 |
{ |
| 198 |
category = parts[i]; |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
if (inherit == NULL) |
| 203 |
{ |
| 204 |
inherit = parts[i]; |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
if (match == NULL || category == NULL) |
| 212 |
{ |
| 213 |
g_warning ("categories.txt: line: %d: Incomplete rule", lineno); |
| 214 |
continue; |
| 215 |
} |
| 216 |
|
| 217 |
if (inherit && !g_str_equal (inherit, "inherit")) |
| 218 |
{ |
| 219 |
g_warning ("categories.txt line %d: malformated inherit", lineno); |
| 220 |
continue; |
| 221 |
} |
| 222 |
|
| 223 |
if (!(category_value = parse_category (category))) |
| 224 |
{ |
| 225 |
g_warning ("categories.txt line %d: malformated category", lineno); |
| 226 |
continue; |
| 227 |
} |
| 228 |
|
| 229 |
sysprof_categories_add_rule (categories, binary_nick, match, category_value, !!inherit); |
| 230 |
} |
| 231 |
|
| 232 |
return categories; |
| 233 |
} |
| 234 |
|
| 235 |
void |
| 236 |
sysprof_categories_free (SysprofCategories *categories) |
| 237 |
{ |
| 238 |
if (categories != NULL) |
| 239 |
{ |
| 240 |
g_clear_pointer (&categories->binary_nick_to_rules, g_hash_table_unref); |
| 241 |
g_free (categories); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
SysprofCallgraphCategory |
| 246 |
sysprof_categories_lookup (SysprofCategories *categories, |
| 247 |
const char *binary_nick, |
| 248 |
const char *symbol) |
| 249 |
{ |
| 250 |
GArray *rules; |
| 251 |
|
| 252 |
if (categories == NULL) |
| 253 |
categories = sysprof_categories_get_default (); |
| 254 |
|
| 255 |
if (binary_nick == NULL || symbol == NULL) |
| 256 |
return 0; |
| 257 |
|
| 258 |
if (!(rules = g_hash_table_lookup (categories->binary_nick_to_rules, binary_nick))) |
| 259 |
return 0; |
| 260 |
|
| 261 |
for (guint i = 0; i < rules->len; i++) |
| 262 |
{ |
| 263 |
const Rule *rule = &g_array_index (rules, Rule, i); |
| 264 |
gboolean ret; |
| 265 |
|
| 266 |
if (rule->kind == MATCH_EXACT) |
| 267 |
ret = strcmp (rule->match, symbol) == 0; |
| 268 |
else if (rule->kind == MATCH_PREFIX) |
| 269 |
ret = g_str_has_prefix (symbol, rule->match); |
| 270 |
else if (rule->kind == MATCH_SUFFIX) |
| 271 |
ret = g_str_has_suffix (symbol, rule->match); |
| 272 |
else |
| 273 |
ret = FALSE; |
| 274 |
|
| 275 |
if (ret) |
| 276 |
{ |
| 277 |
if (rule->inherit) |
| 278 |
return rule->category | SYSPROF_CALLGRAPH_CATEGORY_INHERIT; |
| 279 |
|
| 280 |
return rule->category; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
SysprofCategories * |
| 288 |
sysprof_categories_get_default (void) |
| 289 |
{ |
| 290 |
static SysprofCategories *instance; |
| 291 |
|
| 292 |
if (instance == NULL) |
| 293 |
instance = sysprof_categories_new (); |
| 294 |
|
| 295 |
return instance; |
| 296 |
} |