R, SQL problems debugging a package, debug(.Call) - sql-server

I am trying to debug an error that I think is in a pacakge. While I am stepping through the code with the debugger there is a section of code that produces the error I am getting.
if (fetch == FALSE | nrow(data) < 1) {
stat <- .Call("RODBCExecute", attr(channel, "handle_ptr"),
data, as.integer(rows_at_time))
if (stat == -1L) {
if (errors) {
stop(paste0(RODBC::odbcGetErrMsg(channel), collapse = "\n"))
}
else {
return(stat)
}
}
.Call() sets stat = -1 and this produces the error. I tried to use debug(.Call) but this is not working. From what I understand from searching online is that the .Call() uses compiled C/C++ code. Is there anyway to debug this code any farther?

You are correct: .Call("thisCFunction",...) calls a C/C++ function, loaded in by the package from the .so file, that is called thisCFunction in it's C++ declaration.
I would not try to debug this; you would have to go through the C++ source code by hand to figure out exactly what's happening, which would require a firm understanding of not only general C/C++ but also R's API (and the author's style of programming). Not trying to be rude, but I would guess if you are unfamiliar with .Call and what it does, you're probably unfamiliar with R's API.
An easier first step would be to track down exactly what is being supplied to this function and try to figure out why it's not what RODBCExecute expects. Given that the authors have written an error message, I would check to see if that error message helps point out the issue. My guess is that the real error occurred much earlier and this results in providing invalid arguments to RODBCExecute.

Related

Redefinition + Conflicting types errors, but nothing is being redefined or actually conflicting

I am getting some errors I'm not really understanding, nor a solution to get around them.
I am working on a ESP32 Bluetooth connection using esp-IDF, VSCode, and PlatformIO.
I have a section of code
esp_hid_gap_init(ESP_BT_MODE_BTDM);
esp_ble_gattc_register_callback(esp_hidh_gattc_event_handler);
esp_hidh_config_t config = {
.callback = hidh_callback,
};
ESP_ERROR_CHECK( esp_hidh_init(&config) );
btkeyboard_queue_init();
ESP_ERROR_CHECK( err );
That is initialising Bluetooth and looking for connections, it requires the use of a header called bluetoothkeyboard.h which i pulled from: https://github.com/bozont/Duke3D/tree/master/components/SDL/bluetoothkeyboard
When i include this header in my main.c i get 11 errors, 10 from bluetoothkeyboard.h and one from SDL_event.h (also in the github repo i linked) they are:
The code is all unmodified and it clearly works in his demo and many others who have used it and I'm not keen on messing with a third-party library, I just don't understand what's going on and how to move around it.
Earlier it was compiling but I was getting a Collect2.exe: error: ld returned 1 exit status. To get around it I tried moving the section above into my main.c just to check but ended up in this world of pain.
I get the messages are telling me I am redefining a function, but you use the right click find all and it just references itself so I'm at a loss.
EDIT
Here is an example of one of the compiler errors:
And my CMAKE file
cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(TTGODemo)
EDIT2**
Ok so taking the comments on board, I removed the second include of BTKeyboard out of the ESP_HID_GAP.H i had. I had put it in there to cover a static constant char that was declared in only bluetoothkeyboard.h so idk how they were communicating.
That was done, I got the following wall of Text:
I think there are functions in the Bluetooth libraries that are also in the esp_hid_gap.h that it's getting hung up on. But i don't really know what to do about it, i could try removing the BT libraries but that will probably make things worse.
Thoughts?

C HTTP uriparser lib setup

I am currently working on a simple HTTP proxy as an educational personal project. I am fairly new to socket programming, C, and the need for and use of specific libraries.
After reading up on different ways to parse HTTP request URLs in C, I found that using parsing libraries, like uriparser, seemed to be the most popular and the safest choice.
Unfortunately, I have really been struggling to get this library working properly.
After downloading the library from here, I unpacked the "tarball" and ran the following:
./configure
make
sudo make install
The output for 'sudo make install' claimed that that the libraries were installed in '/usr/local/lib', which they have been. However, when I try to use a UriUriA obj as shown in Uriparser's documentation here, the object is clearly not recognized.
Is this library installed in the right place?
Am I missing something obvious like not including a header file for this library? I could't find one in the linked documentation.
RELEVANT CODE:
(functionality is pretty bare right now, mainly pointing at UriUri instantiation which still produces unknown type compile error. I still have yet to be able to use the lib)
int parse_req(char * buffer, char * url){
UriUriA uri;
char *reqs[] = {"GET ", "CONNECT "};
int i = 0;
//Determine Request Types
while(reqs[i] != NULL){
int check = strncmp(reqs[i],buffer,strlen(reqs[i]));
if (check == 0){
break;
}
i++;
}
return -1;
}
If anyone has any experience with this library, any help is appreciated! Thanks!

Why are some debug symbols missing and how to track them?

I am currently debugging a Kernel module and to this purpose, I built the whole kernel with debug information (produces kallsyms, etc ...).
When I try nm my_module.ko, I get the list of symbols included by my module. All is allright except that some symbols are kind of missing as they do not appear in the symbol list. My feeling about this is that the related functions are being automatically inlined.
Anyway, when running the kernel with qemu-kgdb/gdb, I am able to see that the "missing" function is called. This means the compiler did not wipe it out because it was never used in any code path (hence my "feeling").
Since the symbol does not appear, I can't set a breakpoint on it and gdb won't unroll it so that I can see the running code path - understand I don't know how to tell gdb to unroll it.
Unfortunately, I want to see this part of the code path ... How can I do so ?
EDIT : As suggested in Tom's answer, I tried using the file:line syntax as below :
My code file looks like this :
int foo(int arg) // The function that I suspect to be inlined - here is line 1
{
/* Blabla */
return 42;
}
void foo2(void)
{
foo(0); // Line 9
}
I tried b file.c:1, and the breakpoint was hit but the foo() function is not unrolled.
Of course, I am producing debug symbols, since I also set a breakpoint to foo2 to check what happened (which worked well).
You don't say what version of gdb you are using.
Very old versions of gdb don't have any support for inline functions. This was true for 6.8 and maybe even 7.0 -- I don't recall. You can look at the NEWS file for your gdb to see.
Then there were some versions of gdb that supported breakpoints on inline functions, but only using the "file:line" syntax. So what you would do is look up the function in your editor, and find its line number and enter, e.g.:
(gdb) break myfile.c:777
Even more recent versions of gdb, starting with 7.4 or 7.5 (I forget) will handle "break function" just fine if "function" was inlined.
All of this only works if you have debuginfo available. So if you tried this, and it failed, either you have an older gdb, or you forgot to use -g.
There's no good way inside gdb to see what objects in a compilation were missing -g. You can see it pretty easily from the shell, though, by running "readelf -WS" on the .o files, and looking for files that don't have a .debug_info section.
Setting a breakpoint to the signature line of the function did not work. But setting one to the line of an instruction of the inlined function solved the problem for me. For instance, considering the following function inline_foo, found in myfile.c:
inline int inline_foo(int arg) // l.1
{
int a_var = 0;
do_smth(&a_var);
do_some_other_thing(); // l.5
if (a_var) {
a_var = blob();
} else {
a_var = blub();
return a_var; // l.10
}
I was trying b myfile.c:1, which did not appear to work. But if I tried b myfile.c:3 instead, the breakpoint was well handled by GDB.
Since the technique is the same as the one described previously by Tom, I'll accept his answer.

Packing some of OpenGL commands in a dynamic library, got "wglMakeCurrent failed"

I am trying to put some of my OpenGL commands in an external library. To be specific, I'm currently drawing a 3d scene with Tcl language, and now I want to put some openGl functions (drawing functions for example) in a dll (programmed in C) and load it from Tcl. This is because I will have large quantity of data to render. With a compiled C library it will run faster.
However, I'm having problem doing so. When I launch my program, a "wglMakeCurrent failed" error occurs just before the second run of my display callback function.
Both Tcl and C parts report the same thread ID. So I suppose that I do not have to worry about multithreading and context sharing issue.
This is my display callback function:
proc DisplayCallback { toglwin } {
# adjust camera and objects ...
if {[catch {set cubeList [ExternalDrawCube $::cube(size)]}]} {
puts "Catch drawcube error !!"
} else {
glCallList $cubeList
}
$toglwin swapbuffers
}
Any help or suggestions?
I finally found the stupid bug I did in my code..
In the C function, I wrote "glEnd" instead of "glEnd()". I forgot it when converting the code from tcl!
Hopefully this would help people who have similar wglMakeCurrent failed error...
Good debugging for all

C Loading Code dynamically in the same way as the Java Compiler Api 7

I have the following use case which I had previously solved in Java, but am now required to port the program to C.
I had a method A which called a method do_work() belonging to an abstract class Engine. Each concrete implementation of the class was constructed as follows:
users would submit the definition of the do_work() method . If this definition was correct, the programmer would construct a concrete implementation of the Engine class using the Java Compiler API. (code for this is included for reference below).
How can I do something similar in C:
I now have a structure Engine, with a function pointer to the do_work() method. I want users to be able to submit this method at run time (note: this only occurs once, on startup, once the Engine structure has been constructed, I do not want to change it) via command line.
How could I go about this? I've read around suggestions stating that I would have to use assembly to do this, others stating that this was not possible, but none of them giving a good explanation or references. Any help would be appreciated.
The solution doesn't need to be compatible with 32/64 bits machines, as the program this is written for is only for 64 bits machines.
For reference, the Java Code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager stdFileManager = compiler
.getStandardFileManager(null, Locale.getDefault(), null);
Iterable<? extends JavaFileObject> compilationUnits = null;
String[] compileOptions = new String[] { "-d", "bin" };
Iterable<String> compilationOptions = Arrays.asList(compileOptions);
SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject(
"package.adress",getCode());
JavaFileObject javaFileObjects[] = new JavaFileObject[] { fileObject };
compilationUnits = Arrays.asList(javaFileObjects);
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
CompilationTask compilerTask = compiler.getTask(null, stdFileManager,
diagnostics, compilationOptions, null, compilationUnits);
boolean status = compilerTask.call();
if (!status) {// If compilation error occurs
/* Iterate through each compilation problem and print it */
String result = "";
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
result = String.format("Error on line %d in %s",
diagnostic.getLineNumber(), diagnostic);
}
Exception e = new Exception(result);
throw e;
}
stdFileManager.close();// Close the file manager
/*
* Assuming that the Policy has been successfully compiled, create a new
* instance
*/
Class newEngine = Class
.forName("package.name");
Constructor[] constructor = newPolicy.getConstructors();
constructor[0].setAccessible(true);
etc.
}
In C all code must be compiled to native one before usage, so the only way for you is to use command line compiler to build code submitted by users. It may be GNU C++ compiler for example, or Visual C++ compiler (but for Visual C++ I don't know what about legal problems, is it permitted by license to do that).
So, first of all, select your compiler, probably GNU one.
Next, you can compile it as executable program or as DLL (assuming your software is for
Windows). If you decide to compile it to DLL, you have to use Win32 function LoadLibrary to load new built DLL into your process, and after that you can use GetProcAddress function to get method address and call it dynamically from C++ (you must implement a function wrapper and make it public in DLL).
If you decide to compile it as EXE file, you have to use CreateProcess function to run your code, send parameters via command line and receive data, may be, with pipe (see CreatePipe function), or may be with temporary file, or any other interprocess communication way available in Windows.
I think in your situation it is better to compile to EXE file, because in DLL if user code is buggy your main program may crash.

Resources