Implement GetMacosVersion() to obtain the OS X version at runtime.

llvm-svn: 149382
This commit is contained in:
Alexander Potapenko 2012-01-31 13:19:18 +00:00
parent e76b42b298
commit 81203bdb33
2 changed files with 34 additions and 0 deletions

View File

@ -27,6 +27,7 @@
#include <mach-o/loader.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/ucontext.h>
#include <pthread.h>
#include <fcntl.h>
@ -58,6 +59,28 @@ void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) {
# endif // __WORDSIZE
}
int GetMacosVersion() {
int mib[2] = { CTL_KERN, KERN_OSRELEASE };
char version[100];
size_t len = 0, maxlen = sizeof(version) / sizeof(version[0]);
for (int i = 0; i < maxlen; i++) version[i] = '\0';
// Get the version length.
CHECK(sysctl(mib, 2, NULL, &len, NULL, 0) != -1);
CHECK(len < maxlen);
CHECK(sysctl(mib, 2, version, &len, NULL, 0) != -1);
switch (version[0]) {
case '9': return MACOS_VERSION_LEOPARD;
case '1': {
switch (version[1]) {
case '0': return MACOS_VERSION_SNOW_LEOPARD;
case '1': return MACOS_VERSION_LION;
default: return MACOS_VERSION_UNKNOWN;
}
}
default: return MACOS_VERSION_UNKNOWN;
}
}
// No-op. Mac does not support static linkage anyway.
void *AsanDoesNotSupportStaticLinkage() {
return NULL;

View File

@ -24,6 +24,17 @@
#include <setjmp.h>
#include <CoreFoundation/CFString.h>
enum {
MACOS_VERSION_UNKNOWN = 0,
MACOS_VERSION_LEOPARD,
MACOS_VERSION_SNOW_LEOPARD,
MACOS_VERSION_LION,
};
namespace __asan {
int GetMacosVersion();
}
typedef void* pthread_workqueue_t;
typedef void* pthread_workitem_handle_t;