Skip to content

Commit

Permalink
refactor: Use string_view in event filters and more
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Aponte <[email protected]>
  • Loading branch information
federico-sysdig committed Apr 16, 2024
1 parent 03e34ed commit 8ceee69
Show file tree
Hide file tree
Showing 45 changed files with 196 additions and 198 deletions.
1 change: 1 addition & 0 deletions userspace/libsinsp/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#pragma once

#include <cstdint>
#include <string>

class Base64
Expand Down
6 changes: 3 additions & 3 deletions userspace/libsinsp/container_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::vector<std::string> sinsp_container_info::container_health_probe::probe_typ
};

// Initialize container max label length to default 100 value
uint32_t sinsp_container_info::m_container_label_max_length = 100;
uint32_t sinsp_container_info::m_container_label_max_length = 100;

sinsp_container_info::container_health_probe::container_health_probe()
{
Expand Down Expand Up @@ -120,7 +120,7 @@ const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by
return &(m_mounts[idx]);
}

const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_source(std::string &source) const
const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_source(const std::string& source) const
{
// note: linear search
for (auto &mntinfo :m_mounts)
Expand All @@ -134,7 +134,7 @@ const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by
return NULL;
}

const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_dest(std::string &dest) const
const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_dest(const std::string& dest) const
{
// note: linear search
for (auto &mntinfo :m_mounts)
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/container_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ class sinsp_container_info
const std::vector<std::string>& get_env() const { return m_env; }

const container_mount_info *mount_by_idx(uint32_t idx) const;
const container_mount_info *mount_by_source(std::string &source) const;
const container_mount_info *mount_by_dest(std::string &dest) const;
const container_mount_info *mount_by_source(const std::string&) const;
const container_mount_info *mount_by_dest(const std::string&) const;

bool is_pod_sandbox() const {
return m_is_pod_sandbox;
Expand Down
15 changes: 4 additions & 11 deletions userspace/libsinsp/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,7 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s
return &m_paramstr_storage[0];
}

std::string sinsp_evt::get_param_value_str(const std::string &name, bool resolved)
std::string sinsp_evt::get_param_value_str(std::string_view name, bool resolved)
{
for(uint32_t i = 0; i < get_num_params(); i++)
{
Expand All @@ -1834,14 +1834,7 @@ std::string sinsp_evt::get_param_value_str(const std::string &name, bool resolve
}
}

return std::string("");
}

std::string sinsp_evt::get_param_value_str(const char *name, bool resolved)
{
// TODO fix this !!
std::string s_name = std::string(name);
return get_param_value_str(s_name, resolved);
return std::string();
}

std::string sinsp_evt::get_param_value_str(uint32_t i, bool resolved)
Expand All @@ -1860,11 +1853,11 @@ std::string sinsp_evt::get_param_value_str(uint32_t i, bool resolved)
}
}

const char* sinsp_evt::get_param_value_str(const char* name, OUT const char** resolved_str, param_fmt fmt)
const char* sinsp_evt::get_param_value_str(std::string_view name, OUT const char** resolved_str, param_fmt fmt)
{
for(uint32_t i = 0; i < get_num_params(); i++)
{
if(strcmp(name, get_param_name(i)) == 0)
if(name == get_param_name(i))
{
return get_param_as_str(i, resolved_str, fmt);
}
Expand Down
16 changes: 8 additions & 8 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ class SINSP_PUBLIC sinsp_evt
before returning it. For example, and FD number will be converted into
the correspondent file, TCP tuple, etc.
*/
std::string get_param_value_str(const std::string& name, bool resolved = true);
std::string get_param_value_str(std::string_view name, bool resolved = true);

/*!
\brief Return the event's category, based on the event type and the FD on
Expand Down Expand Up @@ -515,7 +515,7 @@ class SINSP_PUBLIC sinsp_evt

const char* get_param_as_str(uint32_t id, OUT const char** resolved_str, param_fmt fmt = PF_NORMAL);

const char* get_param_value_str(const char* name, OUT const char** resolved_str, param_fmt fmt = PF_NORMAL);
const char* get_param_value_str(std::string_view name, OUT const char** resolved_str, param_fmt fmt = PF_NORMAL);

inline void init_keep_threadinfo()
{
Expand Down Expand Up @@ -661,8 +661,8 @@ class SINSP_PUBLIC sinsp_evt
m_params.emplace_back(this, j, static_cast<const char*>(params[j].buf), params[j].size);
}
}

std::string get_param_value_str(uint32_t id, bool resolved);
std::string get_param_value_str(const char* name, bool resolved = true);
char* render_fd(int64_t fd, const char** resolved_str, sinsp_evt::param_fmt fmt);
int render_fd_json(Json::Value *ret, int64_t fd, const char** resolved_str, sinsp_evt::param_fmt fmt);
inline uint32_t get_dump_flags() const { return m_dump_flags; }
Expand Down Expand Up @@ -690,7 +690,7 @@ class SINSP_PUBLIC sinsp_evt
{
m_pevt = v;
}

inline const char* get_scap_evt_storage() const
{
return m_pevt_storage;
Expand All @@ -705,7 +705,7 @@ class SINSP_PUBLIC sinsp_evt
{
m_pevt_storage = v;
}

inline uint32_t get_flags() const
{
return m_flags;
Expand All @@ -720,12 +720,12 @@ class SINSP_PUBLIC sinsp_evt
{
return m_rawbuf_str_len;
}

inline void set_rawbuf_str_len(int32_t v)
{
m_rawbuf_str_len = v;
}

inline void set_filtered_out(bool v)
{
m_filtered_out = v;
Expand Down Expand Up @@ -775,7 +775,7 @@ class SINSP_PUBLIC sinsp_evt
{
m_fdinfo_ref = v;
}

inline const std::vector<char>& get_paramstr_storage() const
{
return m_paramstr_storage;
Expand Down
6 changes: 2 additions & 4 deletions userspace/libsinsp/eventformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void sinsp_evt_formatter::set_format(output_format of, const std::string& fmt)
}
}

auto chk = m_available_checks.new_filter_check_from_fldname(std::string(cfmt + j + 1),
auto chk = m_available_checks.new_filter_check_from_fldname(std::string_view(cfmt + j + 1),
m_inspector,
false);

Expand Down Expand Up @@ -343,9 +343,7 @@ std::shared_ptr<sinsp_evt_formatter> sinsp_evt_formatter_factory::create_formatt
return it->second;
}

std::shared_ptr<sinsp_evt_formatter> ret;

ret.reset(new sinsp_evt_formatter(m_inspector, m_available_checks));
auto ret = std::make_shared<sinsp_evt_formatter>(m_inspector, m_available_checks);

ret->set_format(m_output_format, format);
m_formatters[format] = ret;
Expand Down
20 changes: 10 additions & 10 deletions userspace/libsinsp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,25 +312,25 @@ void sinsp_filter_compiler::visit(const libsinsp::filter::ast::unary_check_expr*
auto check = create_filtercheck(field);
check->m_cmpop = str_to_cmpop(e->op);
check->m_boolop = m_last_boolop;
check->parse_field_name(field.c_str(), true, true);
check->parse_field_name(field, true, true);
m_filter->add_check(std::move(check));
}

static void add_filtercheck_value(sinsp_filter_check *chk, size_t idx, const std::string& value)
static void add_filtercheck_value(sinsp_filter_check* chk, size_t idx, std::string_view value)
{
std::vector<char> hex_bytes;
switch(chk->m_cmpop)
{
case CO_BCONTAINS:
case CO_BSTARTSWITH:
if(!sinsp_utils::unhex(std::vector<char>(value.c_str(), value.c_str() + value.size()), hex_bytes))
if(!sinsp_utils::unhex(value, hex_bytes))
{
throw sinsp_exception("filter error: bcontains and bstartswith operator support hex strings only");
}
chk->add_filter_value(&hex_bytes[0], hex_bytes.size(), idx);
break;
default:
chk->add_filter_value(value.c_str(), value.size(), idx);
chk->add_filter_value(value.data(), value.size(), idx);
break;
}
}
Expand All @@ -342,7 +342,7 @@ void sinsp_filter_compiler::visit(const libsinsp::filter::ast::binary_check_expr
auto check = create_filtercheck(field);
check->m_cmpop = str_to_cmpop(e->op);
check->m_boolop = m_last_boolop;
check->parse_field_name(field.c_str(), true, true);
check->parse_field_name(field, true, true);

// Read the the the right-hand values of the filtercheck.
// For list-related operators ('in', 'intersects', 'pmatch'), the vector
Expand Down Expand Up @@ -398,17 +398,17 @@ std::string sinsp_filter_compiler::create_filtercheck_name(const std::string& na
return fld;
}

std::unique_ptr<sinsp_filter_check> sinsp_filter_compiler::create_filtercheck(std::string& field)
std::unique_ptr<sinsp_filter_check> sinsp_filter_compiler::create_filtercheck(std::string_view field)
{
auto chk = m_factory->new_filtercheck(field.c_str());
auto chk = m_factory->new_filtercheck(field);
if(chk == NULL)
{
throw sinsp_exception("filter_check called with nonexistent field " + field);
throw sinsp_exception("filter_check called with nonexistent field " + std::string(field));
}
return chk;
}

cmpop sinsp_filter_compiler::str_to_cmpop(const std::string& str)
cmpop sinsp_filter_compiler::str_to_cmpop(std::string_view str)
{
if(str == "=" || str == "==")
{
Expand Down Expand Up @@ -499,7 +499,7 @@ std::unique_ptr<sinsp_filter> sinsp_filter_factory::new_filter() const
return std::make_unique<sinsp_filter>(m_inspector);
}

std::unique_ptr<sinsp_filter_check> sinsp_filter_factory::new_filtercheck(const char *fldname) const
std::unique_ptr<sinsp_filter_check> sinsp_filter_factory::new_filtercheck(std::string_view fldname) const
{
return m_available_checks.new_filter_check_from_fldname(fldname,
m_inspector,
Expand Down
12 changes: 6 additions & 6 deletions userspace/libsinsp/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class sinsp_filter_expression : public sinsp_filter_check
// The following methods are part of the filter check interface but are irrelevant
// for this class, because they are used only for the leaves of the filtering tree.
//
int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) override
int32_t parse_field_name(std::string_view str, bool alloc_state, bool needed_for_filtering) override
{
return 0;
}
Expand Down Expand Up @@ -164,8 +164,8 @@ class sinsp_filter_factory
virtual ~sinsp_filter_factory() = default;

virtual std::unique_ptr<sinsp_filter> new_filter() const;
virtual std::unique_ptr<sinsp_filter_check> new_filtercheck(const char* fldname) const;

virtual std::unique_ptr<sinsp_filter_check> new_filtercheck(std::string_view fldname) const;

virtual std::list<filter_fieldclass_info> get_fields() const;

Expand Down Expand Up @@ -247,9 +247,9 @@ class SINSP_PUBLIC sinsp_filter_compiler:
void visit(const libsinsp::filter::ast::list_expr*) override;
void visit(const libsinsp::filter::ast::unary_check_expr*) override;
void visit(const libsinsp::filter::ast::binary_check_expr*) override;
cmpop str_to_cmpop(const std::string& str);
cmpop str_to_cmpop(std::string_view str);
std::string create_filtercheck_name(const std::string& name, const std::string& arg);
std::unique_ptr<sinsp_filter_check> create_filtercheck(std::string& field);
std::unique_ptr<sinsp_filter_check> create_filtercheck(std::string_view field);

libsinsp::filter::ast::pos_info m_pos;
bool m_expect_values;
Expand All @@ -263,4 +263,4 @@ class SINSP_PUBLIC sinsp_filter_compiler:
sinsp_filter_check_list m_default_filterlist;
};

/*@}*/
/*@}*/
5 changes: 3 additions & 2 deletions userspace/libsinsp/filter_check_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ void filter_check_list::get_all_fields(std::vector<const filter_check_info*>& li
}

/* Craft a new filter check from the field name */
std::unique_ptr<sinsp_filter_check> filter_check_list::new_filter_check_from_fldname(const std::string& name,
std::unique_ptr<sinsp_filter_check> filter_check_list::new_filter_check_from_fldname(
std::string_view name,
sinsp* inspector,
bool do_exact_check) const
{
for(const auto& chk : m_check_list)
{
chk->m_inspector = inspector;

int32_t fldnamelen = chk->parse_field_name(name.c_str(), false, true);
int32_t fldnamelen = chk->parse_field_name(name, false, true);

if(fldnamelen != -1)
{
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/filter_check_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class filter_check_list

void add_filter_check(std::unique_ptr<sinsp_filter_check> filter_check);
void get_all_fields(std::vector<const filter_check_info*>&) const;
std::unique_ptr<sinsp_filter_check> new_filter_check_from_fldname(const std::string& name, sinsp* inspector, bool do_exact_check) const;
std::unique_ptr<sinsp_filter_check> new_filter_check_from_fldname(std::string_view name, sinsp*, bool do_exact_check) const;

protected:
std::vector<std::unique_ptr<sinsp_filter_check>> m_check_list;
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ std::shared_ptr<sinsp_plugin> sinsp_plugin::create(
return nullptr;
}

std::shared_ptr<sinsp_plugin> plugin(new sinsp_plugin(handle, treg));
auto plugin = std::make_shared<sinsp_plugin>(handle, treg);
if (!plugin->resolve_dylib_symbols(errstr))
{
// plugin and handle get deleted here by shared_ptr
Expand All @@ -123,7 +123,7 @@ std::shared_ptr<sinsp_plugin> sinsp_plugin::create(
return nullptr;
}

std::shared_ptr<sinsp_plugin> plugin(new sinsp_plugin(handle, treg));
auto plugin = std::make_shared<sinsp_plugin>(handle, treg);
if (!plugin->resolve_dylib_symbols(errstr))
{
// plugin and handle get deleted here by shared_ptr
Expand Down
Loading

0 comments on commit 8ceee69

Please sign in to comment.