1*287e80b3SSadaf Ebrahimilibtracefs(3) 2*287e80b3SSadaf Ebrahimi============= 3*287e80b3SSadaf Ebrahimi 4*287e80b3SSadaf EbrahimiNAME 5*287e80b3SSadaf Ebrahimi---- 6*287e80b3SSadaf Ebrahimitracefs_event_get_file, tracefs_event_file_read, tracefs_event_file_write, tracefs_event_file_append, 7*287e80b3SSadaf Ebrahimitracefs_event_file_clear, tracefs_event_file_exists - Work with trace event files. 8*287e80b3SSadaf Ebrahimi 9*287e80b3SSadaf EbrahimiSYNOPSIS 10*287e80b3SSadaf Ebrahimi-------- 11*287e80b3SSadaf Ebrahimi[verse] 12*287e80b3SSadaf Ebrahimi-- 13*287e80b3SSadaf Ebrahimi*#include <tracefs.h>* 14*287e80b3SSadaf Ebrahimi 15*287e80b3SSadaf Ebrahimichar pass:[*]*tracefs_event_get_file*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, 16*287e80b3SSadaf Ebrahimi const char pass:[*]_file_); 17*287e80b3SSadaf Ebrahimichar pass:[*]*tracefs_event_file_read*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, 18*287e80b3SSadaf Ebrahimi const char pass:[*]_file_, int pass:[*]_psize_); 19*287e80b3SSadaf Ebrahimiint *tracefs_event_file_write*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, 20*287e80b3SSadaf Ebrahimi const char pass:[*]_file_, const char pass:[*]_str_); 21*287e80b3SSadaf Ebrahimiint *tracefs_event_file_append*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, 22*287e80b3SSadaf Ebrahimi const char pass:[*]_file_, const char pass:[*]_str_); 23*287e80b3SSadaf Ebrahimiint *tracefs_event_file_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, 24*287e80b3SSadaf Ebrahimi const char pass:[*]_file_); 25*287e80b3SSadaf Ebrahimibool *tracefs_event_file_exists*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, 26*287e80b3SSadaf Ebrahimi const char pass:[*]_file_) 27*287e80b3SSadaf Ebrahimi 28*287e80b3SSadaf Ebrahimi-- 29*287e80b3SSadaf Ebrahimi 30*287e80b3SSadaf EbrahimiDESCRIPTION 31*287e80b3SSadaf Ebrahimi----------- 32*287e80b3SSadaf EbrahimiThese are functions for accessing tracefs event specific files. 33*287e80b3SSadaf EbrahimiThese functions act similar to the tracefs instance file functions 34*287e80b3SSadaf Ebrahimibut are easier to get to if the system and events are known before hand. 35*287e80b3SSadaf Ebrahimi 36*287e80b3SSadaf EbrahimiThe *tracefs_event_get_file()* returns the full path of the _file_ for 37*287e80b3SSadaf Ebrahimithe given _system_ and _event_ that is within the given _instance_. 38*287e80b3SSadaf EbrahimiIf _instance_ is NULL, then the file for the _event_ for the top level 39*287e80b3SSadaf Ebrahimiinstance is returned. Note, there is no check to see if the file actually 40*287e80b3SSadaf Ebrahimiexists or even if the system and event exist. It only creates the path 41*287e80b3SSadaf Ebrahiminame for such an event if it did exist. This acts similar to the 42*287e80b3SSadaf Ebrahimi*tracefs_instance_get_file*(3), but is to be used to get to event files 43*287e80b3SSadaf Ebrahimiif the _system_ and _event_ are already known. 44*287e80b3SSadaf Ebrahimi 45*287e80b3SSadaf EbrahimiThe *tracefs_event_file_read()* reads the content for the _event_ _file_ 46*287e80b3SSadaf Ebrahimifor the given _instance_ or the top level instance if _instance_ is 47*287e80b3SSadaf EbrahimiNULL. The content of the file is returned and _psize_ is set to the amount 48*287e80b3SSadaf Ebrahimiof data that was read. The returned content must be freed with *free*(3). 49*287e80b3SSadaf EbrahimiThis acts similar to the *tracefs_instance_file_read*(3), but is 50*287e80b3SSadaf Ebrahimito be used to read the event file if the _system_ and _event_ are already 51*287e80b3SSadaf Ebrahimiknown. 52*287e80b3SSadaf Ebrahimi 53*287e80b3SSadaf EbrahimiThe *tracefs_event_file_write()* writes _str_ to the _event_ _file_. 54*287e80b3SSadaf EbrahimiIt will truncate anything that is already in that file. 55*287e80b3SSadaf EbrahimiThis acts similar to the *tracefs_instance_file_write*(3), but is 56*287e80b3SSadaf Ebrahimito be used to read the event file if the _system_ and _event_ are already 57*287e80b3SSadaf Ebrahimiknown. 58*287e80b3SSadaf Ebrahimi 59*287e80b3SSadaf EbrahimiThe *tracefs_event_file_append()* appends _str_ to the _event_ _file_. 60*287e80b3SSadaf EbrahimiIt will not clear out the file as it writes _sting_. 61*287e80b3SSadaf EbrahimiThis acts similar to the *tracefs_instance_file_append*(3), but is 62*287e80b3SSadaf Ebrahimito be used to read the event file if the _system_ and _event_ are already 63*287e80b3SSadaf Ebrahimiknown. 64*287e80b3SSadaf Ebrahimi 65*287e80b3SSadaf EbrahimiThe *tracefs_event_file_clear()* clears the content of the _event_ _file_. 66*287e80b3SSadaf EbrahimiThis acts similar to the *tracefs_instance_file_clear*(3), but is 67*287e80b3SSadaf Ebrahimito be used to read the event file if the _system_ and _event_ are already 68*287e80b3SSadaf Ebrahimiknown. 69*287e80b3SSadaf Ebrahimi 70*287e80b3SSadaf EbrahimiThe *tracefs_event_file_exists()* returns true if the _event_ _file_ 71*287e80b3SSadaf Ebrahimiexists, and false otherwise. This acts similar to the *tracefs_instance_file_exists*(3), 72*287e80b3SSadaf Ebrahimibut is to be used to read the event file if the _system_ and _event_ are already 73*287e80b3SSadaf Ebrahimiknown. 74*287e80b3SSadaf Ebrahimi 75*287e80b3SSadaf EbrahimiRETURN VALUE 76*287e80b3SSadaf Ebrahimi------------ 77*287e80b3SSadaf Ebrahimi*tracefs_event_get_file()* returns the path of the given _system_/_event_ _file_ on 78*287e80b3SSadaf Ebrahimisuccess and NULL on error. The return value must be freed with *tracefs_put_tracing_file*(3). 79*287e80b3SSadaf Ebrahimi 80*287e80b3SSadaf Ebrahimi*tracefs_event_file_read()* reads the content of the _system_/_event_ _file_ or 81*287e80b3SSadaf EbrahimiNULL on error. The return pointer must be freed with *free*(3). 82*287e80b3SSadaf Ebrahimi 83*287e80b3SSadaf Ebrahimi*tracefs_event_file_write()* and *tracefs_event_file_append()* returns the number of 84*287e80b3SSadaf Ebrahimibytes written to the _system_/_event_ _file_ or negative on error. 85*287e80b3SSadaf Ebrahimi 86*287e80b3SSadaf Ebrahimi*tracefs_event_file_clear()* returns zero on success and -1 on error. 87*287e80b3SSadaf Ebrahimi 88*287e80b3SSadaf Ebrahimi*tracefs_event_file_exists()* returns true if the _system_/_event_ _file_ exists for 89*287e80b3SSadaf Ebrahimithe given _instance_ (or top level if _instance_ is NULL) or false otherwise. 90*287e80b3SSadaf Ebrahimi 91*287e80b3SSadaf EbrahimiEXAMPLE 92*287e80b3SSadaf Ebrahimi------- 93*287e80b3SSadaf Ebrahimi[source,c] 94*287e80b3SSadaf Ebrahimi-- 95*287e80b3SSadaf Ebrahimi#include <stdio.h> 96*287e80b3SSadaf Ebrahimi#include <stdlib.h> 97*287e80b3SSadaf Ebrahimi#include <unistd.h> 98*287e80b3SSadaf Ebrahimi#include <tracefs.h> 99*287e80b3SSadaf Ebrahimi 100*287e80b3SSadaf Ebrahimiint main(int argc, char **argv) 101*287e80b3SSadaf Ebrahimi{ 102*287e80b3SSadaf Ebrahimi char *system; 103*287e80b3SSadaf Ebrahimi char *event; 104*287e80b3SSadaf Ebrahimi char *file; 105*287e80b3SSadaf Ebrahimi char *cmd = NULL; 106*287e80b3SSadaf Ebrahimi char *buf; 107*287e80b3SSadaf Ebrahimi char *str; 108*287e80b3SSadaf Ebrahimi char ch = 'r'; 109*287e80b3SSadaf Ebrahimi int size; 110*287e80b3SSadaf Ebrahimi 111*287e80b3SSadaf Ebrahimi if (argc < 4) { 112*287e80b3SSadaf Ebrahimi printf("usage: %s sytem event file [(-a|-w) write | -c]\n" 113*287e80b3SSadaf Ebrahimi " reads the system/event file or writes if [write is supplied]\n", 114*287e80b3SSadaf Ebrahimi argv[0]); 115*287e80b3SSadaf Ebrahimi exit(0); 116*287e80b3SSadaf Ebrahimi } 117*287e80b3SSadaf Ebrahimi 118*287e80b3SSadaf Ebrahimi system = argv[1]; 119*287e80b3SSadaf Ebrahimi event = argv[2]; 120*287e80b3SSadaf Ebrahimi file = argv[3]; 121*287e80b3SSadaf Ebrahimi if (argc > 4) 122*287e80b3SSadaf Ebrahimi cmd = argv[4]; 123*287e80b3SSadaf Ebrahimi 124*287e80b3SSadaf Ebrahimi if (!tracefs_event_file_exists(NULL, system, event, file)) { 125*287e80b3SSadaf Ebrahimi fprintf(stderr, "File %s/%s/%s does not exist\n", 126*287e80b3SSadaf Ebrahimi system, event, file); 127*287e80b3SSadaf Ebrahimi exit(-1); 128*287e80b3SSadaf Ebrahimi } 129*287e80b3SSadaf Ebrahimi 130*287e80b3SSadaf Ebrahimi if (cmd) { 131*287e80b3SSadaf Ebrahimi if (cmd[0] != '-') 132*287e80b3SSadaf Ebrahimi ch = cmd[0]; 133*287e80b3SSadaf Ebrahimi else 134*287e80b3SSadaf Ebrahimi ch = cmd[1]; 135*287e80b3SSadaf Ebrahimi if (!ch) 136*287e80b3SSadaf Ebrahimi ch = 'c'; 137*287e80b3SSadaf Ebrahimi } 138*287e80b3SSadaf Ebrahimi 139*287e80b3SSadaf Ebrahimi switch (ch) { 140*287e80b3SSadaf Ebrahimi case 'r': 141*287e80b3SSadaf Ebrahimi buf = tracefs_event_file_read(NULL, system, event, file, &size); 142*287e80b3SSadaf Ebrahimi if (buf) 143*287e80b3SSadaf Ebrahimi printf("%s", buf); 144*287e80b3SSadaf Ebrahimi else 145*287e80b3SSadaf Ebrahimi fprintf(stderr, "Failed to read %s/%s/%s\n", 146*287e80b3SSadaf Ebrahimi system, event, file); 147*287e80b3SSadaf Ebrahimi free(buf); 148*287e80b3SSadaf Ebrahimi break; 149*287e80b3SSadaf Ebrahimi case 'w': 150*287e80b3SSadaf Ebrahimi case 'a': 151*287e80b3SSadaf Ebrahimi if (argc < 6) { 152*287e80b3SSadaf Ebrahimi fprintf(stderr, "%s command requires something to write\n", 153*287e80b3SSadaf Ebrahimi ch == 'w' ? "write" : "append"); 154*287e80b3SSadaf Ebrahimi exit(-1); 155*287e80b3SSadaf Ebrahimi } 156*287e80b3SSadaf Ebrahimi if (ch == 'w') 157*287e80b3SSadaf Ebrahimi size = tracefs_event_file_write(NULL, system, event, file, argv[5]); 158*287e80b3SSadaf Ebrahimi else 159*287e80b3SSadaf Ebrahimi size = tracefs_event_file_append(NULL, system, event, file, argv[5]); 160*287e80b3SSadaf Ebrahimi if (size < 0) { 161*287e80b3SSadaf Ebrahimi fprintf(stderr, "Failed to write '%s' to %s/%s/%s\n", 162*287e80b3SSadaf Ebrahimi argv[5], system, event, file); 163*287e80b3SSadaf Ebrahimi exit(-1); 164*287e80b3SSadaf Ebrahimi } 165*287e80b3SSadaf Ebrahimi break; 166*287e80b3SSadaf Ebrahimi case 'c': 167*287e80b3SSadaf Ebrahimi if (tracefs_event_file_clear(NULL, system, event, file) < 0) { 168*287e80b3SSadaf Ebrahimi fprintf(stderr, "Failed to clear %s/%s/%s\n", 169*287e80b3SSadaf Ebrahimi system, event, file); 170*287e80b3SSadaf Ebrahimi exit(-1); 171*287e80b3SSadaf Ebrahimi } 172*287e80b3SSadaf Ebrahimi break; 173*287e80b3SSadaf Ebrahimi default: 174*287e80b3SSadaf Ebrahimi fprintf(stderr, "Unknown command '%c'\n", ch); 175*287e80b3SSadaf Ebrahimi exit(-1); 176*287e80b3SSadaf Ebrahimi } 177*287e80b3SSadaf Ebrahimi exit(0); 178*287e80b3SSadaf Ebrahimi} 179*287e80b3SSadaf Ebrahimi-- 180*287e80b3SSadaf EbrahimiFILES 181*287e80b3SSadaf Ebrahimi----- 182*287e80b3SSadaf Ebrahimi[verse] 183*287e80b3SSadaf Ebrahimi-- 184*287e80b3SSadaf Ebrahimi*tracefs.h* 185*287e80b3SSadaf Ebrahimi Header file to include in order to have access to the library APIs. 186*287e80b3SSadaf Ebrahimi*-ltracefs* 187*287e80b3SSadaf Ebrahimi Linker switch to add when building a program that uses the library. 188*287e80b3SSadaf Ebrahimi-- 189*287e80b3SSadaf Ebrahimi 190*287e80b3SSadaf EbrahimiSEE ALSO 191*287e80b3SSadaf Ebrahimi-------- 192*287e80b3SSadaf Ebrahimi*libtracefs*(3), 193*287e80b3SSadaf Ebrahimi*libtraceevent*(3), 194*287e80b3SSadaf Ebrahimi*trace-cmd*(1) 195*287e80b3SSadaf Ebrahimi 196*287e80b3SSadaf EbrahimiAUTHOR 197*287e80b3SSadaf Ebrahimi------ 198*287e80b3SSadaf Ebrahimi[verse] 199*287e80b3SSadaf Ebrahimi-- 200*287e80b3SSadaf Ebrahimi*Steven Rostedt* <[email protected]> 201*287e80b3SSadaf Ebrahimi-- 202*287e80b3SSadaf EbrahimiREPORTING BUGS 203*287e80b3SSadaf Ebrahimi-------------- 204*287e80b3SSadaf EbrahimiReport bugs to <[email protected]> 205*287e80b3SSadaf Ebrahimi 206*287e80b3SSadaf EbrahimiLICENSE 207*287e80b3SSadaf Ebrahimi------- 208*287e80b3SSadaf Ebrahimilibtracefs is Free Software licensed under the GNU LGPL 2.1 209*287e80b3SSadaf Ebrahimi 210*287e80b3SSadaf EbrahimiRESOURCES 211*287e80b3SSadaf Ebrahimi--------- 212*287e80b3SSadaf Ebrahimihttps://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ 213*287e80b3SSadaf Ebrahimi 214*287e80b3SSadaf EbrahimiCOPYING 215*287e80b3SSadaf Ebrahimi------- 216*287e80b3SSadaf EbrahimiCopyright \(C) 2022 Google, Inc. Free use of this software is granted under 217*287e80b3SSadaf Ebrahimithe terms of the GNU Public License (GPL). 218