this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Trampoline improvements for apps using fork()

+77 -11
+63 -10
src/dyld/Trampoline.cpp
··· 14 14 15 15 extern "C" void reg_saveall(); 16 16 extern "C" void reg_restoreall(); 17 + extern char** g_argv; 17 18 18 19 TrampolineMgr::TrampolineMgr(int entries) 19 20 : m_nNext(0) ··· 29 30 30 31 mem = ::mmap(0, bytes, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); 31 32 if (mem == MAP_FAILED) 32 - throw std::runtime_error("Failed to map pages for TrampolineMgr"); 33 + throw std::runtime_error("Failed to map pages for TrampolineMgr"); 33 34 34 35 m_pMem = static_cast<Trampoline*>(mem); 35 36 m_nMax = bytes / sizeof(Trampoline); 37 + 38 + if (char* p = get_current_dir_name()) 39 + { 40 + m_wd = p; 41 + free(p); 42 + } 36 43 44 + std::cout << logPath() << std::endl; 37 45 } 38 46 39 47 TrampolineMgr::~TrampolineMgr() ··· 119 127 } 120 128 } 121 129 130 + std::string TrampolineMgr::timeStamp() 131 + { 132 + char buf[50]; 133 + time_t t = time(0); 134 + struct tm tm; 135 + 136 + localtime_r(&t, &tm); 137 + strftime(buf, sizeof(buf), "%H:%M:%S", &tm); 138 + 139 + return buf; 140 + } 141 + 142 + std::string TrampolineMgr::logPath() 143 + { 144 + pid_t pid = getpid(); 145 + std::stringstream ss; 146 + std::string progname = g_argv[0]; 147 + size_t pos; 148 + 149 + pos = progname.rfind('/'); 150 + if (pos != std::string::npos) 151 + progname = progname.substr(pos+1); 152 + 153 + ss << m_wd << '/' << progname << '.' << pid << ".log"; 154 + return ss.str(); 155 + } 156 + 157 + bool TrampolineMgr::openLog(std::ofstream& stream) 158 + { 159 + stream.open(logPath(), std::ios_base::out | std::ios_base::app); 160 + return stream.is_open(); 161 + } 162 + 122 163 void* TrampolineMgr::printInfo(uint32_t index, CallStack* stack) 123 164 { 124 165 FunctionInfo* info = 0; 125 166 const std::string& name = m_pInstance->m_entries[index].name; 126 167 auto it = m_functionInfo.find(name); 168 + std::ofstream logFile; 169 + std::ostream* out = &logFile; 170 + 171 + if (!m_pInstance->openLog(logFile)) 172 + out = &(*out); 127 173 128 - std::cerr << std::string(m_nDepth, ' '); 174 + (*out) << std::string(m_nDepth, ' '); 175 + (*out) << '[' << timeStamp() << "] "; 129 176 130 177 if (it != m_functionInfo.end()) 131 178 { 132 179 ArgumentWalker w(stack); 133 180 bool first = true; 134 181 135 - std::cerr << name << '('; 182 + (*out) << name << '('; 136 183 137 184 for (char c : it->second.arguments) 138 185 { 139 186 if (!first) 140 - std::cerr << ", "; 187 + (*out) << ", "; 141 188 else 142 189 first = false; 143 190 144 - std::cerr << w.next(c); 191 + (*out) << w.next(c); 145 192 } 146 - std::cerr << ")\n" << std::flush; 193 + (*out) << ")\n" << std::flush; 147 194 } 148 195 else 149 - std::cerr << m_pInstance->m_entries[index].name << "(?)\n" << std::flush; 196 + (*out) << m_pInstance->m_entries[index].name << "(?)\n" << std::flush; 150 197 m_pInstance->m_entries[index].retAddr = stack->retAddr; 151 198 152 199 m_nDepth++; ··· 157 204 void* TrampolineMgr::printInfoR(uint32_t index, CallStack* stack) 158 205 { 159 206 void* rv = m_pInstance->m_entries[index].retAddr; 207 + std::ofstream logFile; 208 + std::ostream* out = &logFile; 209 + 210 + if (!m_pInstance->openLog(logFile)) 211 + out = &(*out); 160 212 161 213 m_pInstance->m_entries[index].retAddr = 0; 162 214 m_nDepth--; ··· 164 216 const std::string& name = m_pInstance->m_entries[index].name; 165 217 auto it = m_functionInfo.find(name); 166 218 167 - std::cerr << std::string(m_nDepth, ' '); 219 + (*out) << std::string(m_nDepth, ' '); 220 + (*out) << '[' << timeStamp() << "] "; 168 221 169 222 if (it != m_functionInfo.end()) 170 223 { 171 224 ArgumentWalker w(stack); 172 - std::cerr << "-> " << w.ret(it->second.retType) << '\n' << std::flush; 225 + (*out) << "-> " << w.ret(it->second.retType) << '\n' << std::flush; 173 226 } 174 227 else 175 - std::cerr << "-> ?\n" << std::flush; 228 + (*out) << "-> ?\n" << std::flush; 176 229 177 230 // standard retval in rax, double in xmm0 178 231 return rv;
+6 -1
src/dyld/Trampoline.h
··· 5 5 #include <string> 6 6 #include <list> 7 7 #include <map> 8 + #include <fstream> 8 9 9 10 struct Trampoline; 10 11 ··· 24 25 struct CallStack 25 26 { 26 27 long double xmm[8]; // xmm7-xmm0 27 - uint64_t r9, r8, rcx, rdx, rsi, rdi, rbx, rax; 28 + uint64_t r15, r14, r13, r12, r9, r8, rcx, rdx, rsi, rdi, rbx, rax; 28 29 void* retAddr; 29 30 }; 30 31 #pragma pack() 31 32 private: 32 33 void loadMemoryMap(); 33 34 35 + std::string logPath(); 36 + bool openLog(std::ofstream& stream); 37 + static std::string timeStamp(); 34 38 static void* printInfo(uint32_t index, CallStack* stack); 35 39 static void* printInfoR(uint32_t index, CallStack* stack); 36 40 private: ··· 81 85 82 86 std::vector<AddrEntry> m_entries; 83 87 std::list<MemoryPages> m_memoryMap; 88 + std::string m_wd; 84 89 static int m_nDepth; 85 90 static std::map<std::string, FunctionInfo> m_functionInfo; 86 91 };
+8
src/dyld/trampoline_helper.nasm
··· 15 15 push rcx 16 16 push r8 17 17 push r9 18 + push r12 19 + push r13 20 + push r14 21 + push r15 18 22 sub rsp, 128 ; 8*16 19 23 movdqu [rsp], xmm0 20 24 movdqu [rsp+16], xmm1 ··· 37 41 movdqu xmm1, [rsp+16] 38 42 movdqu xmm0, [rsp] 39 43 add rsp, 128 44 + pop r15 45 + pop r14 46 + pop r13 47 + pop r12 40 48 pop r9 41 49 pop r8 42 50 pop rcx