/**
|
/*
|
* @file
|
* Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
|
* @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
|
*
|
* @author Sergey Khabarov - sergeykhbr@gmail.com
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* @brief Core API utils methods implementation.
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
*/
|
*/
|
|
|
#include <string.h>
|
#include <string.h>
|
|
#include <stdlib.h>
|
#include <time.h>
|
#include <time.h>
|
#include <iostream>
|
#include <iostream>
|
#include <dirent.h>
|
#include <dirent.h>
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#else
|
#else
|
#include <dlfcn.h>
|
#include <dlfcn.h>
|
#endif
|
#endif
|
#include "api_types.h"
|
#include "api_types.h"
|
#include "api_utils.h"
|
#include "api_utils.h"
|
#include "attribute.h"
|
#include "attribute.h"
|
#include "iservice.h"
|
#include "iservice.h"
|
#include "iclass.h"
|
#include "iclass.h"
|
#include "coreservices/irawlistener.h"
|
#include "coreservices/irawlistener.h"
|
|
|
namespace debugger {
|
namespace debugger {
|
|
|
/** Plugin Entry point type definition */
|
/** Plugin Entry point type definition */
|
typedef void (*plugin_init_proc)();
|
typedef void (*plugin_init_proc)();
|
|
|
/** Temporary buffer for the log messages. */
|
/** Temporary buffer for the log messages. */
|
static char bufLog[1<<12];
|
static char bufLog[1024*1024];
|
static int uniqueIdx_ = 0;
|
static int uniqueIdx_ = 0;
|
|
|
/** Redirect output to specified console. */
|
/** Redirect output to specified console. */
|
static AttributeType default_console(Attr_List);
|
static AttributeType default_console(Attr_List);
|
mutex_def mutexDefaultConsoles_;
|
mutex_def mutexDefaultConsoles_;
|
|
|
/** */
|
/** */
|
static FILE *logfile_ = 0;
|
static FILE *logfile_ = 0;
|
|
|
static AttributeType log_file(Attr_String);
|
static AttributeType log_file(Attr_String);
|
|
|
/** Mutex to avoid concurency for the output stream among threads. */
|
/** Mutex to avoid concurency for the output stream among threads. */
|
mutex_def mutex_printf;
|
mutex_def mutex_printf;
|
|
|
/** Core log message interface object. */
|
/** Core log message interface object. */
|
extern IFace *getInterface(const char *name);
|
extern IFace *getInterface(const char *name);
|
|
|
extern "C" void RISCV_generate_name(AttributeType *name) {
|
extern "C" void RISCV_generate_name(AttributeType *name) {
|
char str[256];
|
char str[256];
|
RISCV_sprintf(str, sizeof(str), "obj_%08x", uniqueIdx_++);
|
RISCV_sprintf(str, sizeof(str), "obj_%08x", uniqueIdx_++);
|
name->make_string(str);
|
name->make_string(str);
|
}
|
}
|
|
|
extern "C" void RISCV_add_default_output(void *iout) {
|
extern "C" void RISCV_add_default_output(void *iout) {
|
AttributeType lstn(static_cast<IRawListener *>(iout));
|
AttributeType lstn(static_cast<IRawListener *>(iout));
|
RISCV_mutex_lock(&mutexDefaultConsoles_);
|
RISCV_mutex_lock(&mutexDefaultConsoles_);
|
default_console.add_to_list(&lstn);
|
default_console.add_to_list(&lstn);
|
RISCV_mutex_unlock(&mutexDefaultConsoles_);
|
RISCV_mutex_unlock(&mutexDefaultConsoles_);
|
}
|
}
|
|
|
extern "C" void RISCV_remove_default_output(void *iout) {
|
extern "C" void RISCV_remove_default_output(void *iout) {
|
RISCV_mutex_lock(&mutexDefaultConsoles_);
|
RISCV_mutex_lock(&mutexDefaultConsoles_);
|
for (unsigned i = 0; i < default_console.size(); i++) {
|
for (unsigned i = 0; i < default_console.size(); i++) {
|
if (default_console[i].to_iface() == iout) {
|
if (default_console[i].to_iface() == iout) {
|
default_console.remove_from_list(i);
|
default_console.remove_from_list(i);
|
break;
|
break;
|
}
|
}
|
}
|
}
|
RISCV_mutex_unlock(&mutexDefaultConsoles_);
|
RISCV_mutex_unlock(&mutexDefaultConsoles_);
|
}
|
}
|
|
|
extern "C" int RISCV_enable_log(const char *filename) {
|
extern "C" int RISCV_enable_log(const char *filename) {
|
if (logfile_) {
|
if (logfile_) {
|
fclose(logfile_);
|
fclose(logfile_);
|
logfile_ = NULL;
|
logfile_ = NULL;
|
}
|
}
|
logfile_ = fopen(filename, "w");
|
logfile_ = fopen(filename, "w");
|
if (!logfile_) {
|
if (!logfile_) {
|
return 1;
|
return 1;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|
extern "C" void RISCV_disable_log() {
|
extern "C" void RISCV_disable_log() {
|
if (logfile_) {
|
if (logfile_) {
|
fclose(logfile_);
|
fclose(logfile_);
|
}
|
}
|
logfile_ = 0;
|
logfile_ = 0;
|
}
|
}
|
|
|
extern "C" void RISCV_print_bin(int level, char *buf, int len) {
|
extern "C" void RISCV_print_bin(int level, char *buf, int len) {
|
std::cout.write(buf, len);
|
std::cout.write(buf, len);
|
//osLog.write(buf,len);
|
//osLog.write(buf,len);
|
//osLog.flush();
|
//osLog.flush();
|
|
|
}
|
}
|
|
|
extern "C" int RISCV_printf(void *iface, int level,
|
extern "C" int RISCV_printf(void *iface, int level,
|
const char *fmt, ...) {
|
const char *fmt, ...) {
|
int ret = 0;
|
int ret = 0;
|
va_list arg;
|
va_list arg;
|
IFace *iout = reinterpret_cast<IFace *>(iface);
|
IFace *iout = reinterpret_cast<IFace *>(iface);
|
|
|
RISCV_mutex_lock(&mutex_printf);
|
RISCV_mutex_lock(&mutex_printf);
|
if (iout == NULL) {
|
if (iout == NULL) {
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ", "unknown");
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ", "unknown");
|
} else if (strcmp(iout->getFaceName(), IFACE_SERVICE) == 0) {
|
} else if (strcmp(iout->getFaceName(), IFACE_SERVICE) == 0) {
|
IService *iserv = static_cast<IService *>(iout);
|
IService *iserv = static_cast<IService *>(iout);
|
AttributeType *local_level =
|
AttributeType *local_level =
|
static_cast<AttributeType *>(iserv->getAttribute("LogLevel"));
|
static_cast<AttributeType *>(iserv->getAttribute("LogLevel"));
|
if (level > static_cast<int>(local_level->to_int64())) {
|
if (level > static_cast<int>(local_level->to_int64())) {
|
RISCV_mutex_unlock(&mutex_printf);
|
RISCV_mutex_unlock(&mutex_printf);
|
return 0;
|
return 0;
|
}
|
}
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ",
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ",
|
iserv->getObjName());
|
iserv->getObjName());
|
} else if (strcmp(iout->getFaceName(), IFACE_CLASS) == 0) {
|
} else if (strcmp(iout->getFaceName(), IFACE_CLASS) == 0) {
|
IClass *icls = static_cast<IClass *>(iout);
|
IClass *icls = static_cast<IClass *>(iout);
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ",
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ",
|
icls->getClassName());
|
icls->getClassName());
|
} else {
|
} else {
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ",
|
ret = RISCV_sprintf(bufLog, sizeof(bufLog), "[%s]: ",
|
iout->getFaceName());
|
iout->getFaceName());
|
}
|
}
|
va_start(arg, fmt);
|
va_start(arg, fmt);
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
ret += vsprintf_s(&bufLog[ret], sizeof(bufLog) - ret, fmt, arg);
|
ret += vsprintf_s(&bufLog[ret], sizeof(bufLog) - ret, fmt, arg);
|
#else
|
#else
|
ret += vsprintf(&bufLog[ret], fmt, arg);
|
ret += vsprintf(&bufLog[ret], fmt, arg);
|
#endif
|
#endif
|
va_end(arg);
|
va_end(arg);
|
|
|
bufLog[ret++] = '\n';
|
bufLog[ret++] = '\n';
|
bufLog[ret] = '\0';
|
bufLog[ret] = '\0';
|
IRawListener *ilstn;
|
IRawListener *ilstn;
|
RISCV_mutex_lock(&mutexDefaultConsoles_);
|
RISCV_mutex_lock(&mutexDefaultConsoles_);
|
for (unsigned i = 0; i < default_console.size(); i++) {
|
for (unsigned i = 0; i < default_console.size(); i++) {
|
ilstn = static_cast<IRawListener *>(default_console[i].to_iface());
|
ilstn = static_cast<IRawListener *>(default_console[i].to_iface());
|
ilstn->updateData(bufLog, ret);
|
ilstn->updateData(bufLog, ret);
|
}
|
}
|
RISCV_mutex_unlock(&mutexDefaultConsoles_);
|
RISCV_mutex_unlock(&mutexDefaultConsoles_);
|
if (logfile_) {
|
if (logfile_) {
|
fwrite(bufLog, ret, 1, logfile_);
|
fwrite(bufLog, ret, 1, logfile_);
|
fflush(logfile_);
|
fflush(logfile_);
|
}
|
}
|
RISCV_mutex_unlock(&mutex_printf);
|
RISCV_mutex_unlock(&mutex_printf);
|
return ret;
|
return ret;
|
}
|
}
|
|
|
extern "C" int RISCV_sprintf(char *s, size_t len, const char *fmt, ...) {
|
extern "C" int RISCV_sprintf(char *s, size_t len, const char *fmt, ...) {
|
int ret;
|
int ret;
|
va_list arg;
|
va_list arg;
|
va_start(arg, fmt);
|
va_start(arg, fmt);
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
ret = vsprintf_s(s, len, fmt, arg);
|
ret = vsprintf_s(s, len, fmt, arg);
|
#else
|
#else
|
ret = vsprintf(s, fmt, arg);
|
ret = vsprintf(s, fmt, arg);
|
#endif
|
#endif
|
va_end(arg);
|
va_end(arg);
|
return ret;
|
return ret;
|
}
|
}
|
|
|
/* Suspend thread on certain number of milliseconds */
|
/* Suspend thread on certain number of milliseconds */
|
extern "C" void RISCV_sleep_ms(int ms) {
|
extern "C" void RISCV_sleep_ms(int ms) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
Sleep(ms);
|
Sleep(ms);
|
#else
|
#else
|
usleep(1000*ms);
|
usleep(1000*ms);
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" uint64_t RISCV_get_time_ms() {
|
extern "C" uint64_t RISCV_get_time_ms() {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
return 1000*(clock()/CLOCKS_PER_SEC);
|
return 1000*(clock()/CLOCKS_PER_SEC);
|
#else
|
#else
|
struct timeval tc;
|
struct timeval tc;
|
gettimeofday(&tc, NULL);
|
gettimeofday(&tc, NULL);
|
return 1000*tc.tv_sec + tc.tv_usec/1000;
|
return 1000*tc.tv_sec + tc.tv_usec/1000;
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" int RISCV_get_pid() {
|
extern "C" int RISCV_get_pid() {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
return _getpid();
|
return _getpid();
|
#else
|
#else
|
return getpid();
|
return getpid();
|
#endif
|
#endif
|
}
|
}
|
|
|
|
extern "C" void RISCV_memory_barrier() {
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
MemoryBarrier();
|
|
#else
|
|
__sync_synchronize();
|
|
#endif
|
|
}
|
|
|
extern "C" void RISCV_thread_create(void *data) {
|
extern "C" void RISCV_thread_create(void *data) {
|
LibThreadType *p = (LibThreadType *)data;
|
LibThreadType *p = (LibThreadType *)data;
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
p->Handle = (thread_def)_beginthreadex(0, 0, p->func, p->args, 0, 0);
|
p->Handle = (thread_def)_beginthreadex(0, 0, p->func, p->args, 0, 0);
|
#else
|
#else
|
pthread_create(&p->Handle, 0, p->func, p->args);
|
pthread_create(&p->Handle, 0, p->func, p->args);
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" uint64_t RISCV_thread_id() {
|
extern "C" uint64_t RISCV_thread_id() {
|
uint64_t r;
|
uint64_t r;
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
r = GetCurrentThreadId();
|
r = GetCurrentThreadId();
|
#else
|
#else
|
r = pthread_self();
|
r = pthread_self();
|
#endif
|
#endif
|
return r;
|
return r;
|
}
|
}
|
|
|
extern "C" void RISCV_thread_join(thread_def th, int ms) {
|
extern "C" void RISCV_thread_join(thread_def th, int ms) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
WaitForSingleObject(th, ms);
|
WaitForSingleObject(th, ms);
|
#else
|
#else
|
pthread_join(th, 0);
|
pthread_join(th, 0);
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" void RISCV_event_create(event_def *ev, const char *name) {
|
extern "C" void RISCV_event_create(event_def *ev, const char *name) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
ev->state = false;
|
ev->state = false;
|
ev->cond = CreateEvent(
|
ev->cond = CreateEvent(
|
NULL, // default security attributes
|
NULL, // default security attributes
|
TRUE, // manual-reset event
|
TRUE, // manual-reset event
|
FALSE, // initial state is nonsignaled
|
FALSE, // initial state is nonsignaled
|
TEXT(name) // object name
|
TEXT(name) // object name
|
);
|
);
|
#else
|
#else
|
pthread_mutex_init(&ev->mut, NULL);
|
pthread_mutex_init(&ev->mut, NULL);
|
pthread_cond_init(&ev->cond, NULL);
|
pthread_cond_init(&ev->cond, NULL);
|
ev->state = false;
|
ev->state = false;
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" void RISCV_event_close(event_def *ev) {
|
extern "C" void RISCV_event_close(event_def *ev) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
CloseHandle(ev->cond);
|
CloseHandle(ev->cond);
|
#else
|
#else
|
pthread_mutex_destroy(&ev->mut);
|
pthread_mutex_destroy(&ev->mut);
|
pthread_cond_destroy(&ev->cond);
|
pthread_cond_destroy(&ev->cond);
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" void RISCV_event_set(event_def *ev) {
|
extern "C" void RISCV_event_set(event_def *ev) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
ev->state = true;
|
ev->state = true;
|
SetEvent(ev->cond);
|
SetEvent(ev->cond);
|
#else
|
#else
|
pthread_mutex_lock(&ev->mut);
|
pthread_mutex_lock(&ev->mut);
|
ev->state = true;
|
ev->state = true;
|
pthread_mutex_unlock(&ev->mut);
|
pthread_mutex_unlock(&ev->mut);
|
pthread_cond_signal(&ev->cond);
|
pthread_cond_signal(&ev->cond);
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" int RISCV_event_is_set(event_def *ev) {
|
extern "C" int RISCV_event_is_set(event_def *ev) {
|
return ev->state ? 1: 0;
|
return ev->state ? 1: 0;
|
}
|
}
|
|
|
extern "C" void RISCV_event_clear(event_def *ev) {
|
extern "C" void RISCV_event_clear(event_def *ev) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
ev->state = false;
|
ev->state = false;
|
ResetEvent(ev->cond);
|
ResetEvent(ev->cond);
|
#else
|
#else
|
ev->state = false;
|
ev->state = false;
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" void RISCV_event_wait(event_def *ev) {
|
extern "C" void RISCV_event_wait(event_def *ev) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
WaitForSingleObject(ev->cond, INFINITE);
|
WaitForSingleObject(ev->cond, INFINITE);
|
#else
|
#else
|
int result = 0;
|
int result = 0;
|
pthread_mutex_lock(&ev->mut);
|
pthread_mutex_lock(&ev->mut);
|
while (result == 0 && !ev->state) {
|
while (result == 0 && !ev->state) {
|
result = pthread_cond_wait(&ev->cond, &ev->mut);
|
result = pthread_cond_wait(&ev->cond, &ev->mut);
|
}
|
}
|
pthread_mutex_unlock(&ev->mut);
|
pthread_mutex_unlock(&ev->mut);
|
#endif
|
#endif
|
}
|
}
|
|
|
extern "C" int RISCV_event_wait_ms(event_def *ev, int ms) {
|
extern "C" int RISCV_event_wait_ms(event_def *ev, int ms) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
DWORD wait_ms = ms;
|
DWORD wait_ms = ms;
|
if (ms == 0) {
|
if (ms == 0) {
|
wait_ms = INFINITE;
|
wait_ms = INFINITE;
|
}
|
}
|
if (WAIT_TIMEOUT == WaitForSingleObject(ev->cond, wait_ms)) {
|
if (WAIT_TIMEOUT == WaitForSingleObject(ev->cond, wait_ms)) {
|
return 1;
|
return 1;
|
}
|
}
|
return 0;
|
return 0;
|
#else
|
#else
|
struct timeval tc;
|
struct timeval tc;
|
struct timespec ts;
|
struct timespec ts;
|
int next_us;
|
int next_us;
|
int result = 0;
|
int result = 0;
|
gettimeofday(&tc, NULL);
|
gettimeofday(&tc, NULL);
|
next_us = tc.tv_usec + 1000 * ms;
|
next_us = tc.tv_usec + 1000 * ms;
|
ts.tv_sec = tc.tv_sec + (next_us / 1000000);
|
ts.tv_sec = tc.tv_sec + (next_us / 1000000);
|
next_us -= 1000000 * (next_us / 1000000);
|
next_us -= 1000000 * (next_us / 1000000);
|
ts.tv_nsec = 1000 * next_us;
|
ts.tv_nsec = 1000 * next_us;
|
|
|
pthread_mutex_lock(&ev->mut);
|
pthread_mutex_lock(&ev->mut);
|
while (result == 0 && !ev->state) {
|
while (result == 0 && !ev->state) {
|
result = pthread_cond_timedwait(&ev->cond, &ev->mut, &ts);
|
result = pthread_cond_timedwait(&ev->cond, &ev->mut, &ts);
|
}
|
}
|
pthread_mutex_unlock(&ev->mut);
|
pthread_mutex_unlock(&ev->mut);
|
if (ETIMEDOUT == result) {
|
if (ETIMEDOUT == result) {
|
return 1;
|
return 1;
|
}
|
}
|
return 0;
|
return 0;
|
#endif
|
#endif
|
}
|
}
|
|
|
|
extern "C" sharemem_def RISCV_memshare_create(const char *name, int sz) {
|
|
sharemem_def ret = 0;
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
ret = CreateFileMapping(
|
|
INVALID_HANDLE_VALUE, // use paging file
|
|
NULL, // default security
|
|
PAGE_READWRITE, // read/write access
|
|
0, // maximum object size (high-order DWORD)
|
|
sz, // maximum object size (low-order DWORD)
|
|
name); // name of mapping object
|
|
#else
|
|
#endif
|
|
if (!ret) {
|
|
RISCV_error("Couldn't create map object %s", name);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
extern "C" void* RISCV_memshare_map(sharemem_def h, int sz) {
|
|
void *ret = 0;
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
ret = MapViewOfFile(h, // handle to map object
|
|
FILE_MAP_ALL_ACCESS, // read/write permission
|
|
0,
|
|
0,
|
|
sz);
|
|
#else
|
|
#endif
|
|
if (!ret) {
|
|
RISCV_error("Couldn't map view of file with size %d", sz);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
extern "C" void RISCV_memshare_unmap(void *buf) {
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
UnmapViewOfFile(buf);
|
|
#else
|
|
#endif
|
|
}
|
|
|
|
extern "C" void RISCV_memshare_delete(sharemem_def h) {
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
CloseHandle(h);
|
|
#else
|
|
#endif
|
|
}
|
|
|
extern "C" int RISCV_mutex_init(mutex_def *mutex) {
|
extern "C" int RISCV_mutex_init(mutex_def *mutex) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
InitializeCriticalSection(mutex);
|
InitializeCriticalSection(mutex);
|
#else
|
#else
|
pthread_mutex_init(mutex, NULL);
|
pthread_mutex_init(mutex, NULL);
|
#endif
|
#endif
|
return 0;
|
return 0;
|
}
|
}
|
|
|
extern "C" int RISCV_mutex_lock(mutex_def *mutex) {
|
extern "C" int RISCV_mutex_lock(mutex_def *mutex) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
EnterCriticalSection(mutex);
|
EnterCriticalSection(mutex);
|
#else
|
#else
|
pthread_mutex_lock(mutex) ;
|
pthread_mutex_lock(mutex) ;
|
#endif
|
#endif
|
return 0;
|
return 0;
|
}
|
}
|
|
|
extern "C" int RISCV_mutex_unlock(mutex_def * mutex) {
|
extern "C" int RISCV_mutex_unlock(mutex_def * mutex) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
LeaveCriticalSection(mutex);
|
LeaveCriticalSection(mutex);
|
#else
|
#else
|
pthread_mutex_unlock(mutex);
|
pthread_mutex_unlock(mutex);
|
#endif
|
#endif
|
return 0;
|
return 0;
|
}
|
}
|
|
|
extern "C" int RISCV_mutex_destroy(mutex_def *mutex) {
|
extern "C" int RISCV_mutex_destroy(mutex_def *mutex) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
DeleteCriticalSection(mutex);
|
DeleteCriticalSection(mutex);
|
#else
|
#else
|
pthread_mutex_destroy(mutex);
|
pthread_mutex_destroy(mutex);
|
#endif
|
#endif
|
return 0;
|
return 0;
|
}
|
}
|
|
|
extern "C" void *RISCV_malloc(uint64_t sz) {
|
extern "C" void *RISCV_malloc(uint64_t sz) {
|
return malloc((size_t)sz);
|
return malloc((size_t)sz);
|
}
|
}
|
|
|
extern "C" void RISCV_free(void *p) {
|
extern "C" void RISCV_free(void *p) {
|
if (p) {
|
if (p) {
|
free(p);
|
free(p);
|
}
|
}
|
}
|
}
|
|
|
extern "C" int RISCV_get_core_folder(char *out, int sz) {
|
extern "C" int RISCV_get_core_folder(char *out, int sz) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
HMODULE hm = NULL;
|
HMODULE hm = NULL;
|
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
|
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
|
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
(LPCSTR)&RISCV_get_core_folder,
|
(LPCSTR)&RISCV_get_core_folder,
|
&hm)) {
|
&hm)) {
|
|
|
return GetLastError();
|
return GetLastError();
|
}
|
}
|
GetModuleFileNameA(hm, out, sz);
|
GetModuleFileNameA(hm, out, sz);
|
#else
|
#else
|
Dl_info dl_info;
|
Dl_info dl_info;
|
dladdr((void *)RISCV_get_core_folder, &dl_info);
|
dladdr((void *)RISCV_get_core_folder, &dl_info);
|
RISCV_sprintf(out, sz, "%s", dl_info.dli_fname);
|
RISCV_sprintf(out, sz, "%s", dl_info.dli_fname);
|
#endif
|
#endif
|
int n = (int)strlen(out);
|
int n = (int)strlen(out);
|
while (n > 0 && out[n] != '\\' && out[n] != '/') n--;
|
while (n > 0 && out[n] != '\\' && out[n] != '/') n--;
|
|
|
out[n+1] = '\0';
|
out[n+1] = '\0';
|
return 0;
|
return 0;
|
}
|
}
|
|
|
extern "C" void RISCV_set_current_dir() {
|
extern "C" void RISCV_set_current_dir() {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
HMODULE hMod = GetModuleHandle(NULL);
|
HMODULE hMod = GetModuleHandle(NULL);
|
char path[MAX_PATH] = "";
|
char path[MAX_PATH] = "";
|
GetModuleFileNameA(hMod, path, MAX_PATH);
|
GetModuleFileNameA(hMod, path, MAX_PATH);
|
#else // Linux
|
#else // Linux
|
// Get path of executable.
|
// Get path of executable.
|
char path[1024];
|
char path[1024];
|
ssize_t n = readlink("/proc/self/exe", path, sizeof(path)/sizeof(path[0]) - 1);
|
ssize_t n = readlink("/proc/self/exe", path, sizeof(path)/sizeof(path[0]) - 1);
|
if (n == -1) {
|
if (n == -1) {
|
return;
|
return;
|
}
|
}
|
path[n] = 0;
|
path[n] = 0;
|
#endif
|
#endif
|
|
|
size_t i;
|
size_t i;
|
for(i = strlen(path) - 1; i > 0 && path[i] != '/' && path[i] != '\\'; --i);
|
for(i = strlen(path) - 1; i > 0 && path[i] != '/' && path[i] != '\\'; --i);
|
path[i] = '\0';
|
path[i] = '\0';
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
SetCurrentDirectoryA(path);
|
SetCurrentDirectoryA(path);
|
#else // Linux
|
#else // Linux
|
chdir(path);
|
chdir(path);
|
#endif
|
#endif
|
}
|
}
|
|
|
/**
|
/**
|
* @brief Loading all plugins from the 'plugins' sub-folder.
|
* @brief Loading all plugins from the 'plugins' sub-folder.
|
* @details I suppose only one folders level so no itteration algorithm.
|
* @details I suppose only one folders level so no itteration algorithm.
|
*/
|
*/
|
void _load_plugins(AttributeType *list) {
|
void _load_plugins(AttributeType *list) {
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
HMODULE hlib;
|
HMODULE hlib;
|
#else
|
#else
|
void *hlib;
|
void *hlib;
|
#endif
|
#endif
|
DIR *dir;
|
DIR *dir;
|
struct dirent *ent;
|
struct dirent *ent;
|
plugin_init_proc plugin_init;
|
plugin_init_proc plugin_init;
|
char curdir[1024];
|
char curdir[1024];
|
std::string plugin_dir, plugin_lib;
|
std::string plugin_dir, plugin_lib;
|
|
|
RISCV_get_core_folder(curdir, sizeof(curdir));
|
RISCV_get_core_folder(curdir, sizeof(curdir));
|
|
|
plugin_dir = std::string(curdir) + "plugins/";
|
plugin_dir = std::string(curdir) + "plugins/";
|
dir = opendir(plugin_dir.c_str());
|
dir = opendir(plugin_dir.c_str());
|
|
|
if (dir == NULL) {
|
if (dir == NULL) {
|
RISCV_error("Plugins directory '%s' not found", curdir);
|
RISCV_error("Plugins directory '%s' not found", curdir);
|
return;
|
return;
|
}
|
}
|
|
|
while ((ent = readdir(dir)) != NULL) {
|
while ((ent = readdir(dir)) != NULL) {
|
if (ent->d_type != DT_REG) {
|
if (ent->d_type != DT_REG) {
|
continue;
|
continue;
|
}
|
}
|
if ((strstr(ent->d_name, ".dll") == NULL)
|
if ((strstr(ent->d_name, ".dll") == NULL)
|
&& (strstr(ent->d_name, ".so") == NULL)) {
|
&& (strstr(ent->d_name, ".so") == NULL)) {
|
continue;
|
continue;
|
}
|
}
|
plugin_lib = plugin_dir + std::string(ent->d_name);
|
plugin_lib = plugin_dir + std::string(ent->d_name);
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
if ((hlib = LoadLibrary(plugin_lib.c_str())) == 0) {
|
if ((hlib = LoadLibrary(plugin_lib.c_str())) == 0) {
|
continue;
|
continue;
|
}
|
}
|
plugin_init = (plugin_init_proc)GetProcAddress(hlib, "plugin_init");
|
plugin_init = (plugin_init_proc)GetProcAddress(hlib, "plugin_init");
|
if (!plugin_init) {
|
if (!plugin_init) {
|
FreeLibrary(hlib);
|
FreeLibrary(hlib);
|
continue;
|
continue;
|
}
|
}
|
#else
|
#else
|
// reset errors
|
// reset errors
|
dlerror();
|
dlerror();
|
if ((hlib = dlopen(plugin_lib.c_str(), RTLD_LAZY)) == 0) {
|
if ((hlib = dlopen(plugin_lib.c_str(), RTLD_LAZY)) == 0) {
|
RISCV_info("Can't open library '%s': %s",
|
printf("Can't open library '%s': %s\n",
|
plugin_lib.c_str(), dlerror());
|
plugin_lib.c_str(), dlerror());
|
continue;
|
continue;
|
}
|
}
|
plugin_init = (plugin_init_proc)dlsym(hlib, "plugin_init");
|
plugin_init = (plugin_init_proc)dlsym(hlib, "plugin_init");
|
if (dlerror()) {
|
if (dlerror()) {
|
RISCV_info("Not found plugin_init() in file '%s'",
|
printf("Not found plugin_init() in file '%s'\n",
|
plugin_lib.c_str());
|
plugin_lib.c_str());
|
dlclose(hlib);
|
dlclose(hlib);
|
continue;
|
continue;
|
}
|
}
|
#endif
|
#endif
|
|
|
RISCV_info("Loading plugin file '%s'", plugin_lib.c_str());
|
RISCV_info("Loading plugin file '%s'", plugin_lib.c_str());
|
plugin_init();
|
plugin_init();
|
|
|
AttributeType item;
|
AttributeType item;
|
item.make_list(2);
|
item.make_list(2);
|
item[0u] = AttributeType(plugin_lib.c_str());
|
item[0u] = AttributeType(plugin_lib.c_str());
|
item[1] = AttributeType(Attr_UInteger,
|
item[1] = AttributeType(Attr_UInteger,
|
reinterpret_cast<uint64_t>(hlib));
|
reinterpret_cast<uint64_t>(hlib));
|
list->add_to_list(&item);
|
list->add_to_list(&item);
|
}
|
}
|
closedir(dir);
|
closedir(dir);
|
}
|
}
|
|
|
void _unload_plugins(AttributeType *list) {
|
void _unload_plugins(AttributeType *list) {
|
const char *plugin_name;
|
const char *plugin_name;
|
for (unsigned i = 0; i < list->size(); i++) {
|
for (unsigned i = 0; i < list->size(); i++) {
|
if (!(*list)[i].is_list()) {
|
if (!(*list)[i].is_list()) {
|
RISCV_error("Can't free plugin[%d] library", i);
|
RISCV_error("Can't free plugin[%d] library", i);
|
continue;
|
continue;
|
}
|
}
|
plugin_name = (*list)[i][0u].to_string();
|
plugin_name = (*list)[i][0u].to_string();
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
HMODULE hlib = reinterpret_cast<HMODULE>((*list)[i][1].to_uint64());
|
HMODULE hlib = reinterpret_cast<HMODULE>((*list)[i][1].to_uint64());
|
FreeLibrary(hlib);
|
FreeLibrary(hlib);
|
#else
|
#else
|
void *hlib = reinterpret_cast<void *>((*list)[i][1].to_uint64());
|
void *hlib = reinterpret_cast<void *>((*list)[i][1].to_uint64());
|
if (strstr(plugin_name, "gui_plugin") == 0) {
|
if (strstr(plugin_name, "gui_plugin") == 0) {
|
/** It's a workaround to avoid SIGSEGV on application exiting.
|
/** It's a workaround to avoid SIGSEGV on application exiting.
|
* It's a specific of dynamically linked QT-libraries. They
|
* It's a specific of dynamically linked QT-libraries. They
|
* create and use global variable freeing on application
|
* create and use global variable freeing on application
|
* closing.
|
* closing.
|
*/
|
*/
|
dlclose(hlib);
|
dlclose(hlib);
|
}
|
}
|
#endif
|
#endif
|
}
|
}
|
}
|
}
|
|
|
void put_char(char s, FILE *f) {
|
void put_char(char s, FILE *f) {
|
fputc(s, f);
|
fputc(s, f);
|
fflush(f);
|
fflush(f);
|
}
|
}
|
|
|
// check if this attribute like list with items <= 2: [a,b]
|
// check if this attribute like list with items <= 2: [a,b]
|
bool is_single_line(const char *s) {
|
bool is_single_line(const char *s) {
|
bool ret = false;
|
bool ret = false;
|
if (*s == '[') {
|
if (*s == '[') {
|
const char *pcheck = s + 1;
|
const char *pcheck = s + 1;
|
int item_cnt = 0;
|
int item_cnt = 0;
|
int symbol_cnt = 0;
|
int symbol_cnt = 0;
|
while (*pcheck != ']') {
|
while (*pcheck != ']') {
|
symbol_cnt++;
|
symbol_cnt++;
|
if (*pcheck == ',') {
|
if (*pcheck == ',') {
|
item_cnt++;
|
item_cnt++;
|
} else if (*pcheck == '[' || *pcheck == '{') {
|
} else if (*pcheck == '[' || *pcheck == '{') {
|
item_cnt = 100;
|
item_cnt = 100;
|
break;
|
break;
|
}
|
}
|
++pcheck;
|
++pcheck;
|
}
|
}
|
if (item_cnt <= 2 && symbol_cnt < 80) {
|
if (item_cnt <= 2 && symbol_cnt < 80) {
|
ret = true;
|
ret = true;
|
}
|
}
|
}
|
}
|
return ret;
|
return ret;
|
}
|
}
|
|
|
const char *writeAttrToFile(FILE *f, const char *s, int depth) {
|
const char *writeAttrToFile(FILE *f, const char *s, int depth) {
|
const char *ret = s;
|
const char *ret = s;
|
bool end_attr = false;
|
bool end_attr = false;
|
bool do_single_line = false;
|
bool do_single_line = false;
|
|
|
if ((*s) == 0) {
|
if ((*s) == 0) {
|
return ret;
|
return ret;
|
}
|
}
|
|
|
put_char('\n', f);
|
put_char('\n', f);
|
for (int i = 0; i < depth; i++) {
|
for (int i = 0; i < depth; i++) {
|
put_char(' ', f);
|
put_char(' ', f);
|
put_char(' ', f);
|
put_char(' ', f);
|
}
|
}
|
|
|
|
|
while ((*ret) && !end_attr) {
|
while ((*ret) && !end_attr) {
|
put_char(*ret, f);
|
put_char(*ret, f);
|
if ((*ret) == ',') {
|
if ((*ret) == ',') {
|
if (!do_single_line) {
|
if (!do_single_line) {
|
put_char('\n', f);
|
put_char('\n', f);
|
for (int i = 0; i < depth; i++) {
|
for (int i = 0; i < depth; i++) {
|
put_char(' ', f);
|
put_char(' ', f);
|
put_char(' ', f);
|
put_char(' ', f);
|
}
|
}
|
}
|
}
|
} else if ((*ret) == ']' || (*ret) == '}') {
|
} else if ((*ret) == ']' || (*ret) == '}') {
|
if (do_single_line) {
|
if (do_single_line) {
|
do_single_line = false;
|
do_single_line = false;
|
} else {
|
} else {
|
return ret;
|
return ret;
|
}
|
}
|
} else if ((*ret) == '[' || (*ret) == '{') {
|
} else if ((*ret) == '[' || (*ret) == '{') {
|
do_single_line = is_single_line(ret);
|
do_single_line = is_single_line(ret);
|
if (!do_single_line) {
|
if (!do_single_line) {
|
ret = writeAttrToFile(f, ret + 1, depth + 1);
|
ret = writeAttrToFile(f, ret + 1, depth + 1);
|
}
|
}
|
}
|
}
|
++ret;
|
++ret;
|
}
|
}
|
|
|
return ret;
|
return ret;
|
}
|
}
|
|
|
void RISCV_write_json_file(const char *filename, const char *s) {
|
void RISCV_write_json_file(const char *filename, const char *s) {
|
FILE *f = fopen(filename, "w");
|
FILE *f = fopen(filename, "w");
|
if (!f) {
|
if (!f) {
|
return;
|
return;
|
}
|
}
|
writeAttrToFile(f, s, 0);
|
writeAttrToFile(f, s, 0);
|
fclose(f);
|
fclose(f);
|
}
|
}
|
|
|
int RISCV_read_json_file(const char *filename, void *outattr) {
|
int RISCV_read_json_file(const char *filename, void *outattr) {
|
AttributeType *out = reinterpret_cast<AttributeType *>(outattr);
|
AttributeType *out = reinterpret_cast<AttributeType *>(outattr);
|
FILE *f = fopen(filename, "r");
|
FILE *f = fopen(filename, "r");
|
if (!f) {
|
if (!f) {
|
return 0;
|
return 0;
|
}
|
}
|
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
int sz = ftell(f);
|
int sz = ftell(f);
|
out->make_data(sz);
|
out->make_data(sz);
|
|
|
fseek(f, 0, SEEK_SET);
|
fseek(f, 0, SEEK_SET);
|
fread(out->data(), sz, 1, f);
|
fread(out->data(), sz, 1, f);
|
return sz;
|
return sz;
|
}
|
}
|
|
|
} // namespace debugger
|
} // namespace debugger
|
|
|