Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
0004  */
0005 #include <stdio.h>
0006 #include <stdlib.h>
0007 #include <string.h>
0008 #include <errno.h>
0009 
0010 #include "debug.h"
0011 #include "trace-event.h"
0012 
0013 #include <linux/ctype.h>
0014 
0015 static int get_common_field(struct scripting_context *context,
0016                 int *offset, int *size, const char *type)
0017 {
0018     struct tep_handle *pevent = context->pevent;
0019     struct tep_event *event;
0020     struct tep_format_field *field;
0021 
0022     if (!*size) {
0023 
0024         event = tep_get_first_event(pevent);
0025         if (!event)
0026             return 0;
0027 
0028         field = tep_find_common_field(event, type);
0029         if (!field)
0030             return 0;
0031         *offset = field->offset;
0032         *size = field->size;
0033     }
0034 
0035     return tep_read_number(pevent, context->event_data + *offset, *size);
0036 }
0037 
0038 int common_lock_depth(struct scripting_context *context)
0039 {
0040     static int offset;
0041     static int size;
0042     int ret;
0043 
0044     ret = get_common_field(context, &size, &offset,
0045                    "common_lock_depth");
0046     if (ret < 0)
0047         return -1;
0048 
0049     return ret;
0050 }
0051 
0052 int common_flags(struct scripting_context *context)
0053 {
0054     static int offset;
0055     static int size;
0056     int ret;
0057 
0058     ret = get_common_field(context, &size, &offset,
0059                    "common_flags");
0060     if (ret < 0)
0061         return -1;
0062 
0063     return ret;
0064 }
0065 
0066 int common_pc(struct scripting_context *context)
0067 {
0068     static int offset;
0069     static int size;
0070     int ret;
0071 
0072     ret = get_common_field(context, &size, &offset,
0073                    "common_preempt_count");
0074     if (ret < 0)
0075         return -1;
0076 
0077     return ret;
0078 }
0079 
0080 unsigned long long
0081 raw_field_value(struct tep_event *event, const char *name, void *data)
0082 {
0083     struct tep_format_field *field;
0084     unsigned long long val;
0085 
0086     field = tep_find_any_field(event, name);
0087     if (!field)
0088         return 0ULL;
0089 
0090     tep_read_number_field(field, data, &val);
0091 
0092     return val;
0093 }
0094 
0095 unsigned long long read_size(struct tep_event *event, void *ptr, int size)
0096 {
0097     return tep_read_number(event->tep, ptr, size);
0098 }
0099 
0100 void event_format__fprintf(struct tep_event *event,
0101                int cpu, void *data, int size, FILE *fp)
0102 {
0103     struct tep_record record;
0104     struct trace_seq s;
0105 
0106     memset(&record, 0, sizeof(record));
0107     record.cpu = cpu;
0108     record.size = size;
0109     record.data = data;
0110 
0111     trace_seq_init(&s);
0112     tep_print_event(event->tep, &s, &record, "%s", TEP_PRINT_INFO);
0113     trace_seq_do_fprintf(&s, fp);
0114     trace_seq_destroy(&s);
0115 }
0116 
0117 void event_format__print(struct tep_event *event,
0118              int cpu, void *data, int size)
0119 {
0120     return event_format__fprintf(event, cpu, data, size, stdout);
0121 }
0122 
0123 void parse_ftrace_printk(struct tep_handle *pevent,
0124              char *file, unsigned int size __maybe_unused)
0125 {
0126     unsigned long long addr;
0127     char *printk;
0128     char *line;
0129     char *next = NULL;
0130     char *addr_str;
0131     char *fmt = NULL;
0132 
0133     line = strtok_r(file, "\n", &next);
0134     while (line) {
0135         addr_str = strtok_r(line, ":", &fmt);
0136         if (!addr_str) {
0137             pr_warning("printk format with empty entry");
0138             break;
0139         }
0140         addr = strtoull(addr_str, NULL, 16);
0141         /* fmt still has a space, skip it */
0142         printk = strdup(fmt+1);
0143         line = strtok_r(NULL, "\n", &next);
0144         tep_register_print_string(pevent, printk, addr);
0145         free(printk);
0146     }
0147 }
0148 
0149 void parse_saved_cmdline(struct tep_handle *pevent,
0150              char *file, unsigned int size __maybe_unused)
0151 {
0152     char comm[17]; /* Max comm length in the kernel is 16. */
0153     char *line;
0154     char *next = NULL;
0155     int pid;
0156 
0157     line = strtok_r(file, "\n", &next);
0158     while (line) {
0159         if (sscanf(line, "%d %16s", &pid, comm) == 2)
0160             tep_register_comm(pevent, comm, pid);
0161         line = strtok_r(NULL, "\n", &next);
0162     }
0163 }
0164 
0165 int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
0166 {
0167     return tep_parse_event(pevent, buf, size, "ftrace");
0168 }
0169 
0170 int parse_event_file(struct tep_handle *pevent,
0171              char *buf, unsigned long size, char *sys)
0172 {
0173     return tep_parse_event(pevent, buf, size, sys);
0174 }
0175 
0176 struct flag {
0177     const char *name;
0178     unsigned long long value;
0179 };
0180 
0181 static const struct flag flags[] = {
0182     { "HI_SOFTIRQ", 0 },
0183     { "TIMER_SOFTIRQ", 1 },
0184     { "NET_TX_SOFTIRQ", 2 },
0185     { "NET_RX_SOFTIRQ", 3 },
0186     { "BLOCK_SOFTIRQ", 4 },
0187     { "IRQ_POLL_SOFTIRQ", 5 },
0188     { "TASKLET_SOFTIRQ", 6 },
0189     { "SCHED_SOFTIRQ", 7 },
0190     { "HRTIMER_SOFTIRQ", 8 },
0191     { "RCU_SOFTIRQ", 9 },
0192 
0193     { "HRTIMER_NORESTART", 0 },
0194     { "HRTIMER_RESTART", 1 },
0195 };
0196 
0197 unsigned long long eval_flag(const char *flag)
0198 {
0199     int i;
0200 
0201     /*
0202      * Some flags in the format files do not get converted.
0203      * If the flag is not numeric, see if it is something that
0204      * we already know about.
0205      */
0206     if (isdigit(flag[0]))
0207         return strtoull(flag, NULL, 0);
0208 
0209     for (i = 0; i < (int)(ARRAY_SIZE(flags)); i++)
0210         if (strcmp(flags[i].name, flag) == 0)
0211             return flags[i].value;
0212 
0213     return 0;
0214 }