diff --git a/plugins/memory_info/Makefile b/plugins/memory_info/Makefile new file mode 100644 index 0000000..d89c552 --- /dev/null +++ b/plugins/memory_info/Makefile @@ -0,0 +1,291 @@ +DO_LOGGING := 1 + +#--------------------------------------------------------------------------------- +# Clear the implicit built in rules +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- +ifeq ($(strip $(DEVKITPPC)),) +$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") +endif +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=devkitPRO") +endif +export PATH := $(DEVKITPPC)/bin:$(PORTLIBS)/bin:$(PATH) +export PORTLIBS := $(DEVKITPRO)/portlibs/ppc +export WUPSDIR := $(DEVKITPRO)/wups +export GCC_VER := $(shell $(DEVKITPPC)/bin/powerpc-eabi-gcc -dumpversion) + +PREFIX := powerpc-eabi- + +export AS := $(PREFIX)as +export CC := $(PREFIX)gcc +export CXX := $(PREFIX)g++ +export LD := $(PREFIX)ld +export AR := $(PREFIX)ar +export OBJCOPY := $(PREFIX)objcopy + +#--------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# INCLUDES is a list of directories containing extra header files +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)).mod +BUILD := build +SOURCES := src + +DATA := + +INCLUDES := src + +MAP ?= $(TARGET:.mod=.map) + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- + +# -Os: optimise size +# -Wall: generate lots of warnings +# -DGEKKO_U: define the symbol GEKKO (used in some headers) +# -D__wiiu__: define the symbol __wii__ (used in some headers) +# -mrvl: enable wii/gamecube compilation +# -mcpu=750: enable processor specific compilation +# -meabi: enable eabi specific compilation +# -mhard-float: enable hardware floating point instructions +# -fshort-wchar: use 16 bit whcar_t type in keeping with Wii executables +# -fno-common: stop common variables which the loader can't understand +# -msdata-none: do not use r2 or r13 as small data areas +# -memb: enable embedded application specific compilation +# -ffunction-sections: split up functions so linker can garbage collect +# -fdata-sections: split up data so linker can garbage collect +COMMON_CFLAGS += -Os -Wall -DGEKKO_U -D__wiiu__ -mrvl -mcpu=750 -meabi -mhard-float -fshort-wchar -fno-common -msdata=none -memb -ffunction-sections -fdata-sections + + +# -x c: compile as c code +# -std=c11: use the c11 standard +CFLAGS += $(COMMON_CFLAGS) -x c -std=c11 + +# -x c: compile as c++ code +# -std=gnu++11: use the c++11 standard +CXXFLAGS += $(COMMON_CFLAGS) -x c++ -std=gnu++11 + +ifeq ($(DO_LOGGING), 1) + CFLAGS += -D__LOGGING__ + CXXFLAGS += -D__LOGGING__ +endif + +ASFLAGS := -mregnames +# --relocatable: make sure ld doesn't remove relocations wups will need +# -s: strip local symbols to speed linking +# -u: keep certain sections +# -wrap: wrap function +# --gc-sections: remove unneeded symbols +# -T: use the linker script specified (to force certain wups sections together) +# -Map: generate a map file + +LDFLAG_COMMON += -u wups_load -u wups_meta -u wups_hooks -T $(WUPSDIR)/wups.ld \ + -Wl,-wrap,open,-wrap,close,-wrap,write,-wrap,read,-wrap,lseek,-wrap,stat,-wrap,fstat,-wrap,opendir,-wrap,closedir,-wrap,readdir \ + -Wl,-Map,$(notdir $@).map,-wrap,malloc,-wrap,free,-wrap,memalign,-wrap,calloc,-wrap,realloc,-wrap,malloc_usable_size,-wrap,_malloc_r,-wrap,_free_r,-wrap,_realloc_r,-wrap,_calloc_r,-wrap,_memalign_r,-wrap,_malloc_usable_size_r,--gc-sections + +LDFLAGS_MOD += $(LDFLAG_COMMON),--relocatable +LDFLAGS_ELF += --relocatable -s -T $(WUPSDIR)/wups_elf.ld + +#--------------------------------------------------------------------------------- +Q := @ +MAKEFLAGS += --no-print-directory +#--------------------------------------------------------------------------------- +# any extra libraries we wish to link with the project +#--------------------------------------------------------------------------------- +LIBS := -lwups -lutils -ldynamiclibs +# + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(DEVKITPPC) + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- +export PROJECTDIR := $(CURDIR) +export OUTPUT := $(CURDIR)/$(TARGETDIR)/$(TARGET) +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) +export DEPSDIR := $(CURDIR)/$(BUILD) + +#--------------------------------------------------------------------------------- +# automatically build a list of object files for our project +#--------------------------------------------------------------------------------- +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) +TTFFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.ttf))) +PNGFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.png))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +ifeq ($(strip $(CPPFILES)),) + export LD_MOD := $(CC) +else + export LD_MOD := $(CXX) +endif + +export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \ + $(sFILES:.s=.o) $(SFILES:.S=.o) \ + $(PNGFILES:.png=.png.o) $(addsuffix .o,$(BINFILES)) + +#--------------------------------------------------------------------------------- +# build a list of include paths +#--------------------------------------------------------------------------------- +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I$(CURDIR)/$(BUILD) -I$(PORTLIBS)/include \ + -I$(PORTLIBS)/include/libutils -I$(WUPSDIR)/include + +#--------------------------------------------------------------------------------- +# build a list of library paths +#--------------------------------------------------------------------------------- +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ + -L$(PORTLIBS)/lib \ + -L$(WUPSDIR)/lib + +export OUTPUT := $(CURDIR)/$(TARGET) +.PHONY: $(BUILD) clean install + +#--------------------------------------------------------------------------------- +$(BUILD): + @[ -d $@ ] || mkdir -p $@ + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).bin $(BUILD_DBG).elf $(OUTPUT) + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +THIS_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) + +############################################################################### +# Rule to make everything. +PHONY += all + +all : $(OUTPUT) +############################################################################### +# Special build rules + +# Rule to make the module file. +$(OUTPUT) : output.elf + @echo "checking for missing symbols ..." + @$(LD_MOD) ../$(BUILD)/output.elf $(LDFLAG_COMMON) $(LIBS) $(LIBPATHS) -o check_linking.elf + @echo "linking ..." $@ + @$(LD_MOD) ../$(BUILD)/output.elf $(LDFLAGS_MOD) $(LIBS) $(LIBPATHS) -o $@ + +# Rule to make the module file. +output.elf : $(OFILES) + @echo "linking ... output.elf" + + + @$(LD) $(OFILES) $(LDFLAGS_ELF) $(LIBS) $(LIBPATHS) -o $@ + +############################################################################### +# Standard build rules +#--------------------------------------------------------------------------------- +%.a: +#--------------------------------------------------------------------------------- + @echo $(notdir $@) + @rm -f $@ + @$(AR) -rc $@ $^ + +#--------------------------------------------------------------------------------- +%.o: %.cpp + @echo $(notdir $<) + @$(CXX) -MMD -MP -MF $(DEPSDIR)/$*.d $(CXXFLAGS) $(INCLUDE) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.o: %.c + @echo $(notdir $<) + @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) $(INCLUDE) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.o: %.S + @echo $(notdir $<) + @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER) + +#--------------------------------------------------------------------------------- +%.png.o : %.png + @echo $(notdir $<) + @bin2s -a 32 $< | $(AS) -o $(@) + +#--------------------------------------------------------------------------------- +%.jpg.o : %.jpg + @echo $(notdir $<) + @bin2s -a 32 $< | $(AS) -o $(@) + +#--------------------------------------------------------------------------------- +%.ttf.o : %.ttf + @echo $(notdir $<) + @bin2s -a 32 $< | $(AS) -o $(@) + +#--------------------------------------------------------------------------------- +%.bin.o : %.bin + @echo $(notdir $<) + @bin2s -a 32 $< | $(AS) -o $(@) + +#--------------------------------------------------------------------------------- +%.wav.o : %.wav + @echo $(notdir $<) + @bin2s -a 32 $< | $(AS) -o $(@) + +#--------------------------------------------------------------------------------- +%.mp3.o : %.mp3 + @echo $(notdir $<) + @bin2s -a 32 $< | $(AS) -o $(@) + +#--------------------------------------------------------------------------------- +%.ogg.o : %.ogg + @echo $(notdir $<) + @bin2s -a 32 $< | $(AS) -o $(@) + +############################################################################### +# Assembly listing rules + +# Rule to make assembly listing. +PHONY += list +list : $(LIST) + +# Rule to make the listing file. +%.list : $(TARGET) + $(LOG) + -$Qmkdir -p $(dir $@) + $Q$(OBJDUMP) -d $< > $@ + +############################################################################### +# Clean rule + +# Rule to clean files. +PHONY += clean +clean : + $Qrm -rf $(wildcard $(BUILD) $(BIN)) + +############################################################################### +# Phony targets + +.PHONY : $(PHONY) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- diff --git a/plugins/memory_info/README.md b/plugins/memory_info/README.md new file mode 100644 index 0000000..5dcdd1d --- /dev/null +++ b/plugins/memory_info/README.md @@ -0,0 +1,13 @@ +# Memory info for the Wii U Plugin System + +Prints information about the current memory usage when pressing L+R+X+Y+A+B via the UDPReader + +## Building + +For building you need: +- [wups](https://github.com/Maschell/WiiUPluginSystem) +- [dynamic_libs](https://github.com/Maschell/dynamic_libs/tree/lib) for access to the functions. +- [libutils](https://github.com/Maschell/libutils) for common functions. + + +Install them (in this order) according to their README's. Don't forget the dependencies of the libs itself. \ No newline at end of file diff --git a/plugins/memory_info/src/c_retain_vars.c b/plugins/memory_info/src/c_retain_vars.c new file mode 100644 index 0000000..af87a4c --- /dev/null +++ b/plugins/memory_info/src/c_retain_vars.c @@ -0,0 +1,27 @@ +/**************************************************************************** + * Copyright (C) 2018 Maschell + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + ****************************************************************************/ +#include "c_retain_vars.h" + +memHeapInfo expHeapPointer[MAX_EXP_HEAP_INFOS] __attribute__((section(".data"))); +memHeapInfo frmHeapPointer[MAX_FRM_HEAP_INFOS] __attribute__((section(".data"))); +memUnitHeapInfo unitHeapPointer[MAX_UNIT_HEAP_INFOS] __attribute__((section(".data"))); + +baseHeapInfo baseHeaphandles[MAX_BASE_HEAP_INFOS] __attribute__((section(".data"))); +blockHeapInfo blockHeapInfos[MAX_BLOCK_HEAP_INFOS] __attribute__((section(".data"))); +memHeapInfo userHeapInfos[MAX_USER_HEAP_INFOS] __attribute__((section(".data"))); + +u8 gButtonComboCooldown __attribute__((section(".data"))) = 0; diff --git a/plugins/memory_info/src/c_retain_vars.h b/plugins/memory_info/src/c_retain_vars.h new file mode 100644 index 0000000..13077fe --- /dev/null +++ b/plugins/memory_info/src/c_retain_vars.h @@ -0,0 +1,32 @@ +/**************************************************************************** + * Copyright (C) 2018 Maschell + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + ****************************************************************************/ +#ifndef C_RETAINS_VARS_H_ +#define C_RETAINS_VARS_H_ + +#include "memory_defs.h" + +extern memHeapInfo expHeapPointer[MAX_EXP_HEAP_INFOS]; +extern memHeapInfo frmHeapPointer[MAX_FRM_HEAP_INFOS]; +extern memUnitHeapInfo unitHeapPointer[MAX_UNIT_HEAP_INFOS]; + +extern baseHeapInfo baseHeaphandles[MAX_BASE_HEAP_INFOS]; +extern blockHeapInfo blockHeapInfos[MAX_BLOCK_HEAP_INFOS]; +extern memHeapInfo userHeapInfos[MAX_USER_HEAP_INFOS]; + +extern u8 gButtonComboCooldown; + +#endif // C_RETAINS_VARS_H_ diff --git a/plugins/memory_info/src/common_hooks.c b/plugins/memory_info/src/common_hooks.c new file mode 100644 index 0000000..4704c3c --- /dev/null +++ b/plugins/memory_info/src/common_hooks.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include "main.h" + +DECL_FUNCTION(void, __PPCExit, void) { + // We reset them at ende, in case at application even uses the functions + // BEFORE with have our init hook. + memset(expHeapPointer,0,sizeof(expHeapPointer)); + memset(unitHeapPointer,0,sizeof(unitHeapPointer)); + memset(frmHeapPointer,0,sizeof(frmHeapPointer)); + memset(baseHeaphandles,0,sizeof(baseHeaphandles)); + memset(userHeapInfos,0,sizeof(userHeapInfos)); + real___PPCExit(); +} + +DECL_FUNCTION(int, VPADRead, int chan, VPADData *buffer, u32 buffer_size, s32 *error) { + int result = real_VPADRead(chan, buffer, buffer_size, error); + + int btns = VPAD_BUTTON_R | VPAD_BUTTON_L | VPAD_BUTTON_A | VPAD_BUTTON_B | VPAD_BUTTON_X | VPAD_BUTTON_Y; + if(result > 0 && ((buffer[0].btns_h & (btns)) == btns) && gButtonComboCooldown == 0 && OSIsHomeButtonMenuEnabled()) { + print_memory_info(); + gButtonComboCooldown = 0x3C; + } + if(gButtonComboCooldown > 0) { + gButtonComboCooldown--; + } + return result; +} + +WUPS_MUST_REPLACE(__PPCExit, WUPS_LOADER_LIBRARY_COREINIT, __PPCExit); +WUPS_MUST_REPLACE(VPADRead, WUPS_LOADER_LIBRARY_VPAD, VPADRead); diff --git a/plugins/memory_info/src/main.c b/plugins/memory_info/src/main.c new file mode 100644 index 0000000..2febb72 --- /dev/null +++ b/plugins/memory_info/src/main.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include "main.h" + +WUPS_PLUGIN_NAME("Memory Info"); +WUPS_PLUGIN_DESCRIPTION("Prints information about the current memory usage when pressing L+R+X+Y+A+B."); +WUPS_PLUGIN_VERSION("v1.0"); +WUPS_PLUGIN_AUTHOR("Maschell"); +WUPS_PLUGIN_LICENSE("GPL"); + + +INITIALIZE(args) { + InitOSFunctionPointers(); + InitSocketFunctionPointers(); + log_init(); + //InitVPadFunctionPointers(); +} + +void printMemHeapInfo(memHeapInfo * listStart, u32 listLength, const char * listname) { + for(int i = 0; i "); + } else { + log_printf(" parent: 0x%08X ", parent); + } + log_printf(" address 0x%08X handle 0x%08X size %07d kb old size %07d kb flags 0x%04X\n",listStart[i].address,listStart[i].handle,listStart[i].size/1024,listStart[i].oldSize/1024,listStart[i].flags); + } + } +} + +void print_memory_info() { + DEBUG_FUNCTION_LINE("Print expanded heap infos\n"); + printMemHeapInfo(expHeapPointer,MAX_EXP_HEAP_INFOS, "Expanded heap:"); + + DEBUG_FUNCTION_LINE("Print frmHeap\n"); + printMemHeapInfo(frmHeapPointer,MAX_FRM_HEAP_INFOS, "Frame heap:"); + + DEBUG_FUNCTION_LINE("Print unitHeap\n"); + for(int i = 0; i. + ****************************************************************************/ +#ifndef MAIN_H_ +#define MAIN_H_ + +#include "memory_defs.h" +#include "c_retain_vars.h" + +void print_memory_info(); + +#endif // MAIN_H_ diff --git a/plugins/memory_info/src/memory_defs.h b/plugins/memory_info/src/memory_defs.h new file mode 100644 index 0000000..d32a3c1 --- /dev/null +++ b/plugins/memory_info/src/memory_defs.h @@ -0,0 +1,64 @@ +/**************************************************************************** + * Copyright (C) 2018 Maschell + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + ****************************************************************************/ + +#ifndef _MEMORY_DEFS_H_ +#define _MEMORY_DEFS_H_ + +#include + +#define MAX_USER_HEAP_INFOS 50 +#define MAX_FRM_HEAP_INFOS 50 +#define MAX_EXP_HEAP_INFOS 50 +#define MAX_UNIT_HEAP_INFOS 50 +#define MAX_ALLOCATOR_EXP_INFOS 50 +#define MAX_BASE_HEAP_INFOS 50 +#define MAX_BLOCK_HEAP_INFOS 50 + +typedef struct memHeapInfo_ { + s32 handle; + void * address; + u32 size; + u32 oldSize; + u16 flags; +} memHeapInfo; + +typedef struct memUnitHeapInfo_ { + s32 handle; + void * address; + u32 heapSize; + u32 memSize; + u32 align; + u16 flags; +} memUnitHeapInfo; + +typedef struct baseHeapInfo_ { + s32 oldHandle; + s32 newHandle; + s32 memArena; +} baseHeapInfo; + +typedef struct blockHeapInfo_ { + s32 handle; + void * tracking; + void * startAddress; + void * endAddress; + void * initTrackMem; + u32 initTrackMemBytes; + u16 flags; +} blockHeapInfo; + +#endif diff --git a/plugins/memory_info/src/memory_hooks.c b/plugins/memory_info/src/memory_hooks.c new file mode 100644 index 0000000..a5e7b87 --- /dev/null +++ b/plugins/memory_info/src/memory_hooks.c @@ -0,0 +1,244 @@ +#include +#include +#include "memory_defs.h" +#include "c_retain_vars.h" + +DECL_FUNCTION(s32, MEMCreateFrmHeapEx, void* address, u32 size, u16 flags) { + s32 result = real_MEMCreateFrmHeapEx(address,size,flags); + for(int i = 0; i 0x3B600000 && (u32) address < 0x11000000){ + //We are pretty sure this the MEM2 default heap. + //Let's try to increase it's capacity. + //u32 maximumSize = 0x50000000 - (u32) address; + u32 maximumSize = 0x80000000; + //u32 maximumSize = size + 0x500000; + maximumSize -= 0x500000; // remove 5MB, just to leave at least _some_ space. + + u32 curSizeTest = maximumSize; + + do{ + if(curSizeTest < oldSize){ + break; + } + result = real_MEMCreateExpHeapEx(address,curSizeTest,flags); + if(result == 0){ // On failure: + maximumSize -= 0x100000; // reduce by 1MB and try again. + }else{ + allocated = true; + break; + } + }while(!allocated); // Until the allocation was successful, or we are below default size. + }*/ + if(!allocated || result == 0) { + result = real_MEMCreateExpHeapEx(address,size,flags); + } + + for(int i = 0; i