Changeset 3565 for trunk/kStuff/kProfile/kProfileR3.cpp
- Timestamp:
- Aug 27, 2007, 6:06:17 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kStuff/kProfile/kProfileR3.cpp
r3537 r3565 1 /* $Id: $ */ 2 /** @file 3 * kProfiler Mark 2 - The Ring-3 Implementation. 4 */ 5 6 /* 7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net> 8 * 9 * This file is part of kProfiler. 10 * 11 * kProfiler is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU Lesser General Public 13 * License as published by the Free Software Foundation; either 14 * version 2.1 of the License, or (at your option) any later version. 15 * 16 * kProfiler is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * Lesser General Public License for more details. 20 * 21 * You should have received a copy of the GNU Lesser General Public 22 * License along with kProfiler; if not, write to the Free Software 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 * 25 */ 1 26 2 27 … … 8 33 # include <psapi.h> 9 34 # include <malloc.h> 10 typedef unsigned char uint8_t;11 typedef signed char int8_t;12 typedef unsigned short uint16_t;13 typedef signed short int16_t;14 typedef unsigned int uint32_t;15 typedef signed int int32_t;16 typedef unsigned _int64 uint64_t;17 typedef signed _int64 int64_t;18 typedef size_t uintptr_t;19 35 # if _MSC_VER >= 1400 20 36 # include <intrin.h> … … 45 61 #endif 46 62 63 #include <k/kDefs.h> 64 #include <k/kTypes.h> 47 65 48 66 /* … … 100 118 /** Update the state. */ 101 119 #define KPRF_RWLOCK_SETSTATE(pRWLock, enmNewState) \ 102 kPrfAtomicSet32((volatile uint32_t *)&(pRWLock)->enmState, (uint32_t)(enmNewState))120 kPrfAtomicSet32((volatile KU32 *)&(pRWLock)->enmState, (KU32)(enmNewState)) 103 121 104 122 /** Read/Write lock type. */ … … 109 127 KPRF_TYPE(,MUTEX) Mutex; 110 128 /** The current number of readers. */ 111 uint32_tcReaders;129 KU32 cReaders; 112 130 /** The number of readers waiting. */ 113 uint32_tcReadersWaiting;131 KU32 cReadersWaiting; 114 132 /** The current number of waiting writers. */ 115 uint32_tcWritersWaiting;133 KU32 cWritersWaiting; 116 134 # if defined(KPRF_OS_WINDOWS) 117 135 /** The handle of the event object on which the waiting readers block. (manual reset). */ … … 218 236 * @returns The thread id. 219 237 */ 220 static inline uintptr_tkPrfGetThreadId(void)238 static inline KUPTR kPrfGetThreadId(void) 221 239 { 222 240 /* Win32/64 */ 223 241 #if defined(KPRF_OS_WINDOWS) 224 uintptr_t ThreadId = (uintptr_t)GetCurrentThreadId();242 KUPTR ThreadId = (KUPTR)GetCurrentThreadId(); 225 243 226 244 /* Posix Threads */ 227 245 #elif defined(KPRF_USE_PTHREAD) 228 uintptr_t ThreadId = (uintptr_t)pthread_self();246 KUPTR ThreadId = (KUPTR)pthread_self(); 229 247 230 248 #elif defined(KPRF_OS_OS2) … … 248 266 * @returns The process id. 249 267 */ 250 static inline uintptr_tkPrfGetProcessId(void)268 static inline KUPTR kPrfGetProcessId(void) 251 269 { 252 270 /* Win32/64 */ 253 271 #if defined(KPRF_OS_WINDOWS) 254 uintptr_t ThreadId = (uintptr_t)GetProcessId(GetCurrentProcess());272 KUPTR ThreadId = (KUPTR)GetProcessId(GetCurrentProcess()); 255 273 256 274 #elif defined(KPRF_OS_OS2) … … 261 279 262 280 #else 263 uintptr_t ThreadId = (uintptr_t)getpid();281 KUPTR ThreadId = (KUPTR)getpid(); 264 282 #endif 265 283 … … 302 320 * This must correspond to what the assembly code are doing. 303 321 */ 304 static inline uint64_tkPrfNow(void)322 static inline KU64 kPrfNow(void) 305 323 { 306 324 #if defined(HAVE_INTRIN) … … 309 327 union 310 328 { 311 uint64_tu64;329 KU64 u64; 312 330 struct 313 331 { 314 uint32_tu32Lo;315 uint32_tu32Hi;332 KU32 u32Lo; 333 KU32 u32Hi; 316 334 } s; 317 335 } u; … … 336 354 * Atomically set a 32-bit value. 337 355 */ 338 static inline void kPrfAtomicSet32(volatile uint32_t *pu32, const uint32_tu32)356 static inline void kPrfAtomicSet32(volatile KU32 *pu32, const KU32 u32) 339 357 { 340 358 #if defined(HAVE_INTRIN) … … 365 383 * Atomically set a 64-bit value. 366 384 */ 367 static inline void kPrfAtomicSet64(volatile uint64_t *pu64, uint64_tu64)385 static inline void kPrfAtomicSet64(volatile KU64 *pu64, KU64 u64) 368 386 { 369 387 #if defined(HAVE_INTRIN) && KPRF_BITS == 64 370 _InterlockedExchange64(( int64_t *)pu64, (const int64_t)u64);388 _InterlockedExchange64((KI64 *)pu64, (const KI64)u64); 371 389 372 390 #elif defined(__GNUC__) && KPRF_BITS == 64 … … 382 400 "=m" (*pu64) 383 401 : "0" (*pu64), 384 "b" ( ( uint32_t)u64 ),385 "c" ( ( uint32_t)(u64 >> 32) ));402 "b" ( (KU32)u64 ), 403 "c" ( (KU32)(u64 >> 32) )); 386 404 387 405 #elif _MSC_VER … … 407 425 * Atomically add a 32-bit integer to another. 408 426 */ 409 static inline void kPrfAtomicAdd32(volatile uint32_t *pu32, const uint32_tu32)427 static inline void kPrfAtomicAdd32(volatile KU32 *pu32, const KU32 u32) 410 428 { 411 429 #if defined(HAVE_INTRIN) … … 431 449 #define KPRF_ATOMIC_ADD32(a,b) kPrfAtomicAdd32(a, b) 432 450 #define KPRF_ATOMIC_INC32(a) kPrfAtomicAdd32(a, 1); 433 #define KPRF_ATOMIC_DEC32(a) kPrfAtomicAdd32(a, ( uint32_t)-1);451 #define KPRF_ATOMIC_DEC32(a) kPrfAtomicAdd32(a, (KU32)-1); 434 452 435 453 … … 438 456 * Atomically isn't quite required, just a non-corruptive manner, assuming all updates are adds. 439 457 */ 440 static inline void kPrfAtomicAdd64(volatile uint64_t *pu64, const uint64_tu64)458 static inline void kPrfAtomicAdd64(volatile KU64 *pu64, const KU64 u64) 441 459 { 442 460 #if defined(HAVE_INTRIN) && KPRF_BITS == 64 443 _InterlockedExchangeAdd64((volatile int64_t *)pu64, (const int64_t)u64);461 _InterlockedExchangeAdd64((volatile KI64 *)pu64, (const KI64)u64); 444 462 445 463 #elif defined(__GNUC__) && KPRF_BITS == 64 … … 451 469 __asm__ __volatile__("lock; addl %0, %2\n\t" 452 470 "lock; adcl %1, %3\n\t" 453 : "=m" (*(volatile uint32_t*)pu64),454 "=m" (*((volatile uint32_t*)pu64 + 1))455 : "r" (( uint32_t)u64),456 "r" (( uint32_t)(u64 >> 32)));471 : "=m" (*(volatile KU32 *)pu64), 472 "=m" (*((volatile KU32 *)pu64 + 1)) 473 : "r" ((KU32)u64), 474 "r" ((KU32)(u64 >> 32))); 457 475 458 476 #elif _MSC_VER … … 922 940 * 923 941 */ 924 static int kPrfGetModSeg(KPRF_TYPE(,UPTR) uAddress, char *pszPath, uint32_t cchPath, uint32_t*piSegment,942 static int kPrfGetModSeg(KPRF_TYPE(,UPTR) uAddress, char *pszPath, KU32 cchPath, KU32 *piSegment, 925 943 KPRF_TYPE(P,UPTR) puBasePtr, KPRF_TYPE(P,UPTR) pcbSegmentMinusOne) 926 944 { … … 945 963 __try 946 964 { 947 const uintptr_t uImageBase = (uintptr_t)pahModules[i];965 const KUPTR uImageBase = (KUPTR)pahModules[i]; 948 966 union 949 967 { 950 uint8_t*pu8;968 KU8 *pu8; 951 969 PIMAGE_DOS_HEADER pDos; 952 970 PIMAGE_NT_HEADERS pNt; 953 971 PIMAGE_NT_HEADERS32 pNt32; 954 972 PIMAGE_NT_HEADERS64 pNt64; 955 uintptr_tu;973 KUPTR u; 956 974 } u; 957 975 u.u = uImageBase; … … 970 988 971 989 /* Extract necessary info from the optional header (comes in 32-bit and 64-bit variations, we simplify a bit). */ 972 uint32_tcbImage;990 KU32 cbImage; 973 991 PIMAGE_SECTION_HEADER paSHs; 974 992 if (u.pNt->FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32)) … … 986 1004 987 1005 /* Is our address within the image size */ 988 uintptr_t uRVA = uAddress - (uintptr_t)pahModules[i];1006 KUPTR uRVA = uAddress - (KUPTR)pahModules[i]; 989 1007 if (uRVA >= cbImage) 990 1008 continue; … … 994 1012 * (segment == section + 1) 995 1013 */ 996 const uint32_tcSHs = u.pNt->FileHeader.NumberOfSections;1014 const KU32 cSHs = u.pNt->FileHeader.NumberOfSections; 997 1015 if (uRVA < paSHs[0].VirtualAddress) 998 1016 { … … 1004 1022 else 1005 1023 { 1006 uint32_tiSH = 0;1024 KU32 iSH = 0; 1007 1025 for (;;) 1008 1026 { … … 1063 1081 ULONG cb = ~0UL; 1064 1082 ULONG fFlags = ~0UL; 1065 uAddress &= ~( uintptr_t)0xfff;1083 uAddress &= ~(KUPTR)0xfff; 1066 1084 rc = DosQueryMem((PVOID)(uAddress, &cb, &fFlags); 1067 1085 if (!rc) 1068 1086 { 1069 *pcbSegmentMinusOne = (offObj & ~( uintptr_t)0xfff) + KPRF_ALIGN(cb, 0x1000) - 1;1087 *pcbSegmentMinusOne = (offObj & ~(KUPTR)0xfff) + KPRF_ALIGN(cb, 0x1000) - 1; 1070 1088 if ((fFlags & PAG_BASE) && cb <= 0x1000) /* don't quite remember if PAG_BASE returns one page or not */ 1071 1089 { … … 1118 1136 static KPRF_TYPE(P,THREAD) kPrfGetThreadAutoReg(void) 1119 1137 { 1120 uintptr_tuStackBasePtr;1138 KUPTR uStackBasePtr; 1121 1139 1122 1140 #if 0 … … 1129 1147 DosGetInfoBlocks(&pTib, &pPib); /* never fails except if you give it bad input, thus 'Get' not 'Query'. */ 1130 1148 /* I never recall which of these is the right one... */ 1131 uStackBasePtr = ( uintptr_t)pTib->tib_pstack < (uintptr_t)pTib->tib_pstack_limit1132 ? ( uintptr_t)pTib->tib_pstack1133 : ( uintptr_t)pTib->tib_pstack_limit;1149 uStackBasePtr = (KUPTR)pTib->tib_pstack < (KUPTR)pTib->tib_pstack_limit 1150 ? (KUPTR)pTib->tib_pstack 1151 : (KUPTR)pTib->tib_pstack_limit; 1134 1152 1135 1153 #else 1136 1154 /* the default is top of the current stack page (assuming a page to be 4KB) */ 1137 uStackBasePtr = ( uintptr_t)&uStackBasePtr;1138 uStackBasePtr = (uStackBasePtr + 0xfff) & ~( uintptr_t)0xfff;1155 uStackBasePtr = (KUPTR)&uStackBasePtr; 1156 uStackBasePtr = (uStackBasePtr + 0xfff) & ~(KUPTR)0xfff; 1139 1157 #endif 1140 1158 … … 1152 1170 * @param pszDefault The default value. 1153 1171 */ 1154 static char *kPrfGetEnvString(const char *pszVar, char *pszValue, uint32_tcchValue, const char *pszDefault)1172 static char *kPrfGetEnvString(const char *pszVar, char *pszValue, KU32 cchValue, const char *pszDefault) 1155 1173 { 1156 1174 #if defined(KPRF_OS_WINDOWS) … … 1191 1209 * @param uDefault The default value. 1192 1210 */ 1193 static uint32_t kPrfGetEnvValue(const char *pszVar, uint32_tuDefault)1211 static KU32 kPrfGetEnvValue(const char *pszVar, KU32 uDefault) 1194 1212 { 1195 1213 #if defined(KPRF_OS_WINDOWS) … … 1223 1241 */ 1224 1242 unsigned uBase = 10; 1225 uint32_tuValue = 0;1243 KU32 uValue = 0; 1226 1244 const char *psz = pszValue; 1227 1245 … … 1279 1297 * @param cb The amount of memory (in bytes) to allocate. 1280 1298 */ 1281 static void *kPrfAllocMem( uint32_tcb)1299 static void *kPrfAllocMem(KU32 cb) 1282 1300 { 1283 1301 #if defined(KPRF_OS_WINDOWS) … … 1342 1360 * @param cbData The amount of data to write. 1343 1361 */ 1344 static int kPrfWriteFile(const char *pszName, const void *pvData, uint32_tcbData)1362 static int kPrfWriteFile(const char *pszName, const void *pvData, KU32 cbData) 1345 1363 { 1346 1364 #if defined(KPRF_OS_WINDOWS) … … 1412 1430 * Initial suggestions. 1413 1431 */ 1414 uint32_tcbModSegs = kPrfGetEnvValue("KPRF2_CBMODSEGS", 128*1024);1415 uint32_tcFunctions = kPrfGetEnvValue("KPRF2_CFUNCTIONS", 8192);1416 uint32_tcThreads = kPrfGetEnvValue("KPRF2_CTHREADS", 256);1417 uint32_tcStacks = kPrfGetEnvValue("KPRF2_CSTACKS", 48);1418 uint32_tcFrames = kPrfGetEnvValue("KPRF2_CFRAMES", 448);1419 1420 uint32_tcb = KPRF_NAME(CalcSize)(cFunctions, cbModSegs, cThreads, cStacks, cFrames);1432 KU32 cbModSegs = kPrfGetEnvValue("KPRF2_CBMODSEGS", 128*1024); 1433 KU32 cFunctions = kPrfGetEnvValue("KPRF2_CFUNCTIONS", 8192); 1434 KU32 cThreads = kPrfGetEnvValue("KPRF2_CTHREADS", 256); 1435 KU32 cStacks = kPrfGetEnvValue("KPRF2_CSTACKS", 48); 1436 KU32 cFrames = kPrfGetEnvValue("KPRF2_CFRAMES", 448); 1437 1438 KU32 cb = KPRF_NAME(CalcSize)(cFunctions, cbModSegs, cThreads, cStacks, cFrames); 1421 1439 1422 1440 /* … … 1548 1566 1549 1567 /* append the process id */ 1550 uintptr_tpid = kPrfGetProcessId();1568 KUPTR pid = kPrfGetProcessId(); 1551 1569 char *psz = szName; 1552 1570 while (*psz) … … 1554 1572 1555 1573 static char s_szDigits[0x11] = "0123456789abcdef"; 1556 uint32_tuShift = KPRF_BITS - 4;1574 KU32 uShift = KPRF_BITS - 4; 1557 1575 while ( uShift > 0 1558 1576 && !(pid & (0xf << uShift)))
Note:
See TracChangeset
for help on using the changeset viewer.