diff options
| author | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
|---|---|---|
| committer | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
| commit | b691c43c44ff180d10e7d4a9afc83b98551ff586 (patch) | |
| tree | 3e9849222cbc6ba49f2f1fc6e5fe7179632c7390 /Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook | |
| parent | def8cb415354ac390b7e89052a50605285f1aca9 (diff) | |
Initial commit
Diffstat (limited to 'Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook')
7 files changed, 429 insertions, 0 deletions
diff --git a/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/IThread.h b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/IThread.h new file mode 100644 index 00000000..9ac6dcf9 --- /dev/null +++ b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/IThread.h @@ -0,0 +1,17 @@ +#ifndef _MULTITHREADEDHOOKSAMPLE_ITHREAD_H_ +#define _MULTITHREADEDHOOKSAMPLE_ITHREAD_H_ + +typedef void(*ThreadFunction)(); + +class IThread +{ +public: + virtual ~IThread() {} + virtual void Fork(ThreadFunction a_Function) = 0; + virtual void Join() = 0; +}; + +IThread* CreateThread(); +void DestroyThread(IThread* a_Thread); + +#endif // _MULTITHREADEDHOOKSAMPLE_ITHREAD_H_
\ No newline at end of file diff --git a/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/MultiThreadedHookSample.cpp b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/MultiThreadedHookSample.cpp new file mode 100644 index 00000000..4c872680 --- /dev/null +++ b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/MultiThreadedHookSample.cpp @@ -0,0 +1,80 @@ +// ================================================================================================================================= +// This sample is more of a show-(and test) case for HeapInspector's. It demonstrates: +// 1) That HeapInspector is multithread safe. +// 2) HeapInspector's ability to deal with allocations prior to Initialise (although those allocations will not be tracked). +// 3) HeapInspector's ability to deal with API calls during static initialisation phase. +// +// In this sample, multiple threads are started that perform allocations for a set period of time. The +// application will wait for those threads to finish. After the time is passed and the application calls Shutdown, +// the client will disconnect. +// +// To switch between launching the threads during the static initalisation phase and launching the treads +// in main, flip the INIT_IN_STATIC_PHASE define. +// +// ================================================================================================================================= + +#include "IThread.h" +#include <stdlib.h> + +void Wait(int a_MilliSeconds); + +#define INIT_IN_STATIC_PHASE 0 +const int g_NumThreads = 4; + +class MultiThreadedAllocator +{ +public: + static void WorkerThread() + { + for (int i = 0; i != 1000; ++i) + { + void* mem1 = malloc(10); + Wait(10); + free(mem1); + Wait(10); + } + } + + MultiThreadedAllocator() + { + for (int i = 0; i != g_NumThreads; ++i) + { + m_Threads[i] = CreateThread(); + m_Threads[i]->Fork(WorkerThread); + } + } + + ~MultiThreadedAllocator() + { + WaitForThreads(); + for (int i = 0; i != g_NumThreads; ++i) + { + DestroyThread(m_Threads[i]); + } + } + +private: + void WaitForThreads() + { + for (int i = 0; i != g_NumThreads; ++i) + { + m_Threads[i]->Join(); + } + } + +private: + IThread* m_Threads[g_NumThreads]; +}; + +#if INIT_IN_STATIC_PHASE +static MultiThreadedAllocator* g_Allocator = new MultiThreadedAllocator(); +#endif + +void RunHeapInspectorServer() +{ +#if !INIT_IN_STATIC_PHASE + MultiThreadedAllocator* g_Allocator = new MultiThreadedAllocator(); +#endif + + delete g_Allocator; +} diff --git a/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/Main.cpp b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/Main.cpp new file mode 100644 index 00000000..e3417e42 --- /dev/null +++ b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/Main.cpp @@ -0,0 +1,18 @@ +#include "../../../Server/HeapInspectorServer.h" +#include "../../../Server/PS3/HeapHooks.hpp" + +void RunHeapInspectorServer(); + +extern "C" void* __real__malloc_init(); +extern "C" void* __wrap__malloc_init() +{ + void* result = __real__malloc_init(); + Initialise(HeapInspectorServer::GetDefaultHeapInfo(), 3000, HeapInspectorServer::WaitForConnection_Enabled); + return result; +} + +int main(int /*a_ArgC*/, const char* /*a_ArgV[]*/) +{ + RunHeapInspectorServer(); + HeapInspectorServer::Shutdown(); +} diff --git a/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/MultiThreadedHookSample.vcproj b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/MultiThreadedHookSample.vcproj new file mode 100644 index 00000000..76c17590 --- /dev/null +++ b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/MultiThreadedHookSample.vcproj @@ -0,0 +1,191 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="9,00" + Name="MultiThreadedHookSample" + ProjectGUID="{37C97160-EB4F-46FD-9CA7-0E0789EFE398}" + RootNamespace="BasicServerSample" + TargetFrameworkVersion="196613" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="PS3 Debug|Win32" + OutputDirectory="Debug_VS2008" + IntermediateDirectory="Debug_VS2008" + ConfigurationType="1" + DeleteExtensionsOnClean="*.obj;*.d;*.map;*.lst;*.pch;$(TargetPath);$(TargetDir)$(TargetName).self" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + AdditionalOptions="-g -Wall -fno-exceptions -fno-rtti" + AdditionalIncludeDirectories=""$(SN_PS3_PATH)\ppu\include\sn";"$(SCE_PS3_ROOT)\target\ppu\include";"$(SCE_PS3_ROOT)\target\common\include"" + PreprocessorDefinitions="SN_TARGET_PS3;_DEBUG;__GCC__;__CELL_ASSERT__;HEAPINSPECTOR_PS3=1" + ProgramDataBaseFileName="" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalOptions="-fno-exceptions -fno-rtti -Wl,--wrap=malloc,--wrap=free,--wrap=calloc,--wrap=memalign,--wrap=realloc,--wrap=reallocalign,--wrap=_malloc_init" + AdditionalDependencies=""$(SN_PS3_PATH)\ppu\lib\sn\libsn.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libm.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libio_stub.a" "..\..\..\Server\PS3\Debug\libHeapInspectorServer.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libpthread.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libnet_stub.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libsysmodule_stub.a"" + OutputFile="$(OutDir)/$(ProjectName).ppu.elf" + AdditionalLibraryDirectories="" + ProgramDatabaseFile="" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + Description="Fake Signing ELF "$(TargetDir)$(TargetName).self"" + CommandLine="$(SCE_PS3_ROOT)\host-win32\bin\make_fself "$(TargetPath)" "$(TargetDir)$(TargetName).self"" + /> + </Configuration> + <Configuration + Name="PS3 Release|Win32" + OutputDirectory="Release_VS2008" + IntermediateDirectory="Release_VS2008" + ConfigurationType="1" + DeleteExtensionsOnClean="*.obj;*.d;*.map;*.lst;*.pch;$(TargetPath);$(TargetDir)$(TargetName).self" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + AdditionalOptions="-Wall -fno-exceptions -fno-rtti -O2" + AdditionalIncludeDirectories=""$(SN_PS3_PATH)\ppu\include\sn";"$(SCE_PS3_ROOT)\target\ppu\include";"$(SCE_PS3_ROOT)\target\common\include"" + PreprocessorDefinitions="SN_TARGET_PS3;NDEBUG;__GCC__;HEAPINSPECTOR_PS3=1" + ProgramDataBaseFileName="" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalOptions="-fno-exceptions -fno-rtti -Wl,--wrap=malloc,--wrap=free,--wrap=calloc,--wrap=memalign,--wrap=realloc,--wrap=reallocalign,--wrap=_malloc_init" + AdditionalDependencies=""$(SN_PS3_PATH)\ppu\lib\sn\libsn.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libm.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libio_stub.a" "..\..\..\Server\PS3\Release\libHeapInspectorServer.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libpthread.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libnet_stub.a" "$(SCE_PS3_ROOT)\target\ppu\lib\libsysmodule_stub.a"" + OutputFile="$(OutDir)/$(ProjectName).ppu.elf" + AdditionalLibraryDirectories="" + ProgramDatabaseFile="" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + Description="Fake Signing ELF "$(TargetDir)$(TargetName).self"" + CommandLine="$(SCE_PS3_ROOT)\host-win32\bin\make_fself "$(TargetPath)" "$(TargetDir)$(TargetName).self"" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="PS3" + > + <File + RelativePath=".\Main.cpp" + > + </File> + <File + RelativePath=".\ThreadPS3.cpp" + > + </File> + <File + RelativePath=".\Wait.cpp" + > + </File> + </Filter> + <File + RelativePath="..\IThread.h" + > + </File> + <File + RelativePath="..\MultiThreadedHookSample.cpp" + > + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/MultiThreadedHookSample.vcxproj b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/MultiThreadedHookSample.vcxproj new file mode 100644 index 00000000..69d3882c --- /dev/null +++ b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/MultiThreadedHookSample.vcxproj @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|PS3"> + <Configuration>Debug</Configuration> + <Platform>PS3</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|PS3"> + <Configuration>Release</Configuration> + <Platform>PS3</Platform> + </ProjectConfiguration> + </ItemGroup> + <ItemGroup> + <ClInclude Include="..\IThread.h" /> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\MultiThreadedHookSample.cpp" /> + <ClCompile Include="Main.cpp" /> + <ClCompile Include="ThreadPS3.cpp" /> + <ClCompile Include="Wait.cpp" /> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{E9BC25AD-CFFD-43B6-ABEC-CA516CADD296}</ProjectGuid> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|PS3'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <PlatformToolset>GCC</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|PS3'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <PlatformToolset>GCC</PlatformToolset> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|PS3'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|PS3'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|PS3'"> + <OutDir>$(ProjectDir)$(Platform)_$(Configuration)_VS2010\</OutDir> + <IntDir>$(Platform)_$(Configuration)_VS2010\</IntDir> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|PS3'"> + <OutDir>$(ProjectDir)$(Platform)_$(Configuration)_VS2010\</OutDir> + <IntDir>$(Platform)_$(Configuration)_VS2010\</IntDir> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|PS3'"> + <ClCompile> + <PreprocessorDefinitions>_DEBUG;__CELL_ASSERT__;%(PreprocessorDefinitions);;HEAPINSPECTOR_PS3=1</PreprocessorDefinitions> + <GenerateDebugInformation>true</GenerateDebugInformation> + </ClCompile> + <Link> + <AdditionalDependencies>"$(SCE_PS3_ROOT)\target\ppu\lib\libm.a";"$(SCE_PS3_ROOT)\target\ppu\lib\libio_stub.a";"..\..\..\Server\PS3\Debug\libHeapInspectorServer.a";"$(SCE_PS3_ROOT)\target\ppu\lib\libpthread.a";"$(SCE_PS3_ROOT)\target\ppu\lib\libnet_stub.a";"$(SCE_PS3_ROOT)\target\ppu\lib\libsysmodule_stub.a";%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalOptions>-Wl,--wrap=malloc,--wrap=free,--wrap=calloc,--wrap=memalign,--wrap=realloc,--wrap=reallocalign,--wrap=_malloc_init %(AdditionalOptions)</AdditionalOptions> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|PS3'"> + <ClCompile> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions);;HEAPINSPECTOR_PS3=1</PreprocessorDefinitions> + <OptimizationLevel>Level2</OptimizationLevel> + </ClCompile> + <Link> + <AdditionalDependencies>"..\..\..\Server\PS3\Release\libHeapInspectorServer.a";"$(SCE_PS3_ROOT)\target\ppu\lib\libpthread.a";"$(SCE_PS3_ROOT)\target\ppu\lib\libnet_stub.a";"$(SCE_PS3_ROOT)\target\ppu\lib\libsysmodule_stub.a";%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalOptions>-Wl,--wrap=malloc,--wrap=free,--wrap=calloc,--wrap=memalign,--wrap=realloc,--wrap=reallocalign,--wrap=_malloc_init %(AdditionalOptions)</AdditionalOptions> + </Link> + </ItemDefinitionGroup> + <Import Condition="'$(ConfigurationType)' == 'Makefile' and Exists('$(VCTargetsPath)\Platforms\$(Platform)\SCE.Makefile.$(Platform).targets')" Project="$(VCTargetsPath)\Platforms\$(Platform)\SCE.Makefile.$(Platform).targets" /> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/ThreadPS3.cpp b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/ThreadPS3.cpp new file mode 100644 index 00000000..ed27945a --- /dev/null +++ b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/ThreadPS3.cpp @@ -0,0 +1,41 @@ +#include <pthread.h> +#include "../IThread.h" + +void* InternalThread(void* a_UserData) +{ + ThreadFunction function = (ThreadFunction)a_UserData; + function(); + + pthread_exit(0); + return 0; +} + +class ThreadPS3 : public IThread +{ +public: + virtual void Fork(ThreadFunction a_Function) + { + pthread_create(&m_Thread, 0, InternalThread, (void*)a_Function); + } + + virtual void Join() + { + void* threadResult; + pthread_join(m_Thread, &threadResult); + } + +private: + pthread_t m_Thread; +}; + + +IThread* CreateThread() +{ + return new ThreadPS3(); +} + +void DestroyThread(IThread* a_Thread) +{ + delete a_Thread; +} + diff --git a/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/Wait.cpp b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/Wait.cpp new file mode 100644 index 00000000..2c436c84 --- /dev/null +++ b/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/PS3/Wait.cpp @@ -0,0 +1,6 @@ +#include <sys/timer.h> + +void Wait(int a_Milliseconds) +{ + sys_timer_usleep(a_Milliseconds * 1000); +}
\ No newline at end of file |
