difference kernel exception handling and programming language exception handling - exception

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???

Related

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

How can set different function signature to the same function pointer?

How can I set a function pointer depending on some condition to functions with different signature?
Example:
short int A()
{
return 0;
}
long int B()
{
return 0;
}
void main()
{
std::function<short int()> f = A;
f();
if(true)
{
//error
f = B;
}
}
How can use the same function pointer for two functions with different signature?
Is it possible?
If is not, there is an efficient way to call the appropriate function depending on behavior instead of use a variable and split the whole code with if statements?
EDIT / EXPANSION ("2nd case")
#include <SDL.h>
class Obj { //whatever ...}
class A
{
private:
Uint16 ret16() { return SDL_ReadLE16(_pFile); }
Uint32 ret32() { return SDL_ReadLE32(_pFile); }
_pFile = nullptr;
public:
Obj* func()
{
Obj obj = new Obj();
_pFile = SDL_RWFromFile("filename.bin","r"));
auto ret = std::mem_fn(&SHPfile::ret16);
if(true)
{
ret = std::mem_fn(&SHPfile::ret32);
}
//ret();
// continue whatever
// ....
SDL_RWclose(_pFile);
return *obj;
}
}
I have a compilation error on a similar case using the Uint16 and Uint32 variable of SDL 2 library, using std::mem_fn
the compiler give me this error (relative to my code, but it's implemented in a way like the above example):
error: no match for ‘operator=’ (operand types are ‘std::_Mem_fn<short unsigned int (IO::File::*)()>’ and ‘std::_Mem_fn<unsigned int (IO::File::*)()>’)
To resolve this compilation error, I forced both the function to return a int type.
Is there a better way?
Or I did something wrong?
The comments already say that clang accepts the code as is, and I can now say that GCC 4.8.4 and GCC 4.9.2 both accept it as well, after fixing void main() to say int main().
This use of std::function is perfectly valid. The C++11 standard says:
20.8.11.2 Class template function [func.wrap.func]
function& operator=(const function&);
function& operator=(function&&);
function& operator=(nullptr_t);
There is no template assignment operator here, so assignment of B could only construct a new temporary function<short int()> object, and move-assign from that. To determine whether the construction of that temporary is possible:
20.8.11.2.1 function construct/copy/destroy [func.wrap.func.con]
template<class F> function(F f);
template <class F, class A> function(allocator_arg_t, const A& a, F f);
7 Requires: F shall be CopyConstructible. f shall be Callable (20.8.11.2) for argument types ArgTypes and return type R. The copy constructor and destructor of A shall not throw exceptions.
20.8.11.2 Class template function [func.wrap.func]
2 A callable object f of type F is Callable for argument types ArgTypes and return type R if the expression INVOKE(f, declval<ArgTypes>()..., R), considered as an unevaluated operand (Clause 5), is well formed (20.8.2).
20.8.2 Requirements [func.require]
2 Define INVOKE(f, t1, t2, ..., tN, R) as INVOKE(f, t1, t2, ..., tN) implicitly converted to R.
1 Define INVOKE(f, t1, t2, ..., tN) as follows:
... (all related to pointer-to-member types)
f(t1, t2, ..., tN) in all other cases.
In short, this means that std::function<short int()> can be used with any function that can be called with no arguments, and which has a return type that can be implicitly converted to short. long clearly can be implicitly converted to short, so there is no problem whatsoever.
If your compiler's library doesn't accept it, and you cannot upgrade to a more recent version, one alternative is to try boost::function instead.
Aaron McDaid points out lambdas as another alternative: if your library's std::function is lacking, you can write
std::function<short int()> f = A;
f = []() -> short int { return B(); };
but if you take this route, you can take it a step further and avoid std::function altogether:
short int (*f)() = A;
f = []() -> short int { return B(); };
This works because lambas that don't capture anything are implicitly convertible to a pointer-to-function type that matches the lambda's arguments and return type. Effectively, it's short for writing
short int B_wrapper() { return B(); }
...
f = B_wrapper;
Note: the conversion from long to short may lose data. If you want to avoid that, you can use std::function<long int()> or long int (*)() instead.
No, you can't do that in a statically typed language unless your types all have a common super type, and C++ doesn't have that for primitives. You would need to box them into an object, then have the function return the object.
However, if you did that, you may as well just keep an object pointer around and use that instead of a function pointer, especially since it's going to make it easier to actually do something useful with the result without doing casts all over the place.
For example, in a calculator I wrote in Java, I wanted to work with BigInteger fractions as much as possible to preserve precision, but fallback to doubles for operations that returned irrational numbers. I created a Result interface, with BigFractionResult and DoubleResult implementations. The UI code would call things like Result sum = firstOperand.add(otherOperand) and didn't have to care which implementation of add it was using.
The cleanest option that comes to mind is templates:
#include <iostream>
using namespace std;
template <typename T>
T foo() {
return 0;
}
int main() {
long a = foo<long>();
cout << sizeof a << " bytes with value " << a << endl;
int b = foo<int>();
cout << sizeof b << " bytes with value " << b << endl;
short c = foo<short>();
cout << sizeof c << " bytes with value " << c << endl;
return 0;
}
In ideone.com this outputs:
4 bytes with value 0
4 bytes with value 0
2 bytes with value 0
Hopefully this is what you needed.
If for some reason you really need to pass an actual function around, I would recommend looking into std::function and trying to write some template code using that.

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;
}

Thrust vector of type uint2: "has no member x" compiler error?

I have just started using the Thrust library. I am trying to make a vector of length 5 on the device. Her I am just setting the members of the first element vec[0]
#include<thrust/device_vector.h>
#include<iostream>
.
.
.
thrust::device_vector<uint2> vec(5);
vec[0]=make_uint2(4,5);
std::cout<<vec[0].x<<std::endl;
However for the above code I get the error
error: class "thrust::device_reference<uint2>" has no member "x"
1 error detected in the compilation of "/tmp/tmpxft_000020dc_00000000-4_test.cpp1.ii".
Where am I going wrong? I thought that accessing a member of a native CUDA vector data type such as uint2 with .x and .y was the correct way of doing .
As talonmies notes in his comment, you can't directly access the members of elements owned by a device_vector, or any object wrapped with device_reference. However, I wanted to provide this answer to demonstrate an alternative approach to your problem.
Even though device_reference doesn't allow you to access the members of the wrapped object, it is compatible with operator<<. This code should work as expected:
#include <thrust/device_vector.h>
#include <iostream>
// provide an overload for operator<<(ostream, uint2)
// as one is not provided otherwise
std::ostream &operator<<(std::ostream &os, const uint2 &x)
{
os << x.x << ", " << x.y;
return os;
}
int main()
{
thrust::device_vector<uint2> vec(5);
vec[0] = make_uint2(4,5);
std::cout << vec[0] << std::endl;
return 0;
}

Object allocation inline on the stack

What does that mean when it says 'object allocation inline on the stack'?
Especially the 'inline' bit
It means that all the data for the object is allocated on the stack, and will be popped off when the current method terminates.
The alternative (which occurs in C# and Java, or if you're using a pointer in C++) is to have a reference or pointer on the stack, which refers to the object data which is allocated on the heap.
I think the "inline" here just means "as part of the stack frame for this method" as opposed to existing separately from the method.
Well, you know what the stack is, right? If you declare a function in, say, C:
int foo() {
int bar = 42;
return bar;
}
When the function is called, some space is created for information about the function on the stack, and the integer bar is allocated there as well. When the function returns, everything in that stack frame is deallocated.
Now, in C++:
class A {
int a;
int b;
A(int x, int y) {
a = x;
b = y;
}
~A() { // destructor
cout << "A(" << a << "," << b << ") being deleted!" << endl;
}
}
void foo() {
A on_the_stack(1,2);
A *on_the_heap = new A(3,4);
}
In languages like Java, all objects are allocated on the heap (unless the compiler does some sort of optimization). But in some languages like C++, the class objects can go right on the stack just like ints or floats. Memory from the heap is not used unless you explicitly call new. Note that our on_the_heap object never gets deallocated (by calling delete on it), so it causes a memory leak. The on_the_stack object, on the other hand, is automatically deallocated when the function returns, and will have its destructor called prior to doing so.