File: viewinfo.cpp

package info (click to toggle)
falcosecurity-libs 0.1.1dev%2Bgit20220316.e5c53d64-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,732 kB
  • sloc: cpp: 55,770; ansic: 37,330; makefile: 74; sh: 13
file content (403 lines) | stat: -rw-r--r-- 8,244 bytes parent folder | download
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
/*
Copyright (C) 2021 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
#include <algorithm>
#include "sinsp.h"
#include "sinsp_int.h"
#include "sinsp_errno.h"
#include "sinsp_signal.h"
#include "filter.h"
#include "filterchecks.h"
#include "protodecoder.h"

///////////////////////////////////////////////////////////////////////////////
// sinsp_view_column_info implementation
///////////////////////////////////////////////////////////////////////////////
string sinsp_view_column_info::get_field(uint32_t depth)
{
	// Trim the string
	replace_in_place(m_field, " ", "");
	replace_in_place(m_field, "\t", "");

	if(m_field.find("%depth-1") != string::npos)
	{
		string res = m_field;
		replace_in_place(res, "%depth-1", to_string(depth - 1));
		return res;
	}
	else if(m_field.find("%depth+1") != string::npos)
	{
		string res = m_field;
		replace_in_place(res, "%depth+1", to_string(depth - 1));
		return res;
	}
	else if(m_field.find("%depth") != string::npos)
	{
		string res = m_field;
		replace_in_place(res, "%depth", to_string(depth));
		return res;
	}
	else
	{
		return m_field;
	}
}

string sinsp_view_column_info::get_filter_field(uint32_t depth)
{
	//
	// If m_filterfield, return it as an override to m_field
	//
	if(m_filterfield != "")
	{
		return m_filterfield;
	}
	else
	{
		return get_field(depth);
	}
}

///////////////////////////////////////////////////////////////////////////////
// sinsp_view_info implementation
///////////////////////////////////////////////////////////////////////////////
sinsp_view_info::sinsp_view_info()
{
	m_valid = false;
}

sinsp_view_info::sinsp_view_info(viewtype type, 
	string id,
	string name,
	string description,
	vector<string> tags,
	vector<string> tips,
	vector<sinsp_view_column_info> columns,
	vector<string> applies_to,
	string filter,
	string drilldown_target,
	bool use_defaults,
	bool is_root,
	vector<sinsp_view_action_info> actions,
	bool drilldown_increase_depth,
	string spectro_type,
	bool propagate_filter)
{
	m_id = id;
	m_name = name;
	m_description = description;
	m_does_groupby = false;
	m_type = type;
	m_tags = tags;	
	m_tips = tips;
	m_columns = columns;
	m_drilldown_target = drilldown_target;
	m_is_root = is_root;
	m_applies_to = applies_to;
	m_drilldown_increase_depth = drilldown_increase_depth;
	m_spectro_type = spectro_type;
	m_propagate_filter = propagate_filter;

	m_use_defaults = use_defaults;
	
	//
	// Make sure the keys go at the beginning
	//
	move_key_to_front(TEF_IS_GROUPBY_KEY);
	move_key_to_front(TEF_IS_KEY);

	//
	// Determine the sorting and grouping columns
	//
	set_sorting_col();

	m_filter = filter;
	m_valid = true;
	m_actions = actions;
	// init the array for hotkeys for sorting columns
	set_col_sorting_hotkeys();
}

void sinsp_view_info::set_col_sorting_hotkeys()
{
	const char shift_number_keys [] = {'!', '@', '#', '$', '%', '^', '&', '*', '('};
	uint32_t size = sizeof(shift_number_keys) / sizeof(shift_number_keys[0]);
	for(uint32_t i=0; i<size; i++) 
	{
		m_col_sort_hotkeys.push_back(shift_number_keys[i]); 
	}
	max_col_sort_hotkeys = m_col_sort_hotkeys.size();
}

void sinsp_view_info::set_sorting_col()
{
	m_n_sorting_cols = 0;

	for(uint32_t j = 0; j < m_columns.size(); j++)
	{
		if((m_columns[j].m_flags & TEF_IS_SORT_COLUMN) != 0)
		{
			m_sortingcol = j;
			m_n_sorting_cols++;
		}

		if((m_columns[j].m_flags & TEF_IS_GROUPBY_KEY) != 0)
		{
			m_does_groupby = true;
		}
	}

	if(m_does_groupby)
	{
		m_sortingcol--;
	}

	if(m_n_sorting_cols == 0)
	{
		if(m_does_groupby)
		{
			m_sortingcol = 2;
		}
		else
		{
			m_sortingcol = 1;
		}
	}
	else if(m_n_sorting_cols > 1)
	{
		throw sinsp_exception("view format error: more than one sorting column");
	}

	if((int64_t)m_sortingcol < 0)
	{
		ASSERT(false);
		throw sinsp_exception("view sorting column configuration error");
	}
}

void sinsp_view_info::apply_tag(string tag)
{
	for(auto it = m_columns.begin(); it != m_columns.end();)
	{
		bool found = false;

		if(it->m_tags.size() != 0)
		{
			for(string t : it->m_tags)
			{
				if(t == tag)
				{
					found = true;
					break;
				}
			}

			if(!found)
			{
				it = m_columns.erase(it);
				continue;
			}
		}

		++it;
	}

	//
	// Make sure to recalculate the sorting and grouping columns, which could change
	// if we remove columns.
	//
	set_sorting_col();
}

void sinsp_view_info::get_col_names_and_sizes(OUT vector<string>* colnames, OUT vector<int32_t>* colsizes)
{
	if(m_type == viewtype::T_LIST)
	{
		colsizes->push_back(-1);
		colnames->push_back("");
	}

	for(auto fit : m_columns)
	{
		if(m_does_groupby)
		{
			if((fit.m_flags & TEF_IS_KEY) != 0)
			{
				continue;
			}

			if((fit.m_flags & TEF_IS_GROUPBY_KEY) != 0)
			{
				colsizes->insert(colsizes->begin(), fit.m_colsize);
				colnames->insert(colnames->begin(), fit.m_name);
				continue;
			}
		}

		colsizes->push_back(fit.m_colsize);
		colnames->push_back(fit.m_name);
	}
}

void sinsp_view_info::move_key_to_front(uint32_t keyflag)
{
	for(uint32_t j = 0; j < m_columns.size(); j++)
	{
		if((m_columns[j].m_flags & keyflag) != 0)
		{
			sinsp_view_column_info ci = m_columns[j];

			m_columns.erase(m_columns.begin() +j);
			m_columns.insert(m_columns.begin(), ci);
			return;
		}
	}
}

sinsp_view_column_info* sinsp_view_info::get_key()
{
	for(uint32_t j = 0; j < m_columns.size(); j++)
	{
		if((m_columns[j].m_flags & TEF_IS_GROUPBY_KEY) != 0)
		{
			return &m_columns[j];
		}
	}

	for(uint32_t j = 0; j < m_columns.size(); j++)
	{
		if((m_columns[j].m_flags & TEF_IS_KEY) != 0)
		{
			return &m_columns[j];
		}
	}

	// The *must* be a key
	return NULL;
}

string sinsp_view_info::get_filter(uint32_t depth)
{
	if(m_filter.find("%depth+1") != string::npos)
	{
		string res = m_filter;
		replace_in_place(res, "%depth+1", to_string(depth + 1));
		replace_in_place(res, "%depth + 1", to_string(depth + 1));
		return res;
	}
	else if(m_filter.find("%depth-1") != string::npos)
	{
		string res = m_filter;
		replace_in_place(res, "%depth-1", to_string(depth - 1));
		replace_in_place(res, "%depth - 1", to_string(depth - 1));
		return res;
	}
	else if(m_filter.find("%depth") != string::npos)
	{
		string res = m_filter;
		replace_in_place(res, "%depth", to_string(depth));
		return res;
	}
	else
	{
		return m_filter;
	}
}

///////////////////////////////////////////////////////////////////////////////
// sinsp_view_manager implementation
///////////////////////////////////////////////////////////////////////////////
void sinsp_view_manager::add(sinsp_view_info* vinfo)
{
	m_views.push_back(*vinfo);
}

typedef struct view_cmp
{
	bool operator()(const sinsp_view_info& src, const sinsp_view_info& dst)
	{
		return src.m_name < dst.m_name;
	}
}table_row_cmp;

void sinsp_view_manager::sort_views()
{
	view_cmp cc;

	//
	// Sort the list alphabetically
	//
	sort(m_views.begin(),
		m_views.end(),
		cc);

	//
	// Print the view list for debugging purposes
	//
	//for(uint32_t j = 0; j < m_views.size(); j++)
	//{
	//	g_logger.format("> %d) %s", j, m_views[j].m_name.c_str());
	//}
}

vector<sinsp_view_info>* sinsp_view_manager::get_views()
{
	sort_views();
	return &m_views;
}

uint32_t sinsp_view_manager::get_selected_view()
{
	sort_views();

	if(m_selected_view_id != "")
	{
		for(uint32_t j = 0; j < m_views.size(); j++)
		{
			if(m_views[j].m_id == m_selected_view_id)
			{
				return j;
			}
		}

		if(m_selected_view_id == "echo")
		{
			return VIEW_ID_SPY;
		}
		else if(m_selected_view_id == "dig")
		{
			return VIEW_ID_DIG;
		}
	}
	else
	{
		for(uint32_t j = 0; j < m_views.size(); j++)
		{
			if(m_views[j].m_is_root)
			{
				return j;
			}
		}
	}

	throw sinsp_exception("view " + m_selected_view_id + " not found");
	return 0;
}

void sinsp_view_manager::set_selected_view(string viewid)
{
	m_selected_view_id = viewid;
}