2014-01-11 02:09:40 +01:00
|
|
|
/*
|
|
|
|
* \brief Utility for status reporting
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2014-01-07
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2014-2017 Genode Labs GmbH
|
2014-01-11 02:09:40 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2014-01-11 02:09:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__OS__REPORTER_H_
|
|
|
|
#define _INCLUDE__OS__REPORTER_H_
|
|
|
|
|
2018-01-24 19:00:35 +01:00
|
|
|
#include <util/retry.h>
|
2018-05-25 22:54:02 +02:00
|
|
|
#include <util/xml_node.h>
|
2016-12-01 17:37:08 +01:00
|
|
|
#include <util/reconstructible.h>
|
2017-01-30 11:35:12 +01:00
|
|
|
#include <base/attached_dataspace.h>
|
2014-01-11 02:09:40 +01:00
|
|
|
#include <report_session/connection.h>
|
|
|
|
#include <util/xml_generator.h>
|
|
|
|
|
2018-01-24 19:00:35 +01:00
|
|
|
namespace Genode {
|
|
|
|
class Reporter;
|
|
|
|
class Expanding_reporter;
|
|
|
|
}
|
2014-01-11 02:09:40 +01:00
|
|
|
|
|
|
|
|
2018-01-24 19:00:35 +01:00
|
|
|
class Genode::Reporter
|
2014-01-11 02:09:40 +01:00
|
|
|
{
|
2014-09-30 18:17:28 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
typedef String<100> Name;
|
|
|
|
|
2014-01-11 02:09:40 +01:00
|
|
|
private:
|
|
|
|
|
2019-01-30 17:27:46 +01:00
|
|
|
Env &_env;
|
|
|
|
|
2016-04-18 21:13:47 +02:00
|
|
|
Name const _xml_name;
|
|
|
|
Name const _label;
|
2014-01-11 02:09:40 +01:00
|
|
|
|
2014-04-28 23:23:43 +02:00
|
|
|
size_t const _buffer_size;
|
|
|
|
|
2014-01-11 02:09:40 +01:00
|
|
|
bool _enabled = false;
|
|
|
|
|
|
|
|
struct Connection
|
|
|
|
{
|
|
|
|
Report::Connection report;
|
2019-01-30 17:27:46 +01:00
|
|
|
Attached_dataspace ds;
|
2014-01-11 02:09:40 +01:00
|
|
|
|
2019-01-30 17:27:46 +01:00
|
|
|
Connection(Env &env, char const *name, size_t buffer_size)
|
|
|
|
: report(env, name, buffer_size), ds(env.rm(), report.dataspace())
|
|
|
|
{ }
|
2014-01-11 02:09:40 +01:00
|
|
|
};
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
Constructible<Connection> _conn { };
|
2014-01-11 02:09:40 +01:00
|
|
|
|
2015-06-09 13:14:57 +02:00
|
|
|
/**
|
|
|
|
* Return size of report buffer
|
|
|
|
*/
|
|
|
|
size_t _size() const { return _enabled ? _conn->ds.size() : 0; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return pointer to report buffer
|
|
|
|
*/
|
|
|
|
char *_base() { return _enabled ? _conn->ds.local_addr<char>() : 0; }
|
|
|
|
|
2014-01-11 02:09:40 +01:00
|
|
|
public:
|
|
|
|
|
2019-01-30 17:27:46 +01:00
|
|
|
Reporter(Env &env, char const *xml_name, char const *label = nullptr,
|
2016-04-18 21:13:47 +02:00
|
|
|
size_t buffer_size = 4096)
|
|
|
|
:
|
2019-01-30 17:27:46 +01:00
|
|
|
_env(env), _xml_name(xml_name), _label(label ? label : xml_name),
|
2017-01-09 15:18:49 +01:00
|
|
|
_buffer_size(buffer_size)
|
|
|
|
{ }
|
|
|
|
|
2014-01-11 02:09:40 +01:00
|
|
|
/**
|
|
|
|
* Enable or disable reporting
|
|
|
|
*/
|
|
|
|
void enabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled == _enabled) return;
|
|
|
|
|
2014-02-14 20:39:15 +01:00
|
|
|
if (enabled)
|
2019-01-30 17:27:46 +01:00
|
|
|
_conn.construct(_env, _label.string(), _buffer_size);
|
2014-01-11 02:09:40 +01:00
|
|
|
else
|
|
|
|
_conn.destruct();
|
2014-02-14 20:39:15 +01:00
|
|
|
|
|
|
|
_enabled = enabled;
|
2014-01-11 02:09:40 +01:00
|
|
|
}
|
|
|
|
|
2016-05-11 18:21:47 +02:00
|
|
|
/**
|
|
|
|
* Return true if reporter is enabled
|
|
|
|
*/
|
|
|
|
bool enabled() const { return _enabled; }
|
|
|
|
|
2016-04-18 21:13:47 +02:00
|
|
|
Name name() const { return _label; }
|
2014-09-30 18:17:28 +02:00
|
|
|
|
2015-06-16 12:06:54 +02:00
|
|
|
/**
|
|
|
|
* Clear report buffer
|
|
|
|
*/
|
|
|
|
void clear() { memset(_base(), 0, _size()); }
|
|
|
|
|
2014-01-11 02:09:40 +01:00
|
|
|
/**
|
2015-06-09 13:14:57 +02:00
|
|
|
* Report data buffer
|
|
|
|
*
|
|
|
|
* \param data data buffer
|
|
|
|
* \param length number of bytes to report
|
2014-01-11 02:09:40 +01:00
|
|
|
*/
|
2015-06-09 13:14:57 +02:00
|
|
|
void report(void const *data, size_t length)
|
|
|
|
{
|
|
|
|
void *base = _base();
|
2014-01-11 02:09:40 +01:00
|
|
|
|
2015-06-09 13:14:57 +02:00
|
|
|
if (!base || length > _size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(base, data, length);
|
|
|
|
_conn->report.submit(length);
|
|
|
|
}
|
2014-01-11 02:09:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* XML generator targeting a reporter
|
|
|
|
*/
|
|
|
|
struct Xml_generator : public Genode::Xml_generator
|
|
|
|
{
|
|
|
|
template <typename FUNC>
|
|
|
|
Xml_generator(Reporter &reporter, FUNC const &func)
|
|
|
|
:
|
2015-06-09 13:14:57 +02:00
|
|
|
Genode::Xml_generator(reporter._base(),
|
|
|
|
reporter._size(),
|
2016-04-18 21:13:47 +02:00
|
|
|
reporter._xml_name.string(),
|
2014-01-11 02:09:40 +01:00
|
|
|
func)
|
|
|
|
{
|
2017-03-02 12:24:26 +01:00
|
|
|
if (reporter.enabled())
|
|
|
|
reporter._conn->report.submit(used());
|
2014-01-11 02:09:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-01-24 19:00:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reporter that increases the report buffer capacity on demand
|
|
|
|
*
|
|
|
|
* This convenience wrapper of the 'Reporter' alleviates the need to handle
|
|
|
|
* 'Xml_generator::Buffer_exceeded' exceptions manually. In most cases, the
|
|
|
|
* only reasonable way to handle such an exception is upgrading the report
|
|
|
|
* buffer as done by this class. Furthermore, in contrast to the regular
|
|
|
|
* 'Reporter', which needs to be 'enabled', the 'Expanding_reporter' is
|
|
|
|
* implicitly enabled at construction time.
|
|
|
|
*/
|
|
|
|
class Genode::Expanding_reporter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef Session_label Label;
|
|
|
|
typedef String<64> Node_type;
|
|
|
|
|
2019-10-21 09:40:00 +02:00
|
|
|
struct Initial_buffer_size { size_t value; };
|
|
|
|
|
2018-01-24 19:00:35 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
Env &_env;
|
|
|
|
|
|
|
|
Node_type const _type;
|
|
|
|
Label const _label;
|
|
|
|
|
|
|
|
Constructible<Reporter> _reporter { };
|
|
|
|
|
2019-10-21 09:40:00 +02:00
|
|
|
size_t _buffer_size;
|
2018-01-24 19:00:35 +01:00
|
|
|
|
|
|
|
void _construct()
|
|
|
|
{
|
|
|
|
_reporter.construct(_env, _type.string(), _label.string(), _buffer_size);
|
|
|
|
_reporter->enabled(true);
|
|
|
|
}
|
|
|
|
|
2018-05-25 22:54:02 +02:00
|
|
|
void _increase_report_buffer()
|
|
|
|
{
|
|
|
|
_buffer_size += 4096;
|
|
|
|
_construct();
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:00:35 +01:00
|
|
|
public:
|
|
|
|
|
2019-10-21 09:40:00 +02:00
|
|
|
Expanding_reporter(Env &env, Node_type const &type, Label const &label,
|
|
|
|
Initial_buffer_size const size = { 4096 })
|
|
|
|
: _env(env), _type(type), _label(label), _buffer_size(size.value)
|
|
|
|
{ _construct(); }
|
2018-01-24 19:00:35 +01:00
|
|
|
|
|
|
|
template <typename FN>
|
|
|
|
void generate(FN const &fn)
|
|
|
|
{
|
|
|
|
retry<Xml_generator::Buffer_exceeded>(
|
|
|
|
|
|
|
|
[&] () {
|
|
|
|
Reporter::Xml_generator
|
|
|
|
xml(*_reporter, [&] () { fn(xml); }); },
|
|
|
|
|
2018-05-25 22:54:02 +02:00
|
|
|
[&] () { _increase_report_buffer(); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void generate(Xml_node node)
|
|
|
|
{
|
|
|
|
retry<Xml_generator::Buffer_exceeded>(
|
|
|
|
|
2019-01-30 17:27:46 +01:00
|
|
|
[&] () {
|
|
|
|
node.with_raw_node([&] (char const *start, size_t length) {
|
|
|
|
_reporter->report(start, length); }); },
|
2018-05-25 22:54:02 +02:00
|
|
|
|
|
|
|
[&] () { _increase_report_buffer(); }
|
2018-01-24 19:00:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-11 02:09:40 +01:00
|
|
|
#endif /* _INCLUDE__OS__REPORTER_H_ */
|