I have MS Visual Studio 2019 setup with a CMake project and a configuration to cross-compile on a raspberry pi I have on my network.
I am trying to build a simple library (libmylib.so). The odd thing is that it works, but it seg faults at the end. The library is built and the library works fine, but at the end I am getting "Exception Unhandled - Segmentation fault". So I apparently have something wrong in my setup, just not sure what it is.
Here are the CMakeLists.txt
# Root CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project ("native-test")
# Include sub-projects.
add_subdirectory("libmylib")
# libmylib\CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project("mylib")
add_library(mylib SHARED library.cpp utils.cpp)
target_include_directories(mylib PUBLIC
/usr/lib/jvm/java-8-openjdk-armhf/include
/usr/lib/jvm/java-8-openjdk-armhf/include/linux
)
set_target_properties(mylib PROPERTIES VERSION 1)
There is some output in the Output window, but I am snipping out the textual part... if this helps
shellexec 1.0
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l
Last login: Sun May 17 13:35:25 2020 from 192.168.70.94
pi@raspberrypi:~$ =thread-group-added,id="i1"
GNU gdb (Raspbian 7.12-6) 7.12.0.20161007-git
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
Segmentation fault
1017kill
The thread 'libmylib.so' (0x7bbb) has exited with code 0 (0x0).
The program 'libmylib.so' has exited with code 0 (0x0).
It almost sounds like it is trying to execute my library as a native executable, but not sure what I have wrong. I am new to Visual Studio and CMake so any help is appreciated
Here is the code as requested. I'll omit the headers, but can include those if it helps
// library.cpp
#include "library.h"
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_com_test_platform_arch_ArmLinuxPlatform_reboot(JNIEnv* env, jobject obj) {
sync(); // write any pending mods to the filesystem
printf("Rebooting...\n");
int retCode = reboot(LINUX_REBOOT_CMD_RESTART);
if (retCode != 0) {
if (errno == EFAULT) {
printf("Reboot Failed. EFAULT");
}
else if (errno == EINVAL) {
printf("Reboot Failed. EINVAL");
}
else if (errno == EPERM) {
printf("Reboot Failed. EPERM");
}
else {
printf("Reboot Failed. Error=%i", errno);
}
}
else
printf("Reboot successful");
printf("\n");
}
JNIEXPORT jint JNICALL Java_com_test_platform_arch_ArmLinuxPlatform_pid(JNIEnv* env, jobject obj) {
return getpid();
}
JNIEXPORT jfloat JNICALL Java_com_test_platform_arch_ArmLinuxPlatform_cpuUsage(JNIEnv* env, jobject obj) {
CPUData d1 = ReadStats();
std::this_thread::sleep_for(std::chrono::milliseconds(250));
CPUData d2 = ReadStats();
const float ACTIVE_TIME = static_cast<float>(GetActiveTime(d2) - GetActiveTime(d1));
const float IDLE_TIME = static_cast<float>(GetIdleTime(d2) - GetIdleTime(d1));
const float TOTAL_TIME = ACTIVE_TIME + IDLE_TIME;
return 100.0f * (ACTIVE_TIME / TOTAL_TIME);
}
#ifdef __cplusplus
}
#endif
// utils.cpp
#include "utils.h"
#ifdef __cplusplus
extern "C" {
#endif
CPUData ReadStats() {
std::ifstream fileStat("/proc/stat");
std::string line;
std::getline(fileStat, line);
std::istringstream ss(line);
CPUData data;
ss >> data.cpu;
for (int i = 0; i < NUM_CPU_STATES; ++i)
ss >> data.times[i];
fileStat.close();
return data;
}
size_t GetIdleTime(const CPUData& e)
{
return e.times[S_IDLE] +
e.times[S_IOWAIT];
}
size_t GetActiveTime(const CPUData& e)
{
return e.times[S_USER] +
e.times[S_NICE] +
e.times[S_SYSTEM] +
e.times[S_IRQ] +
e.times[S_SOFTIRQ] +
e.times[S_STEAL] +
e.times[S_GUEST] +
e.times[S_GUEST_NICE];
}
#ifdef __cplusplus
}
#endif
// utils.h
#pragma once
#include <vector>
#include <chrono>
#include <thread>
#include <string>
#include <fstream>
#include <sstream>
#ifdef __cplusplus
extern "C" {
#endif
const int NUM_CPU_STATES = 10;
enum CPUStates
{
S_USER = 0,
S_NICE,
S_SYSTEM,
S_IDLE,
S_IOWAIT,
S_IRQ,
S_SOFTIRQ,
S_STEAL,
S_GUEST,
S_GUEST_NICE
};
typedef struct CPUData
{
std::string cpu;
size_t times[NUM_CPU_STATES];
} CPUData;
CPUData ReadStats();
size_t GetIdleTime(const CPUData& e);
size_t GetActiveTime(const CPUData& e);
#ifdef __cplusplus
}
#endif
From https://www.geeksforgeeks.org/segmentation-fault-sigsegv-vs-bus-error-sigbus/
Segmentation Fault (also known as SIGSEGV and is usually signal 11) occur when the program tries to write/read outside the memory allocated for it or when writing memory which can only be read.In other words when the program tries to access the memory to which it doesn’t have access to. SIGSEGV is abbreviation for “Segmentation Violation”.
We should see the code, i don't think it's a compiler error.
User contributions licensed under CC BY-SA 3.0