strace: Add mutex type

Added a dedicated mutex type handler that can print mutex status
flags while still showing the mutex address.

Change-Id: Ie028e5c0a336063a4c03a4f9adf955ffa6911837
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6557
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Trung Nguyen 2023-06-09 20:21:35 +10:00 committed by waddlesplash
parent c80a875a74
commit 29f8da76d7
4 changed files with 88 additions and 1 deletions

View File

@ -24,6 +24,7 @@ local straceSources =
exec.cpp
fcntl.cpp
ioctl.cpp
mutex.cpp
network.cpp
signals.cpp
;

View File

@ -63,9 +63,10 @@ public:
string GetParameterValue(Context &c, Parameter *, const void *);
string GetReturnValue(Context &, uint64 value);
private:
protected:
string RenderValue(Context &, unsigned int value) const;
private:
const FlagsList &fList;
};

View File

@ -0,0 +1,83 @@
/*
* Copyright 2023, Trung Nguyen, trungnt282910@gmail.com.
* Distributed under the terms of the MIT License.
*/
#include <string.h>
#include <user_mutex_defs.h>
#include "strace.h"
#include "Context.h"
#include "MemoryReader.h"
#include "TypeHandler.h"
#define FLAG_INFO_ENTRY(name) \
{ name, #name }
static const FlagsTypeHandler::FlagInfo kMutexFlagsInfo[] = {
FLAG_INFO_ENTRY(B_USER_MUTEX_LOCKED),
FLAG_INFO_ENTRY(B_USER_MUTEX_WAITING),
FLAG_INFO_ENTRY(B_USER_MUTEX_DISABLED),
{ 0, NULL }
};
static FlagsTypeHandler::FlagsList kMutexFlags;
class MutexTypeHandler : public FlagsTypeHandler {
public:
MutexTypeHandler()
:
FlagsTypeHandler(kMutexFlags)
{
}
string GetParameterValue(Context &context, Parameter *param,
const void *address)
{
void *data = *(void **)address;
if (context.GetContents(Context::POINTER_VALUES)) {
int32 value, bytesRead;
status_t err = context.Reader().Read(data, &value, sizeof(value), bytesRead);
if (err != B_OK)
return context.FormatPointer(data);
string r = context.FormatPointer(data);
r += " [";
r += RenderValue(context, (unsigned int)value);
r += "]";
return r;
}
return context.FormatPointer(data);
}
string GetReturnValue(Context &context, uint64 value)
{
return context.FormatPointer((void *)value);
}
};
void
patch_mutex()
{
for (int i = 0; kMutexFlagsInfo[i].name != NULL; i++) {
kMutexFlags.push_back(kMutexFlagsInfo[i]);
}
Syscall *mutex_lock = get_syscall("_kern_mutex_lock");
mutex_lock->GetParameter("mutex")->SetHandler(new MutexTypeHandler());
Syscall *mutex_unlock = get_syscall("_kern_mutex_unblock");
mutex_unlock->GetParameter("mutex")->SetHandler(new MutexTypeHandler());
Syscall *mutex_switch_lock = get_syscall("_kern_mutex_switch_lock");
mutex_switch_lock->GetParameter("fromMutex")->SetHandler(new MutexTypeHandler());
mutex_switch_lock->GetParameter("toMutex")->SetHandler(new MutexTypeHandler());
}

View File

@ -233,6 +233,7 @@ patch_syscalls()
extern void patch_exec();
extern void patch_fcntl();
extern void patch_ioctl();
extern void patch_mutex();
extern void patch_network();
for (size_t i = 0; i < sSyscallVector.size(); i++) {
@ -250,6 +251,7 @@ patch_syscalls()
patch_exec();
patch_fcntl();
patch_ioctl();
patch_mutex();
patch_network();
}