dlsym function return type - dlopen

i am loading libslabhidtouart.so file using dlopen() without any error but when i am calling a function using dlsym() ,I got no such process error
here is my code
int main(int argc, char **argv)
{
typedef unsigned int DWORD;
typedef unsigned short WORD;
typedef int HID_UART_STATUS;
void *handle;
HID_UART_STATUS (*cosine)( DWORD*,WORD,WORD);
//typedef void (*simple_demo_function)(void);
char *error;
handle = dlopen("libslabhidtouart.so.1.0", RTLD_NOW);
if (!handle) {
fprintf(stderr, " %s\n", dlerror());
getchar();
exit(EXIT_FAILURE);
}
dlerror(); /* Clear any existing error */
/* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
would seem more natural, but the C99 standard leaves
casting from "void *" to a function pointer undefined.
The assignment used below is the POSIX.1-2003 (Technical
Corrigendum 1) workaround; see the Rationale for the
POSIX specification of dlsym(). */
*(void **) (&cosine) = dlsym(handle, "HidUart_GetNumDevices");
if ((error = dlerror()) != NULL) {
fprintf(stderr, " %s\n", error);
getchar();
exit(EXIT_FAILURE);
}
getchar();
dlclose(handle);
exit(EXIT_SUCCESS);
return 0;
}
/**** return type of function HidUart_GetNumDevices is int,so is there any casting problem or my method signature is wrong or what else plz guide me,i am no good at c .

I also got strange "No such process" errors, yet already directly upon a dlopen() call within OpenSSL:
2675996:error:25066067:lib(37):func(102):reason(103):dso_dlfcn.c:187:filename(./engine_pkcs11.dll): No such process
It turned out that the referenced DLL (or .so file) exists, but has a dependency on some other library (cygp11-2.dll in my case) that could not be resolved in the context of the application's process (taking into account its PATH environment variable setting). In this case, use ldd (or cygcheck.exe if applicable) to see if all dependencies are correctly resolved.
So the "no such process" error message returned by dlerror() can be quite misleading.

Related

Why NVCC is unable to match function definition to an existing declaration?

I have this files that produce the following error when compiling with nvcc
error C2244: 'TemplateClass<N>::print': unable to match function definition to an existing declaration
note: see declaration of 'TemplateClass<N>::print'
note: definition note: 'void TemplateClass<N>::print(const std::string [])'
note: existing declarations note: 'void TemplateClass<N>::print(const std::string [N])'
Template.h
#pragma once
#include <string>
#include <iostream>
template <unsigned int N>
class TemplateClass
{
private:
std::string name;
public:
TemplateClass();
TemplateClass(const std::string& name);
void print(const std::string familyName[N]);
};
#include "template.inl"
Template.inl
template <unsigned int N>
TemplateClass<N>::TemplateClass()
{
name = "Unknown";
}
template <unsigned int N>
TemplateClass<N>::TemplateClass(const std::string& name)
{
this->name = name;
}
template <unsigned int N>
void TemplateClass<N>::print(const std::string familyName[N])
{
std::cout << "My name is " << name << " ";
for (auto i = 0; i < N; i++)
std::cout << familyName[i] << " ";
std::cout << std::endl;
}
consume_template.cu
#include "template.h"
void consume_template_gpu()
{
TemplateClass<3> obj("aname");
std::string namesf[3];
namesf[0] = "un";
namesf[1] = "deux";
namesf[2] = "trois";
obj.print(namesf);
}
I am using VS2017 15.4.5, later versions failed to create the project with CMake.
The project was created with CMake like this
cmake_minimum_required(VERSION 3.10)
project(template_inl_file LANGUAGES CXX CUDA)
set (lib_files template.h consume_template.cu)
add_library(template_inl_file_lib ${lib_files})
Just out of curiosity , try using std::string namesf = {"un","deux","trois"}; It seems like a compiler issue. Trying different formats might help compiler to understand better. Otherwise code seems to be ok.
Maybe you're missing some linkage with CMake. Also try compiling straight from VS2017 without using CMake by creating a CUDA project.
What's happening is that the array is decaying into a pointer and the size of the array is lost during compilation. So this
template <unsigned int N>
void TemplateClass<N>::print(const std::string familyName[N]);
will be actually turned into this
template <unsigned int N>
void TemplateClass<N>::print(const std::string* familyName);
as we can see there is no way for the compiler to know that it has to generate different functions depending on the size of the array (i.e. the template parameter N).
To solve this we can use an old trick to avoid array decay like this
template <unsigned int N>
void TemplateClass<N>::print(const std::string (&familyName)[N]);
Now the size N is present through the compilation and the compiler knows there are different functions to be generated. I guess, as pointed out in the comments of the question that NVCC produces code that VS does not produce by itself and then it does not know how to handle it.
More info on the topic on the following links
http://pointer-overloading.blogspot.ch/2013/09/c-template-argument-deduction-to-deduce.html
http://en.cppreference.com/w/cpp/language/template_argument_deduction
https://theotherbranch.wordpress.com/2011/08/24/template-parameter-deduction-from-array-dimensions/

Declaring a global device array using a pointer with cudaMemcpyFromSymbol

When I use the following code, it shows the correct value 3345.
#include <iostream>
#include <cstdio>
__device__ int d_Array[1];
__global__ void foo(){
d_Array[0] = 3345;
}
int main()
{
foo<<<1,1>>>();
cudaDeviceSynchronize();
int h_Array[1];
cudaMemcpyFromSymbol(&h_Array, d_Array, sizeof(int));
std::cout << "values: " << h_Array[0] << std::endl;
}
But if we replace the line of code __device__ int d_Array[1]; by
__device__ int *d_Array; it shows a wrong value. Why?
The problem is in memory allocation. Try the same thing on C++ (on host) and you will either get an error or unexpected value.
In addition, you can check the Cuda errors calling cudaGetLastError() after your kernel. In the first case everything is fine, and the result is cudaSuccess. In the second case there is cudaErrorLaunchFailure error. Here is the explanation of this error (from cuda toolkit documentation):
"An exception occurred on the device while executing a kernel. Common causes include dereferencing an invalid device pointer and accessing out of bounds shared memory. The device cannot be used until cudaThreadExit() is called. All existing device memory allocations are invalid and must be reconstructed if the program is to continue using CUDA."
Note that cudaMemcpyToSymbol also supports the offset parameter for array indexing

difference kernel exception handling and programming language exception handling

I don't understand how c++, java or others high level languages that support exception handling work???
I know that if I write an application will be run in user mode and if it rises an exception like zero division, the system call an interrupt routine in kernel mode or use my try/catch block???
what I want to say is:
when I write simple code in c++ for example like this:
#include <iostream>
using namespace std;
double divide(int a, int b)
{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}
int main ()
{
int x = 50;
int y = 0;
double z = 0;
try {
z = divide(x, y);
cout << z << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
and compile it, I'm getting an software with inside an exception routine.
I mean, during execution code CPU will try to do x/y and will raise an exception. Now, in this case who handles this error:
1) process un user mode pass the workflow to an exception routine that I write
Or
2) System switch in kernel mode and throw a trap for running an interrupt routine.
I don't understand what is the specific steps that system like linux or windows use to solve an exception???

passing command line argument in c as parameters

I'm trying to make a function to assign a structure members a value.
#include <stdio.h>
#include <string.h>
typedef struct
{
int id;
char *data;
}person_t;
person_t person_build(int id, char *data);
int main (int argc, char *argv[])
{
person_t person = person_build(atoi(argv[1]), argv[2]);
return 0;
}
person_t person_build(int id, char *data)
{
person_t person;
person.id = id;
strcpy(person.data, data);
return person;
}
This program compiled successfully.
I run that program and give command line arguments as parameters
to person_build() function as parameters.
>struct5.exe 4 Something
operating system(windows 7) give me a warning this program has stopped working
but when run without any command line argument (changing the person_build() parameter other than command line arguments) that program works.
can someone explain why this behaviour happen?
Your program is not working because you are accessing memory structures that you have not initialized. Specifically:
typedef struct
{
int id;
char *data;
}person_t;
This creates a structure that has a char * as a member. That char * allocates no actual memory, it simply reserves a member in the structure that can hold a memory address that should point to a value. Later, you:
strcpy(person.data, data);
You are now copying data into the memory location that person.data points at even though you have never allocated memory or initialized person.data.
You could take this approach:
person_t person_build(int id, char *data)
{
person_t person;
person.id = id;
person.data = malloc(sizeof(char) * strlen(data) + 1);
if(person.data != NULL) strcpy(person.data, data);
return person;
}
This allocates memory of the proper size, accounting for null termination at the end of the string, verifies that the allocation was successful and only then will it attempt to copy into that memory.
This is far from complete. I think you may have many more obstacles yet to overcome!

Intel pin tool cannot catch thrown exceptions

I am now learning Intel pin, I write the following codes in main function of my pintool.
try
{
throw std::exception("test daniel");
}
catch (std::exception& e)
{
printf(e.what());
}
Run it( pin.exe -t Test.dll -- calc.exe), but it just crashed, this is definitely due to an uncaught exception.
But I wonder why my "catch" code failed.
Anyone know the reason, or how to catch exception in pintool?
Here is how thrown exceptions should be catched in a pintool, assuming you have all the mandatory compile options. It should be noted that this simple pintool does not do anything beside catching exceptions thrown by pin or the tool (not the application).
You will note that the registration of the exception handler function occures before the PIN_StartProgram() function, otherwise exceptions will be ignored.
Finally, although it is not mentioned in the documentation, I would expect that exceptions thrown after the call to PIN_AddInternalExceptionHandler() and before PIN_StartProgram() be not catched by the handler. I would instead expect the handler to catch exceptions thrown after PIN_StartProgram(), but again, it is not mentioned in the documentation.
//-------------------------------------main.cpp--------------------------------
#include "pin.h"
#include <iostream>
EXCEPT_HANDLING_RESULT ExceptionHandler(THREADID tid, EXCEPTION_INFO *pExceptInfo, PHYSICAL_CONTEXT *pPhysCtxt, VOID *v)
{
EXCEPTION_CODE c = PIN_GetExceptionCode(pExceptInfo);
EXCEPTION_CLASS cl = PIN_GetExceptionClass(c);
std::cout << "Exception class " << cl;
std::cout << PIN_ExceptionToString(pExceptInfo);
return EHR_UNHANDLED ;
}
VOID test(TRACE trace, VOID * v)
{
// throw your exception here
}
int main(int argc, char *argv[])
{
// Initialize PIN library. This was apparently missing in your tool ?
PIN_InitSymbols();
if( PIN_Init(argc,argv) )
{
return Usage();
}
// Register here your exception handler function
PIN_AddInternalExceptionHandler(ExceptionHandler,NULL);
//register your instrumentation function
TRACE_AddInstrumentFunction(test,null);
// Start the program, never returns...
PIN_StartProgram();
return 0;
}