1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
|
/*
* Oracle Linux DTrace.
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*
* Provider support code for tracepoint-based probes.
*/
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include "dt_bpf.h"
#include "dt_provider_tp.h"
#include "dt_probe.h"
#include "dt_impl.h"
/*
* Tracepoint-specific probe data. This is allocated for every tracepoint
* based probe. Since 0 is not a valid tracepoint event id, and given that BTF
* id 0 refers to 'void', this value is used to denote that no association with
* a tracepoint or BTF type exists yet.
*/
struct tp_probe {
uint32_t id; /* tracepoint event id or BTF id */
int fd; /* tracepoint perf event fd */
};
/*
* Allocate tracepoint-specific probe data.
*/
tp_probe_t *
dt_tp_alloc(dtrace_hdl_t *dtp)
{
tp_probe_t *tpp;
tpp = dt_zalloc(dtp, sizeof(tp_probe_t));
if (tpp == NULL)
return NULL;
tpp->id = 0;
tpp->fd = -1;
return tpp;
}
/*
* Attach the given (loaded) BPF program to the given tracepoint probe. This
* function performs the necessary steps for attaching the BPF program to a
* tracepoint based probe by opening a perf event for the tracepoint, and
* associating the BPF program with the perf event.
*/
int
dt_tp_attach(dtrace_hdl_t *dtp, tp_probe_t *tpp, int bpf_fd)
{
if (tpp->id == 0)
return 0;
if (tpp->fd == -1) {
int fd;
struct perf_event_attr attr = { 0, };
attr.type = PERF_TYPE_TRACEPOINT;
attr.size = sizeof(attr);
attr.sample_period = 1;
attr.wakeup_events = 1;
attr.config = tpp->id;
fd = dt_perf_event_open(&attr, -1, 0, -1, 0);
if (fd < 0)
return fd;
tpp->fd = fd;
}
if (ioctl(tpp->fd, PERF_EVENT_IOC_SET_BPF, bpf_fd) < 0)
return -errno;
return 0;
}
/*
* Attach the given (loaded) BPF program to the given raw tracepoint. The
* raw tracepoint is identified by name (tpp->id is UINT_MAX) or BTF id
* (tpp->id).
*/
int
dt_tp_attach_raw(dtrace_hdl_t *dtp, tp_probe_t *tpp, const char *name,
int bpf_fd)
{
if (tpp->fd == -1) {
int fd;
fd = dt_bpf_raw_tracepoint_open(
tpp->id == UINT_MAX ? name : NULL,
bpf_fd);
if (fd < 0)
return -errno;
tpp->fd = fd;
}
return 0;
}
/*
* Parse a EVENTSFS/<group>/<event>/format file to determine the event id and
* the argument types.
*
* The event id is easy enough to parse out, because it appears on a line in
* the following format:
* ID: <num>
*
* The argument types are a bit more complicated. The basic format for each
* argument is:
* field:<var-decl>; offset:<num> size:<num> signed:(0|1);
* The <var-decl> may be prefixed by __data_loc, which is a tag that we can
* ignore. The <var-decl> does contain an identifier name that dtrace cannot
* cope with because it expects just the type specification. Getting rid of
* the identifier isn't as easy because it may be suffixed by one or more
* array dimension specifiers (and those are part of the type).
*
* All events include a number of common fields that we are not interested
* in and that need to be skipped. Callers of this function can specify a
* callback function (valid_arg) to validate a non-common field.
*/
int
dt_tp_event_info(dtrace_hdl_t *dtp, FILE *f, dt_valid_arg_f *valid_arg,
tp_probe_t *tpp, int *argcp, dt_argdesc_t **argvp)
{
char *buf = NULL;
size_t bufsz;
int idx = 0, argc = 0, skip = 0, common = 1;
dt_argdesc_t *argv = NULL;
tpp->id = 0;
/*
* Pass 1:
* Determine the event id and the number of arguments.
* We will skip initial "common fields".
*/
while (getline(&buf, &bufsz, f) >= 0) {
char *p = buf;
if (sscanf(buf, "ID: %u\n", &tpp->id) == 1)
continue;
if (sscanf(buf, " field:%[^;]", p) <= 0)
continue;
/*
* If we have only seen common fields to date, keep
* looking for a non-common field.
*/
if (common == 1) {
char *s = p + strlen(p) - 1;
/*
* Strip off any [] array size specifications at the end.
*/
while (*s == ']') {
/* From ']' hunt back to '['. They are not nested. */
while (s > p && *(--s) != '[') ;
/* Then remove any spaces. */
while (s > p && *(--s) == ' ') ;
}
*(++s) = '\0';
/*
* Go to the beginning of the identifier.
*/
p = strrchr(p, ' ');
if (p == NULL)
return -EINVAL;
p++;
/*
* Check if it is a common field.
*
* In kernel source file kernel/trace/trace_events.c
* in trace_define_common_fields(), the macro
* __common_field() is used to define common fields,
* prepending names with "common_".
*/
if (strncmp(p, "common_", 7) == 0) {
/*
* For Pass 2, we will not bother checking
* for "common" fields; we will just pretend
* the caller asked us to skip more arguments.
*/
skip++;
continue;
}
/*
* No more need to check for common fields.
*/
common = 0;
}
/*
* We found a non-common "field:" description.
*
* If the caller provided a validation hook, call it with the
* current non-common field counter and the description text.
* The hook returns
* -1 if the field is not a valid argument, and no additional
* arguments can follow
* 0 if the field should be skipped
* 1 if the field is a valid argument
*/
idx++;
if (valid_arg != NULL) {
int rc = valid_arg(idx - 1, p);
if (rc == -1)
break;
if (rc == 0) {
skip++;
continue;
}
}
sscanf(p, "__data_loc %[^;]", p);
argc++;
}
free(buf);
buf = NULL;
/*
* If we saw less fields than expected, we flag an error.
* If we are not interested in arguments, we are done.
* If there are no arguments, we are done.
*/
if (argc < 0)
return -EINVAL;
if (argcp == NULL || argvp == NULL)
return 0;
if (argc == 0)
goto done;
argv = dt_calloc(dtp, argc, sizeof(dt_argdesc_t));
if (!argv)
return -ENOMEM;
/*
* Pass 2:
* Fill in the actual argument datatype strings.
*/
rewind(f);
idx = -skip;
while (getline(&buf, &bufsz, f) >= 0) {
char *p;
size_t l;
size_t size = 0;
char tstr[DT_TYPE_NAMELEN];
char *strp;
p = strstr(buf, "size:");
if (p != NULL)
size = strtol(p + 5, NULL, 10);
p = buf;
if (sscanf(buf, " field:%[^;]", p) <= 0)
continue;
/* We found a field: description - see if we should skip it. */
if (idx < 0)
goto skip;
sscanf(p, "__data_loc %[^;]", p);
/*
* If the last character is not ']', the last token must be the
* identifier name. Get rid of it.
*/
l = strlen(p);
if (p[l - 1] != ']') {
char *q;
if ((q = strrchr(p, ' ')))
*q = '\0';
l = q - p;
strp = p;
} else {
char *s, *q;
int alpha = 0;
/*
* The identifier is followed by at least one array
* size specification. Find the beginning of the
* sequence of (one or more) array size specifications.
* We also skip any spaces in front of [ characters.
*/
s = p + l - 1;
for (;;) {
while (*(--s) != '[') {
if (!isdigit(*s))
alpha = 1;
}
while (*(--s) == ' ') ;
if (*s != ']')
break;
}
/*
* Insert a \0 byte right before the array size
* specifications. The \0 byte overwrites the last
* character of the identifier which is fine because we
* know that the identifier is at least one character
* long.
*/
*(s++) = '\0';
if ((q = strrchr(p, ' ')))
*q = '\0';
strp = tstr;
if (alpha) {
ctf_file_t *ctfp = dtp->dt_shared_ctf;
ctf_id_t type;
size_t esize = 1;
type = ctf_lookup_by_name(ctfp, p);
if (type != CTF_ERR)
esize = ctf_type_size(ctfp, type);
if (esize != 0)
size /= esize;
l = snprintf(strp, q - p + strlen(s), "%s[%lu]",
p, size);
} else {
l = q - p;
memcpy(strp, p, l);
memcpy(strp + l, s, strlen(s) + 1);
}
}
argv[idx].mapping = idx;
argv[idx].flags = 0;
argv[idx].native = strdup(strp);
argv[idx].xlate = NULL;
skip:
idx++;
if (idx == argc)
break;
}
done:
free(buf);
*argcp = argc;
*argvp = argv;
return 0;
}
/*
* Return whether event info for a tracepoint is valid.
*/
int
dt_tp_has_info(const tp_probe_t *tpp)
{
return tpp->id > 0;
}
/*
* Detach from a tracepoint for a tracepoint-based probe. The caller should
* still call dt_tp_destroy() to free the tracepoint-specific probe data.
*/
void
dt_tp_detach(dtrace_hdl_t *dtp, tp_probe_t *tpp)
{
tpp->id = 0;
if (tpp->fd != -1) {
close(tpp->fd);
tpp->fd = -1;
}
}
/*
* Clean up the tracepoint-specific data for a probe. This may be called with
* tracepoint-specific data that has not been attached to a probe (e.g. if the
* creation of the actual probe failed).
*/
void
dt_tp_destroy(dtrace_hdl_t *dtp, tp_probe_t *tpp)
{
dt_free(dtp, tpp);
}
/*
* Return the (event or BTF) id for the tracepoint.
*/
uint32_t
dt_tp_get_id(const tp_probe_t *tpp)
{
return tpp->id;
}
/*
* Set the (event or BTF) id for the tracepoint.
*/
void
dt_tp_set_id(tp_probe_t *tpp, uint32_t id)
{
tpp->id = id;
}
/*
* Create a tracepoint-based probe. This function is called from any provider
* that handles tracepoint-based probes. It allocates tracepoint-specific data
* for the probe, and adds the probe to the framework.
*/
dt_probe_t *
dt_tp_probe_insert(dtrace_hdl_t *dtp, dt_provider_t *prov, const char *prv,
const char *mod, const char *fun, const char *prb)
{
tp_probe_t *tpp;
tpp = dt_tp_alloc(dtp);
if (tpp == NULL)
return NULL;
return dt_probe_insert(dtp, prov, prv, mod, fun, prb, tpp);
}
/*
* Parse a EVENTSFS/<group>/<event>/format file to determine the event id and
* the argument types for a given probe.
*/
int
dt_tp_probe_info(dtrace_hdl_t *dtp, FILE *f, dt_valid_arg_f *valid_arg,
const dt_probe_t *prp, int *argcp, dt_argdesc_t **argvp)
{
tp_probe_t *tpp = prp->prv_data;
return dt_tp_event_info(dtp, f, valid_arg, tpp, argcp, argvp);
}
/*
* Return whether a tracepoint has been associated with this probe.
*/
int
dt_tp_probe_has_info(const dt_probe_t *prp)
{
tp_probe_t *tpp = prp->prv_data;
return dt_tp_has_info(tpp);
}
/*
* Convenience function for basic tracepoint-based probe attach.
*/
int
dt_tp_probe_attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
{
return dt_tp_attach(dtp, prp->prv_data, bpf_fd);
}
/*
* Convenience function for raw tracepoint-based probe attach.
*/
int
dt_tp_probe_attach_raw(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
{
return dt_tp_attach_raw(dtp, prp->prv_data, prp->desc->prb, bpf_fd);
}
/*
* Convenience function for basic tracepoint-based probe detach.
*/
void
dt_tp_probe_detach(dtrace_hdl_t *dtp, const dt_probe_t *prp)
{
dt_tp_detach(dtp, prp->prv_data);
}
/*
* Convenience function for probe cleanup for tracepoint-based probes.
*/
void
dt_tp_probe_destroy(dtrace_hdl_t *dtp, void *datap)
{
dt_tp_destroy(dtp, datap);
}
/*
* Convenience function to get the (event or BTF) id for the tracepoint of a
* probe.
*/
uint32_t
dt_tp_probe_get_id(const dt_probe_t *prp)
{
return dt_tp_get_id(prp->prv_data);
}
/*
* Convenience function to set the (event or BTF) id for the tracepoint of a
* probe.
*/
void
dt_tp_probe_set_id(const dt_probe_t *prp, uint32_t id)
{
dt_tp_set_id(prp->prv_data, id);
}
|