1 #include "../include/util.hpp"
2 #include "../include/tracer.hpp"
14 pthread_mutex_t
util::m_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
31 catch (std::exception &x) {
60 if (
likely(major != NULL) )
63 if (
likely(minor != NULL) )
94 i8 path[PATH_MAX + 1];
95 i32 len = snprintf(path, PATH_MAX + 1,
"/proc/%d/exe", getpid());
97 throw exception(
"snprintf failed with retval %d", len);
99 i8 *retval =
new i8[PATH_MAX + 1];
102 len = readlink(path, retval, PATH_MAX);
106 "failed to read symlink '%s' (errno %d - %s)",
139 string *token = NULL;
145 for (
u32 i = 0, sz = strlen(val);
likely(i < sz); i++) {
192 if (
unlikely(argc <= 1 || argv == NULL) )
200 if (
likely(strstr(arg,
"--csdbg-") != arg) )
203 if (
likely(strlen(arg) > 8) )
208 argv[j] = argv[j + 1];
214 #if CSDBG_DBG_LEVEL & CSDBG_DBGL_INFO
229 catch (std::exception &x) {
278 const i8 *nm = inf.name();
281 i8 *retval = abi::__cxa_demangle(nm, NULL, NULL, NULL);
282 if (
likely(retval != NULL) )
285 retval =
new i8[strlen(nm) + 1];
301 return (a < c) ? a : c;
303 return (b < c)? b : c;
326 u8 *p =
static_cast<u8*
> (mem);
327 while (
likely(sz-- > 0) )
351 if (
unlikely(dst == NULL || src == NULL) )
354 u8 *d =
static_cast<u8*
> (dst);
355 const u8 *s =
static_cast<const u8*
> (src);
356 while (
likely(sz-- > 0) )
380 u8 *l =
static_cast<u8*
> (mem);
399 pthread_mutex_lock(&
m_lock);
408 pthread_mutex_unlock(&
m_lock);
421 return S_ISREG(inf.st_mode);
434 return S_ISCHR(inf.st_mode);
447 if (
likely(geteuid() == inf.st_uid && (inf.st_mode & S_IRUSR)) )
450 if (
likely(getegid() == inf.st_gid && (inf.st_mode & S_IRGRP)) )
453 return inf.st_mode & S_IROTH;
466 if (
likely(geteuid() == inf.st_uid && (inf.st_mode & S_IWUSR)) )
469 if (
likely(getegid() == inf.st_gid && (inf.st_mode & S_IWGRP)) )
472 return inf.st_mode & S_IWOTH;
493 throw exception(
"invalid argument: fmt (=%p)", fmt);
496 i32 retval = vsnprintf(NULL, 0, fmt, args);
499 throw exception(
"vsnprintf failed with retval %d", retval);
523 throw exception(
"invalid argument: fmt (=%p)", fmt);
527 va_copy(cpargs, args);
532 retval =
new i8[sz + 1];
534 i32 check = vsprintf(retval, fmt, args);
536 throw exception(
"vsprintf failed with retval %d", check);
572 throw exception(
"invalid argument: fmt (=%p)", fmt);
578 i32 sz = vsprintf(dst, fmt, args);
581 throw exception(
"vsprintf failed with retval %d", sz);
600 #ifdef CSDBG_WITH_COLOR_TERM
601 stream <<
"\e[38;5;" << std::dec;
607 else if (
likely(ch ==
'w') )
613 stream <<
"m[" << tag <<
"]\e[0m";
615 stream <<
"[" << tag <<
"]";
618 stream <<
" [" << std::dec << getpid() <<
", ";
619 stream <<
"0x" << std::hex << pthread_self();
621 const i8 *thr = NULL;
623 if (
likely(iface != NULL) )
626 stream <<
" (" << ((thr != NULL) ? thr :
"anon") <<
")] ";
643 #ifdef CSDBG_WITH_DEBUG
646 if (
unlikely(tag == NULL || fmt == NULL) ) {
656 if (
likely(!isspace(fmt[0])) )
659 std::cerr << msg <<
"\r\n";
682 #if CSDBG_DBG_LEVEL & CSDBG_DBGL_INFO
705 #if CSDBG_DBG_LEVEL & CSDBG_DBGL_WARNING
728 #if CSDBG_DBG_LEVEL & CSDBG_DBGL_ERROR
static pthread_mutex_t m_lock
Global access mutex.
virtual string & append(const string &)
Append a string.
virtual T * at(u32) const
Get the node data pointer at a chain offset.
static const i8 * exec_path()
Get the absolute path of the executable.
static bool is_writable(const fileinfo_t &)
Check if the process has write access to a file.
virtual u32 size() const
Get the chain size (node count)
static void * memset(void *, u8, u32)
Fill a memory block with a constant byte.
char i8
8-bit signed integer
struct stat fileinfo_t
File metadata.
static tracer * interface()
Get the interface object.
static void version(u16 *, u16 *)
Get the library version numbers.
A tracer object is the default interface to libcsdbg for the instrumentation functions and for the li...
static const u16 g_minor
Library version minor.
static void on_lib_load() __attribute((constructor))
Library constructor.
static u32 argc()
Get the number of CLI arguments.
static const i8 * type_name(const std::type_info &)
Get the demangled name of a type.
unsigned short u16
16-bit unsigned integer
static void on_lib_unload() __attribute((destructor))
Library destructor.
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
static void dbg_warn(const i8 *,...)
Print a warning debug message on the standard error stream.
static const i8 * prefix()
Get the library installation prefix.
unsigned char u8
8-bit unsigned integer
static void lock()
Lock the global access mutex.
static void unlock()
Unlock the global access mutex.
static bool is_readable(const fileinfo_t &)
Check if the process has read access to a file.
static void dbg_error(const i8 *,...)
Print an error debug message on the standard error stream.
static bool is_chardev(const fileinfo_t &)
Check if a file is a character device node.
static chain< string > * m_config
Runtime configuration.
virtual const i8 * cstr() const
Get the C-string equivalent.
virtual chain & add(T *)
Add a node to the chain.
static const i8 g_prefix[]
Library installation prefix.
static void dbg(const i8 *, const i8 *, va_list)
Print a tagged debug message on the standard error stream.
static u32 min(u32, u32, u32)
Get the minimum of three numbers.
Lightweight string buffer class (for ISO-8859-1 text)
static i8 * va_format(const i8 *, va_list)
Format a buffer with a printf-style string expanded with the values of a variable argument list...
static void * memswap(void *, u32)
Reverse the byte order of a memory block.
unsigned int u32
32-bit unsigned integer
int i32
32-bit signed integer
static chain< string > * getenv(const i8 *)
Parse a shell (environment) variable to its components.
static bool is_regular(const fileinfo_t &)
Check if a file is a regular one.
virtual u32 length() const
Get the character count.
#define ERROR_TAG_FG
Tag color for error and exception messages.
static void header(std::ostream &, const i8 *)
Print a tagged message header on an output stream.
virtual const i8 * name() const
Get the thread name.
#define WARNING_TAG_FG
Tag color for warning messages.
virtual process * proc() const
Get the process handle.
This class is a throwable with a textual description of an error.
virtual thread * current_thread()
Get the currently executing thread.
static i32 va_size(const i8 *, va_list)
Compute the size of a printf-style format string expanded with the values of a variable argument list...
#define INFO_TAG_FG
Tag color for informational messages.
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
static void * memcpy(void *, const void *, u32)
Copy a memory block.
static void dbg_info(const i8 *,...)
Print an informational debug message on the standard error stream.
static const string * argv(u32)
Get a CLI argument, given its offset in util::m_config.
static const u16 g_major
Library version major.
static void init(i32 &, i8 **)
Initialize the library runtime configuration. Seek command line arguments that are related with libcs...
#define __D_ASSERT(x)
Assertion macro.