Windows porting: port hyperscan and chimera tools to windows.

This commit is contained in:
Lu, Qi
2018-05-08 12:05:44 -04:00
committed by Wang, Xiang W
parent bf87f8c003
commit 5a0885d235
34 changed files with 563 additions and 109 deletions

View File

@@ -1,6 +1,3 @@
if (WIN32)
return()
endif()
find_package(Threads)
# remove some warnings
@@ -12,11 +9,18 @@ include_directories(${PROJECT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/util)
# add any subdir with a cmake file
file(GLOB dirents RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
foreach(e ${dirents})
if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${e} AND
EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${e}/CMakeLists.txt)
add_subdirectory(${e})
endif ()
endforeach ()
if (WIN32)
add_subdirectory(hscheck)
add_subdirectory(hsbench)
add_subdirectory(hsdump)
add_subdirectory(hscollider)
else()
# add any subdir with a cmake file
file(GLOB dirents RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
foreach(e ${dirents})
if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${e} AND
EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${e}/CMakeLists.txt)
add_subdirectory(${e})
endif ()
endforeach ()
endif()

View File

@@ -61,8 +61,13 @@ endif()
add_executable(hsbench ${hsbench_SOURCES})
if (BUILD_CHIMERA)
include_directories(${PCRE_INCLUDE_DIRS})
target_link_libraries(hsbench hs chimera ${PCRE_LDFLAGS} databaseutil
expressionutil ${SQLITE3_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT})
if(NOT WIN32)
target_link_libraries(hsbench hs chimera ${PCRE_LDFLAGS} databaseutil
expressionutil ${SQLITE3_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT})
else()
target_link_libraries(hsbench hs chimera pcre databaseutil
expressionutil ${SQLITE3_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT})
endif()
else()
target_link_libraries(hsbench hs databaseutil expressionutil
${SQLITE3_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT})

View File

@@ -68,8 +68,9 @@ struct ScanCHContext {
* "echo matches" is off.
*/
static
int onMatch(unsigned int, unsigned long long, unsigned long long, unsigned int,
unsigned int, const ch_capture_t *, void *ctx) {
int HS_CDECL onMatch(unsigned int, unsigned long long, unsigned long long,
unsigned int, unsigned int, const ch_capture_t *,
void *ctx) {
ScanCHContext *sc = static_cast<ScanCHContext *>(ctx);
assert(sc);
sc->result.matches++;
@@ -82,8 +83,9 @@ int onMatch(unsigned int, unsigned long long, unsigned long long, unsigned int,
* matches" is enabled.
*/
static
int onMatchEcho(unsigned int id, unsigned long long, unsigned long long to,
unsigned int, unsigned int, const ch_capture_t *, void *ctx) {
int HS_CDECL onMatchEcho(unsigned int id, unsigned long long,
unsigned long long to, unsigned int, unsigned int,
const ch_capture_t *, void *ctx) {
ScanCHContext *sc = static_cast<ScanCHContext *>(ctx);
assert(sc);
sc->result.matches++;
@@ -166,12 +168,23 @@ void EngineChimera::printStats() const {
}
printf("Signatures: %s\n", compile_stats.signatures.c_str());
printf("Chimera info: %s\n", compile_stats.db_info.c_str());
#ifndef _WIN32
printf("Expression count: %'zu\n", compile_stats.expressionCount);
printf("Bytecode size: %'zu bytes\n", compile_stats.compiledSize);
#else
printf("Expression count: %zu\n", compile_stats.expressionCount);
printf("Bytecode size: %zu bytes\n", compile_stats.compiledSize);
#endif
printf("Database CRC: 0x%x\n", compile_stats.crc32);
#ifndef _WIN32
printf("Scratch size: %'zu bytes\n", compile_stats.scratchSize);
printf("Compile time: %'0.3Lf seconds\n", compile_stats.compileSecs);
printf("Peak heap usage: %'u bytes\n", compile_stats.peakMemorySize);
#else
printf("Scratch size: %zu bytes\n", compile_stats.scratchSize);
printf("Compile time: %0.3Lf seconds\n", compile_stats.compileSecs);
printf("Peak heap usage: %u bytes\n", compile_stats.peakMemorySize);
#endif
}
void EngineChimera::sqlStats(SqlDB &sqldb) const {

View File

@@ -87,8 +87,8 @@ struct ScanHSContext {
* "echo matches" is off.
*/
static
int onMatch(unsigned int, unsigned long long, unsigned long long, unsigned int,
void *ctx) {
int HS_CDECL onMatch(unsigned int, unsigned long long,
unsigned long long, unsigned int, void *ctx) {
ScanHSContext *sc = static_cast<ScanHSContext *>(ctx);
assert(sc);
sc->result.matches++;
@@ -101,8 +101,8 @@ int onMatch(unsigned int, unsigned long long, unsigned long long, unsigned int,
* matches" is enabled.
*/
static
int onMatchEcho(unsigned int id, unsigned long long, unsigned long long to,
unsigned int, void *ctx) {
int HS_CDECL onMatchEcho(unsigned int id, unsigned long long,
unsigned long long to, unsigned int, void *ctx) {
ScanHSContext *sc = static_cast<ScanHSContext *>(ctx);
assert(sc);
sc->result.matches++;
@@ -250,15 +250,30 @@ void EngineHyperscan::printStats() const {
}
printf("Signatures: %s\n", compile_stats.signatures.c_str());
printf("Hyperscan info: %s\n", compile_stats.db_info.c_str());
#ifndef _WIN32
printf("Expression count: %'zu\n", compile_stats.expressionCount);
printf("Bytecode size: %'zu bytes\n", compile_stats.compiledSize);
#else
printf("Expression count: %zu\n", compile_stats.expressionCount);
printf("Bytecode size: %zu bytes\n", compile_stats.compiledSize);
#endif
printf("Database CRC: 0x%x\n", compile_stats.crc32);
if (compile_stats.streaming) {
#ifndef _WIN32
printf("Stream state size: %'zu bytes\n", compile_stats.streamSize);
#else
printf("Stream state size: %zu bytes\n", compile_stats.streamSize);
#endif
}
#ifndef _WIN32
printf("Scratch size: %'zu bytes\n", compile_stats.scratchSize);
printf("Compile time: %'0.3Lf seconds\n", compile_stats.compileSecs);
printf("Peak heap usage: %'u bytes\n", compile_stats.peakMemorySize);
#else
printf("Scratch size: %zu bytes\n", compile_stats.scratchSize);
printf("Compile time: %0.3Lf seconds\n", compile_stats.compileSecs);
printf("Peak heap usage: %u bytes\n", compile_stats.peakMemorySize);
#endif
}
void EngineHyperscan::sqlStats(SqlDB &sqldb) const {

View File

@@ -26,6 +26,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef _WIN32
#define PCRE_STATIC
#endif
#include "config.h"
#include "common.h"
@@ -38,6 +41,8 @@
#include "util/make_unique.h"
#include "util/unicode_def.h"
#include <algorithm>
using namespace std;
EnginePCREContext::EnginePCREContext(int capture_cnt) {
@@ -207,11 +212,19 @@ void EnginePCRE::printStats() const {
}
printf("Signatures: %s\n", compile_stats.signatures.c_str());
printf("PCRE info: %s\n", compile_stats.db_info.c_str());
#ifndef _WIN32
printf("Expression count: %'zu\n", compile_stats.expressionCount);
printf("Bytecode size: %'zu bytes\n", compile_stats.compiledSize);
printf("Scratch size: %'zu bytes\n", compile_stats.scratchSize);
printf("Compile time: %'0.3Lf seconds\n", compile_stats.compileSecs);
printf("Peak heap usage: %'u bytes\n", compile_stats.peakMemorySize);
#else
printf("Expression count: %zu\n", compile_stats.expressionCount);
printf("Bytecode size: %zu bytes\n", compile_stats.compiledSize);
printf("Scratch size: %zu bytes\n", compile_stats.scratchSize);
printf("Compile time: %0.3Lf seconds\n", compile_stats.compileSecs);
printf("Peak heap usage: %u bytes\n", compile_stats.peakMemorySize);
#endif
}
void EnginePCRE::sqlStats(SqlDB &sqldb) const {

View File

@@ -58,7 +58,11 @@
#include <set>
#include <thread>
#ifndef _WIN32
#include <getopt.h>
#else
#include "win_getopt.h"
#endif
#ifndef _WIN32
#include <pthread.h>
#if defined(HAVE_PTHREAD_NP_H)
@@ -138,6 +142,16 @@ public:
// Apply processor affinity (if available) to this thread.
bool affine(UNUSED int cpu) {
#if defined(_WIN32)
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
assert(cpu >= 0 && (DWORD)cpu < system_info.dwNumberOfProcessors);
DWORD_PTR mask = 1 << cpu;
DWORD_PTR rv = SetThreadAffinityMask(thr.native_handle(), mask);
return rv != 0;
#endif
#ifdef HAVE_DECL_PTHREAD_SETAFFINITY_NP
#if defined(__FreeBSD__)
cpuset_t cpuset;
@@ -191,7 +205,7 @@ void usage(const char *error) {
printf(" -H Benchmark using Chimera (if supported).\n");
printf(" -P Benchmark using PCRE (if supported).\n");
#endif
#ifdef HAVE_DECL_PTHREAD_SETAFFINITY_NP
#if defined(HAVE_DECL_PTHREAD_SETAFFINITY_NP) || defined(_WIN32)
printf(" -T CPU,CPU,... Benchmark with threads on these CPUs.\n");
#endif
printf(" -i DIR Don't compile, load from files in DIR"
@@ -225,7 +239,7 @@ static
void processArgs(int argc, char *argv[], vector<BenchmarkSigs> &sigSets,
UNUSED unique_ptr<Grey> &grey) {
const char options[] = "-b:c:Cd:e:E:G:hHi:n:No:p:PsS:Vw:z:"
#ifdef HAVE_DECL_PTHREAD_SETAFFINITY_NP
#if defined(HAVE_DECL_PTHREAD_SETAFFINITY_NP) || defined(_WIN32)
"T:" // add the thread flag
#endif
;
@@ -332,7 +346,7 @@ void processArgs(int argc, char *argv[], vector<BenchmarkSigs> &sigSets,
case 'S':
sigName.assign(optarg);
break;
#ifdef HAVE_DECL_PTHREAD_SETAFFINITY_NP
#if defined(HAVE_DECL_PTHREAD_SETAFFINITY_NP) || defined(_WIN32)
case 'T':
if (!strToList(optarg, threadCores)) {
usage("Couldn't parse argument to -T flag, should be"
@@ -704,7 +718,11 @@ void displayPerScanResults(const vector<unique_ptr<ThreadContext>> &threads,
for (size_t j = 0; j != results.size(); j++) {
const auto &r = results[j];
double mbps = calc_mbps(r.seconds, bytesPerRun);
#ifndef _WIN32
printf("T %2u Scan %2zu: %'0.2f Mbit/sec\n", t->num, j, mbps);
#else
printf("T %2u Scan %2zu: %0.2f Mbit/sec\n", t->num, j, mbps);
#endif
}
}
printf("\n");
@@ -749,6 +767,7 @@ void displayResults(const vector<unique_ptr<ThreadContext>> &threads,
}
}
#ifndef _WIN32
printf("Time spent scanning: %'0.3f seconds\n", totalSecs);
printf("Corpus size: %'llu bytes ", bytesPerRun);
switch (scan_mode) {
@@ -764,22 +783,56 @@ void displayResults(const vector<unique_ptr<ThreadContext>> &threads,
printf("(%'zu blocks)\n", corpus_blocks.size());
break;
}
#else
printf("Time spent scanning: %0.3f seconds\n", totalSecs);
printf("Corpus size: %llu bytes ", bytesPerRun);
switch (scan_mode) {
case ScanMode::STREAMING:
printf("(%zu blocks in %llu streams)\n", corpus_blocks.size(),
count_streams(corpus_blocks));
break;
case ScanMode::VECTORED:
printf("(%zu blocks in %llu vectors)\n", corpus_blocks.size(),
count_streams(corpus_blocks));
break;
case ScanMode::BLOCK:
printf("(%zu blocks)\n", corpus_blocks.size());
break;
}
#endif
u64a totalBytes = bytesPerRun * repeats * threads.size();
u64a totalBlocks = corpus_blocks.size() * repeats * threads.size();
double matchRate = ((double)matchesPerRun * 1024) / bytesPerRun;
#ifndef _WIN32
printf("Matches per iteration: %'llu (%'0.3f matches/kilobyte)\n",
matchesPerRun, matchRate);
#else
printf("Matches per iteration: %llu (%0.3f matches/kilobyte)\n",
matchesPerRun, matchRate);
#endif
double blockRate = (double)totalBlocks / (double)totalSecs;
#ifndef _WIN32
printf("Overall block rate: %'0.2f blocks/sec\n", blockRate);
printf("Mean throughput (overall): %'0.2Lf Mbit/sec\n",
calc_mbps(totalSecs, totalBytes));
#else
printf("Overall block rate: %0.2f blocks/sec\n", blockRate);
printf("Mean throughput (overall): %0.2Lf Mbit/sec\n",
calc_mbps(totalSecs, totalBytes));
#endif
double lowestScanTime = fastestResult(threads);
#ifndef _WIN32
printf("Max throughput (per core): %'0.2Lf Mbit/sec\n",
calc_mbps(lowestScanTime, bytesPerRun));
#else
printf("Max throughput (per core): %0.2Lf Mbit/sec\n",
calc_mbps(lowestScanTime, bytesPerRun));
#endif
printf("\n");
if (display_per_scan) {
@@ -892,7 +945,7 @@ void runBenchmark(const Engine &db,
numThreads = 1;
} else {
numThreads = threadCores.size();
#ifdef HAVE_DECL_PTHREAD_SETAFFINITY_NP
#if defined(HAVE_DECL_PTHREAD_SETAFFINITY_NP) || defined(_WIN32)
useAffinity = true;
#else
useAffinity = false;
@@ -932,7 +985,7 @@ void runBenchmark(const Engine &db,
} // namespace
/** Main driver. */
int main(int argc, char *argv[]) {
int HS_CDECL main(int argc, char *argv[]) {
unique_ptr<Grey> grey;
#if !defined(RELEASE_BUILD)
grey = make_unique<Grey>();

View File

@@ -10,9 +10,16 @@ if (BUILD_CHIMERA)
include_directories(${PCRE_INCLUDE_DIRS})
add_definitions(-DHS_HYBRID)
add_executable(hscheck ${hscheck_SOURCES})
target_link_libraries(hscheck hs chimera ${PCRE_LDFLAGS} expressionutil pthread)
if(NOT WIN32)
target_link_libraries(hscheck hs chimera ${PCRE_LDFLAGS} expressionutil pthread)
else()
target_link_libraries(hscheck hs chimera pcre expressionutil)
endif()
else()
add_executable(hscheck ${hscheck_SOURCES})
target_link_libraries(hscheck hs expressionutil pthread)
if(NOT WIN32)
target_link_libraries(hscheck hs expressionutil pthread)
else()
target_link_libraries(hscheck hs expressionutil)
endif()
endif()

View File

@@ -70,8 +70,11 @@
#include <stdexcept>
#include <string>
#include <thread>
#ifndef _WIN32
#include <getopt.h>
#else
#include "win_getopt.h"
#endif
#include <boost/algorithm/string/trim.hpp>
using namespace std;
@@ -625,7 +628,7 @@ void loadSignatureBuildSigs(const string &inFile,
}
}
int main(int argc, char **argv) {
int HS_CDECL main(int argc, char **argv) {
num_of_threads = max(1u, std::thread::hardware_concurrency());
#if !defined(RELEASE_BUILD)

View File

@@ -74,7 +74,8 @@ if(HAVE_BACKTRACE)
"${BACKTRACE_CFLAGS}")
endif()
else() # WIN32
target_link_libraries(hscollider hs chimera ${PCRE_LDFLAGS} databaseutil
set_target_properties(hscollider PROPERTIES LINK_FLAGS "/STACK:8388608,8388608")
target_link_libraries(hscollider hs chimera pcre databaseutil
expressionutil corpusomatic crosscompileutil)
endif()

View File

@@ -26,6 +26,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef _WIN32
#define PCRE_STATIC
#endif
#include "config.h"
#include "common.h"

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Intel Corporation
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -35,8 +35,7 @@
#include <cstdlib>
#include <iostream>
#include <pthread.h>
#ifndef _WIN32
static const size_t COLLIDER_THREAD_STACK_SIZE = 8192 * 1024;
void Thread::start() {
@@ -79,6 +78,16 @@ create_thread:
}
}
void Thread::join() { pthread_join(thread, nullptr); }
#else // windows
void Thread::start() { thread = std::thread(&runThread, this); }
void Thread::join() { thread.join(); }
#endif
// Dispatch
void *Thread::runThread(void *thr) {
if (!no_signal_handler) {
@@ -88,7 +97,6 @@ void *Thread::runThread(void *thr) {
return nullptr;
}
void Thread::join() { pthread_join(thread, nullptr); }
Thread::Thread(size_t num) : thread_id(num) {}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Intel Corporation
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -31,7 +31,11 @@
#include <cstdlib>
#ifndef _WIN32
#include <pthread.h>
#else
#include <thread>
#endif
#include <boost/core/noncopyable.hpp>
@@ -54,7 +58,11 @@ protected:
const size_t thread_id;
private:
#ifndef _WIN32
pthread_t thread;
#else
std::thread thread;
#endif
};
#endif // UE2COLLIDER_THREAD_H

View File

@@ -169,8 +169,9 @@ struct MultiContext {
// Callback used for all (both single and multi-mode) scans.
static
int callbackMulti(unsigned int id, unsigned long long from,
unsigned long long to, UNUSED unsigned int flags, void *ctx) {
int HS_CDECL callbackMulti(unsigned int id, unsigned long long from,
unsigned long long to,
UNUSED unsigned int flags, void *ctx) {
MultiContext *mctx = static_cast<MultiContext *>(ctx);
assert(mctx);
assert(mctx->rs);
@@ -269,7 +270,7 @@ int callbackMulti(unsigned int id, unsigned long long from,
// Hybrid matcher callback.
static
ch_callback_t callbackHybrid(unsigned id, unsigned long long from,
ch_callback_t HS_CDECL callbackHybrid(unsigned id, unsigned long long from,
unsigned long long to, unsigned, unsigned size,
const ch_capture_t *captured, void *ctx) {
MultiContext *mctx = static_cast<MultiContext *>(ctx);
@@ -322,8 +323,9 @@ ch_callback_t callbackHybrid(unsigned id, unsigned long long from,
// Hybrid matcher error callback.
static
ch_callback_t errorCallback(UNUSED ch_error_event_t errorType, UNUSED unsigned int id, void *,
void *ctx) {
ch_callback_t HS_CDECL errorCallback(UNUSED ch_error_event_t errorType,
UNUSED unsigned int id, void *,
void *ctx) {
UNUSED MultiContext *mctx = static_cast<MultiContext *>(ctx);
assert(mctx);
assert(mctx->rs);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017, Intel Corporation
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -46,7 +46,11 @@
#include <sstream>
#include <string>
#include <vector>
#ifndef _WIN32
#include <getopt.h>
#else
#include "win_getopt.h"
#endif
#define xstr(s) str(s)
#define str(s) #s
@@ -467,7 +471,7 @@ void processArgs(int argc, char *argv[], CorpusProperties &corpus_gen_prop,
exit(1);
}
break;
case 'Z':
case 'Z': { // Parentheses save VS C2360
static constexpr unsigned ALIGN_LIMIT = MAX_MAX_UE2_ALIGN - 1;
if (optarg == string("R")) {
// Random min alignment selected.
@@ -481,6 +485,7 @@ void processArgs(int argc, char *argv[], CorpusProperties &corpus_gen_prop,
}
max_ue2_align = min_ue2_align + 1;
break;
}
case '8':
force_utf8 = true;
break;

View File

@@ -1840,13 +1840,17 @@ unique_ptr<CorporaSource> buildCorpora(const vector<string> &corporaFiles,
static
bool needsQuotes(const char *s) {
size_t len = strlen(s);
// don't confuse the correct isblank for the one in locale
int (*blank)(int) = &std::isblank;
if (len == 0) {
return true;
}
#ifndef _WIN32
// don't confuse the correct isblank for the one in locale
int (*blank)(int) = &std::isblank;
if (find_if(s, s + len, blank) != s + len) {
#else
if (find_if(s, s + len, [](unsigned char c){ return std::isblank(c); }) != s + len) {
#endif
return true;
}
@@ -1910,7 +1914,7 @@ bool runTests(CorporaSource &corpora_source, const ExpressionMap &exprMap,
return !summary.hasFailure();
}
int main(int argc, char *argv[]) {
int HS_CDECL main(int argc, char *argv[]) {
Grey grey;
vector<string> corporaFiles;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017, Intel Corporation
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -36,7 +36,7 @@
#include <ctype.h>
#include <string>
#ifdef HAVE_SIGACTION
#if defined(HAVE_SIGACTION) || defined(_WIN32)
#include <signal.h>
#endif
@@ -56,8 +56,12 @@ TLS_VARIABLE volatile size_t debug_corpus_len = 0;
extern std::string g_cmdline;
#ifdef HAVE_SIGACTION
#if defined(_WIN32)
static void __cdecl sighandler(int signum) {
#elif defined(HAVE_SIGACTION)
static void sighandler(int signum) {
#endif
#if defined(HAVE_SIGACTION) || defined(_WIN32)
/* NOTE: This signal handler is designed solely to provide more information
* when a crash occurs in ue2collider -- it makes calls to signal-unsafe
* functions like printf() and backtrace() by design, since we're already
@@ -141,7 +145,13 @@ static void sighandler(int signum) {
#endif // HAVE_SIGACTION
void installSignalHandler(void) {
#ifdef HAVE_SIGACTION
#ifdef _WIN32
signal(SIGABRT, sighandler);
signal(SIGFPE, sighandler);
signal(SIGILL, sighandler);
signal(SIGSEGV, sighandler);
#elif defined(HAVE_SIGACTION)
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sighandler;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Intel Corporation
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -40,7 +40,11 @@
#define STAGE_GRAPH_COMPILE 6
#define STAGE_GRAPH_RUN 7
#ifndef WIN32
#define TLS_VARIABLE __thread
#else
#define TLS_VARIABLE __declspec(thread)
#endif
extern TLS_VARIABLE volatile int debug_stage;
extern TLS_VARIABLE volatile int debug_expr;

View File

@@ -3,10 +3,6 @@ if (NOT DUMP_SUPPORT)
return()
endif ()
if (WIN32)
return()
endif ()
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/util)

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017, Intel Corporation
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -58,9 +58,19 @@
#include <string>
#include <vector>
#ifndef _WIN32
#include <getopt.h>
#else
#include "win_getopt.h"
#endif
#include <sys/stat.h>
#ifndef _WIN32
#include <dirent.h>
#else
#include <direct.h>
#define stat _stat
#endif
#include <boost/ptr_container/ptr_vector.hpp>
@@ -318,6 +328,7 @@ u32 buildDumpFlags(void) {
return flags;
}
#ifndef _WIN32
static
void clearDir(const string &path) {
DIR *dir = opendir(path.c_str());
@@ -341,15 +352,54 @@ void clearDir(const string &path) {
}
closedir(dir);
}
#else // windows
static
void clearDir(const string &path) {
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
string glob = path + "/*";
hFind = FindFirstFile(glob.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
printf("ERROR: couldn't open location %s\n", path.c_str());
exit(1);
}
do {
string basename(ffd.cFileName);
string fname(path);
fname.push_back('/');
fname.append(basename);
// Ignore '.' and '..'
if (basename == "." || basename == "..") {
continue;
}
if (!DeleteFile(fname.c_str())) {
printf("ERROR: couldn't remove file %s\n", fname.c_str());
}
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
}
#endif
static
int makeDirectory(const string &dirName) {
#ifndef _WIN32
mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP |
S_IROTH | S_IXOTH;
return mkdir(dirName.c_str(), mode);
#else
return _mkdir(dirName.c_str());
#endif
}
static
void prepareDumpLoc(string parent, string path, u32 flags, Grey &grey) {
struct stat st;
if (stat(parent.c_str(), &st)) {
// Create dump location if not found
mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP |
S_IROTH | S_IXOTH;
if (mkdir(parent.c_str(), mode) < 0) {
if (makeDirectory(parent) < 0) {
printf("ERROR: could not create dump location %s: %s\n",
parent.c_str(), strerror(errno));
exit(1);
@@ -365,9 +415,7 @@ void prepareDumpLoc(string parent, string path, u32 flags, Grey &grey) {
path = parent.append(path);
if (stat(path.c_str(), &st)) {
// Create dump location if not found
mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP |
S_IROTH | S_IXOTH;
if (mkdir(path.c_str(), mode) < 0) {
if (makeDirectory(path) < 0) {
printf("ERROR: could not create dump location %s: %s\n",
path.c_str(), strerror(errno));
exit(1);
@@ -546,7 +594,7 @@ unsigned int dumpData(const ExpressionMap &exprMap, Grey &grey) {
return dumpDataMulti(patterns, flags, ids, ext, grey);
}
int main(int argc, char *argv[]) {
int HS_CDECL main(int argc, char *argv[]) {
Grey grey;
grey.dumpFlags = Grey::DUMP_BASICS;