/usr/share/systemtap/tapset/linux/json.stp is in systemtap-common 3.1-3ubuntu0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // JSON tapset.
// Copyright (C) 2015 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
//
// <tapsetdescription>
// The JSON tapset provides probes, functions, and macros to generate
// a JSON metadata and data file. The JSON metadata file is located in
// /proc/systemtap/MODULE/metadata.json. The JSON data file is located
// in /proc/systemtap/MODULE/data.json. The JSON data file is updated
// with current data every time the file is read.
// </tapsetdescription>
@__private30 global __json_prefix
@__private30 global __json_metric_type, __json_metric_desc
@__private30 global __json_metric_units, __json_metric_pointer
@__private30 global __json_array_metric_type, __json_array_metric_desc
@__private30 global __json_array_metric_units, __json_array_metric_pointer
global __json_metric_output, __json_array_output
probe init
{
// Make sure the systemtap translator knows what types the globals are.
__json_prefix = ""
__json_metric_units[""] = ""
delete __json_metric_units
__json_array_metric_type["", ""] = ""
delete __json_array_metric_type
__json_array_metric_desc["", ""] = ""
delete __json_array_metric_desc
__json_array_metric_units["", ""] = ""
delete __json_array_metric_units
__json_array_metric_pointer["", ""] = ""
delete __json_array_metric_pointer
__json_array_output[""] = 1
delete __json_array_output
}
/**
* sfunction json_set_prefix - Set the metric prefix.
*
* @prefix: The prefix name to be used.
*
* Description: This function sets the "prefix", which is the name
* of the base of the metric hierarchy. Calling this function is
* optional, by default the name of the systemtap module is used.
*/
function json_set_prefix:long(prefix:string)
{
__json_prefix = prefix
}
/**
* sfunction json_add_numeric_metric - Add a numeric metric
*
* @name: The name of the numeric metric.
* @description: Metric description. An empty string can be used.
* @units: Metic units. An empty string can be used.
*
* Description: This function adds a numeric metric, setting up
* everything needed.
*/
function json_add_numeric_metric:long(name:string, description:string, units:string)
{
if (name in __json_metric_type)
error(sprintf("Metric '%s' already exists", name))
__json_metric_type[name] = "integer"
__json_metric_desc[name] = description
__json_metric_units[name] = units
# FIXME: Do we need to validate the name? For instance, what if it
# had a double-quote character in it?
__json_metric_pointer[name] = "/" . name
return 0
}
/**
* sfunction json_add_string_metric - Add a string metric
*
* @name: The name of the string metric.
* @description: Metric description. An empty string can be used.
*
* Description: This function adds a string metric, setting up
* everything needed.
*/
function json_add_string_metric:long(name:string, description:string)
{
if (name in __json_metric_type)
error(sprintf("Metric '%s' already exists", name))
__json_metric_type[name] = "string"
__json_metric_desc[name] = description
__json_metric_pointer[name] = "/" . name
return 0
}
/**
* sfunction json_add_array - Add an array
*
* @name: The name of the array.
* @description: Array description. An empty string can be used.
*
* Description: This function adds a array, setting up everything
* needed. Arrays contain other metrics, added with
* json_add_array_numeric_metric() or json_add_array_string_metric().
*/
function json_add_array:long(name:string, description:string)
{
if (name in __json_metric_type)
error(sprintf("Metric '%s' already exists", name))
__json_metric_type[name] = "array"
__json_metric_desc[name] = description
__json_metric_pointer[name] = "/" . name
# Go ahead and add "__id", which is the array index.
json_add_array_string_metric(name, "__id", "")
return 0
}
/**
* sfunction json_add_array_numeric_metric - Add a numeric metric to an array
*
* @array_name: The name of the array the numeric metric should be
* added to.
* @metric_name: The name of the numeric metric.
* @metric_description: Metric description. An empty string can be used.
* @metric_units: Metic units. An empty string can be used.
*
* Description: This function adds a numeric metric to an array,
* setting up everything needed.
*/
function json_add_array_numeric_metric:long(array_name:string, metric_name:string, metric_description:string, metric_units:string)
{
if ([array_name, metric_name] in __json_array_metric_type)
error(sprintf("Array metric '%s' already exists in array %s", metric_name,
array_name))
__json_array_metric_pointer[array_name, metric_name]
= sprintf("/%s", metric_name)
__json_array_metric_type[array_name, metric_name] = "integer"
__json_array_metric_desc[array_name, metric_name] = metric_description
__json_array_metric_units[array_name, metric_name] = metric_units
return 0
}
/**
* sfunction json_add_array_string_metric - Add a string metric to an array
*
* @array_name: The name of the array the string metric should be
* added to.
* @metric_name: The name of the string metric.
* @metric_description: Metric description. An empty string can be used.
*
* Description: This function adds a string metric to an array,
* setting up everything needed.
*/
function json_add_array_string_metric:long(array_name:string, metric_name:string, metric_description:string)
{
if ([array_name, metric_name] in __json_array_metric_type)
error(sprintf("Array metric '%s' already exists in array %s", metric_name,
array_name))
__json_array_metric_pointer[array_name, metric_name]
= sprintf("/%s", metric_name)
__json_array_metric_type[array_name, metric_name] = "string"
__json_array_metric_desc[array_name, metric_name] = metric_description
return 0
}
probe procfs("metadata.json").read.maxsize(8192)
{
# Note: This is the "pretty-printed" version, intended to be read by
# humans. We could remove the whitespace and newlines if we wanted
# to make the output shorter (but less readable).
#
# Note 2: Note that we have to break this long string into more than
# 1 assignment since we're bumping up against MAXSTRINGLEN. Procfs
# $value can hold more than MAXSTRINGLEN because of the
# '.maxsize(N)' parameter.
$value = "{\n"
if (__json_prefix != "")
$value .= sprintf(" \"prefix\": \"%s\",\n", __json_prefix)
$value .= " \"metrics\": [\n"
__comma_needed = 0
foreach (__name in __json_metric_type) {
if (__comma_needed)
$value .= ",\n"
__comma_needed = 1
if (__json_metric_type[__name] != "array") {
@__json_output_metric_metadata(" ", __name,
__json_metric_pointer[__name],
__json_metric_type[__name],
__json_metric_desc[__name],
__json_metric_units[__name])
}
else {
# For an array, we have to output the information about the
# array itself, then output each field in the array.
$value .= " \{\n"
$value .= sprintf(" \"name\": \"%s\",\n", __name)
$value .= sprintf(" \"pointer\": \"%s\",\n",
__json_metric_pointer[__name])
$value .= " \"type\": \"array\",\n"
if (strlen(__json_metric_desc[__name]) > 0)
$value .= sprintf(" \"description\": \"%s\",\n",
__json_metric_desc[__name])
$value .= " \"index\": \"/__id\",\n"
$value .= " \"metrics\": [\n"
__array_comma_needed = 0
# Output each field in the array.
foreach ([__array_name, __metric_name] in __json_array_metric_type) {
if (__name == __array_name) {
# Skip '__id', the array index.
if (__metric_name == "__id")
continue
if (__array_comma_needed)
$value .= ",\n"
__array_comma_needed = 1
__subpointer = __json_array_metric_pointer[__array_name, __metric_name]
__subtype = __json_array_metric_type[__array_name, __metric_name]
__subdesc = __json_array_metric_desc[__array_name, __metric_name]
__subunits = __json_array_metric_units[__array_name, __metric_name]
@__json_output_metric_metadata(" ", __metric_name,
__subpointer, __subtype,
__subdesc, __subunits)
}
}
$value .=
"\n"
" ]\n"
" }"
}
}
$value .=
"\n"
" ]\n"
"}\n"
}
/**
* probe json_data - Fires whenever JSON data is wanted by a reader.
*
* Context:
* This probe fires when the JSON data is about to be read. This
* probe must gather up data and then call the following macros to
* output the data in JSON format. First, @json_output_data_start()
* must be called. That call is followed by one or more of the
* following (one call for each data item):
* @json_output_string_value(), @json_output_numeric_value(),
* @json_output_array_string_value(), and
* @json_output_array_numeric_value(). Finally @json_output_data_end()
* must be called.
*/
probe json_data = procfs("data.json").read.maxsize(8192)
{
}
|