PostgreSQL: Cant match function in DLL - postgresql

I created a custom DLL containing following function:
#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include "utils/geo_decls.h"
PG_MODULE_MAGIC;
/* by value */
PG_FUNCTION_INFO_V1(add_one);
Datum
add_one(PG_FUNCTION_ARGS)
{
int32 arg = PG_GETARG_INT32(0);
PG_RETURN_INT32(arg + 1);
}
But when i try to create it in PgAdmin4 using next command:
CREATE FUNCTION add_one(INTEGER) RETURNS INTEGER
AS '$libdir/new_func', 'add_one'
LANGUAGE C STRICT ;
It says:
ERROR:cannot match function "add_one" in file "C:/Program Files/PostgreSQL/14/lib/new_func.dll"
SQL-state: 42883
DLL was compiled according to this guide.
Is there any way to check the list of functions in a .dll using PGAdmin? Or how can i try to solve this?

After some time i found out the reason why my DLL could not been read.
I used wrong compiler.
First i used compiler from this site. Then i followed the guide in Question.
Few days ago I found another MinGW compiler. And finally everything works.
Compiler i used for the last time here.

Related

implicit declaration of function ‘bpf’

I have been studying BPF recently, but it is not proceeding because of a very basic problem.
I included linux/bpf.h as described in man bpf(2), but GCC can not find bpf function. This code is just for the test to make sure that GCC can find bpf function.
#include <linux/bpf.h>
int main()
{
bpf(0,(void *)0,0);
return 0;
}
GCC output is this.
$ gcc -o test bpf.c
bpf.c: In function ‘main’:
bpf.c:5:2: warning: implicit declaration of function ‘bpf’ [-Wimplicit-function-declaration]
bpf(0,(void *)0,0);
^~~
/usr/bin/ld: /tmp/cc4tjrUh.o: in function `main':
bpf.c:(.text+0x19): undefined reference to `bpf'
collect2: error: ld returned 1 exit status
I'm using Archlinux and linux kernel version is 4.20.11-arch1-1-ARCH.
Please help me how to include bpf function.
The manual page documents the system call bpf. While this is not intuitive, there is actually no function defined in the header <linux/bpf.h> that is simply called bpf(). Instead, you can do an indirect syscall with syscall(__NR_bpf, ...) (see also man syscall).
Projects relying on this syscall often define a wrapper that looks like this:
int bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
Here is an example from libbpf.

Passing a struct as a parameter in System Verilog

The following code works fine under Modelsim when the unused localparam is removed. It produces the error below if it is left in. If it is possible to use a struct to pass parameters to a module, what am I doing wrong? Many thanks.
typedef bit [7:0] myarr[2];
typedef struct { int a; myarr bytes; } mystruct;
module printer #(mystruct ms)();
// works fine if this is removed
localparam myarr extracted = ms.bytes;
initial
$display("Got %d and %p", ms.a, ms.bytes);
endmodule
parameter mystruct ms = '{ a:123, bytes:'{5, 6}};
module top;
printer #(.ms(ms)) DUT ();
endmodule
Here is the error. Compilation using vlog -sv -sv12compat produces no errors or warnings.
$ vsim -c -do "run -all; quit" top
Model Technology ModelSim - Intel FPGA Edition vlog 10.5c Compiler 2017.01 Jan 23 2017
(.......)
# ** Error: (vsim-8348) An override for an untyped parameter ('#dummyparam#0') must be integral or real.
I think the problem here is that you are assigning a whole unpacked array in one statement, which is not allowed. Try changing the myarr typedef to a packed array instead.
My workaround was to use a packed array. I didn't need to pack the whole struct.
I would happily upvote/accept someone else's answer if one appears. In particular, it would be helpful to confirm whether this is really a bug in Modelsim, or just an instance of a correct compilation error that could be made more helpful by including the location and parameter name.

C programming error when building and running

Im following a youtube tutorial by Bucky in C programming. Every time i use scanf and enter the input i get an error pop up. i typed these exact same codes from the video tutorial but mine does not work.
HERE'S THE CODE:
int main()
{
int age;
printf("How old are you?\n");
scanf("%d, &age");
if (age>= 18){
printf("You may enter this website!");
}
if(age<18){
printf("nothing to see here!");
}
return 0;
}
It only works after compiling and running. But after i input the age, an error window pops up and says "A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available."
I'm sure the codes are correct but What is causing this? Help me please so i can move forward.
You placed quote in incorrect place in scanf:
scanf("%d, &age");
should be:
scanf("%d", &age);
C language is limited in a ways to implement functions with variable numbers of arguments (which scanf is, since it's can read variable number of variables from terminal), so this is why such errors are possible.
Modern compilers will warn you about incorrect scanf usage, e.g. GCC will give a warning:
1.c: In function ‘main’:
1.c:8:1: warning: format ‘%d’ expects a matching ‘int *’ argument [-Wformat=]
scanf("%d, &age");
^
By the way, your example misses
#include <stdio.h>

Need of using MACROS like module_init and module_exit while writing Loadable Kernel Modules

What is the need of using MACROs like module_init and module_exit while writing Loadable Kernel Modules? Also, Why are we using MACROs like __init or __exit. Even though we can do the job without using them.
Without MACROS
/*
Without using MACROS
Author: Sricharan Chiruvolu
Date: 14 Dec 2014
*/
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void){
printk(KERN_ALERT "This is our first program.");
return 0;
}
void cleanup_module(void){
printk(KERN_ALERT "End of our first program.");
}
With MACROs
/*
Edited first.c; Added macros module_init and module_exit
Author: Sricharan Chiruvolu
Date: 14 Dec 2014
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
static int __init first_init(void)
{
printk(KERN_ALERT "This is our first program.");
return 0;
}
static void __exit first_exit(void)
{
printk(KERN_ALERT "End of our first program.");
}
module_init(first_init);
module_exit(first_exit);
What is the difference?
module_{init,exit}() adds the necessary boilerplate to initialize / cleanup the module and run the entry / exit code, when the module file is loaded / unloaded into or from the kernel space.
__init is telling kernel that this function is executed once and
never come back mainly for built-in drivers while module_init() is to initialize the module when it is being insmod.
Refer Rubini & Corbet
"
The attribute __init, , will cause the initialization function
to be discarded, and its memory reclaimed, after initialization is
complete. It only works, however, for built-in drivers; it has no
effect on modules. __exit, instead, causes the omission of the marked
function when the driver is not built as a module; again, in modules,
it has no effect.
The use of __init (and __initdata for data items) can reduce the
amount of memory used by the kernel. There is no harm in marking
module initialization functions with __init, even though currently
there is no benefit either. Management of initialization sections has
not been implemented yet for modules, but it's a possible enhancement
for the future.
"

Macro without definition in C

What is the use/applicability of macro function without definition:
#ifndef __SYSCALL
#define __SYSCALL(a, b)
#endif
One can find this macro in Linux system in header file /usr/include/asm/msr.h
I also notice macro of following kind.
#define _M(x) x
And only reason to defined this kind of macro that I can think to make code uniform. like in #define SOMETHING (1 << 0). Is there any other hidden(better) use of this kind of macros?
An answer with example will be very helpful. Also
can someone provide me a text/link to read about this.
One of the most common case of a macro of this form:
#define _M(x) x
is to provide backwards compatibility for compilers that only supported the original K&R dialect of C, that predated the now-ubiquitous ANSI C dialect. In the original K&R dialect of the language, function arguments were not specified when declaring the function. In 1989, ANSI standardized the language and incorporated a number of improvements, including function prototypes that declared the number of type of arguments.
int f(int x, double y); /* ANSI C. K&R compilers would not accept this */
int f(); /* Function declared in the original K&R dialect */
While compilers that support the original K&R dialect of C are rare (or extinct) these days, a lot of software was written when both kinds of compilers needed to be supported, and macros provided an easy way to support both. There are still a lot of headers laying about that provide this backwards compatibility.
To provide backwards compatibility for K&R compilers, many header files have the following:
#if ANSI_PROTOTYPES
# define _P(x) x
#else
# define _P(x) ()
#endif
...
int f _P((int x, double y));
If the ANSI_PROTOTYPES definition has been correctly set (either by the user or by some prior #ifdef logic), then you get the desired behavior:
If ANSI_PROTOTYPES is defined, the definition expands to int f(int x, double y).
If ANSI_PROTOTYPES is not defined, the definition expands to int f()
This is often used with conditional expressions to disable a macro by causing it to be preprocessed to nothing. For example (simplified):
#ifdef DEBUG
#define ASSERT(x) if(!(x)) { abort(); }
#else
#define ASSERT(x) /* nothing */
#endif
Just a follow-up to my question.
I got good answers. but I am also adding some more helpful example where macros without definition are useful, one can find it helpful in future:
(1): Why do I see THROW in a C library?
uses to share header file between C and C++. The macro name is _THROW(x)
#ifdef __cplusplus
#define __THROW(x) throw(x)
#else
#define __THROW(x)
#endif
(2) to eliminate warnings when a function parameter isn't used:
This use is for c++. In C it will cause an error too few arguments But in C++ it works with no error: (codepad linked)
#define UNUSED(x)
int value = 0;
int foo(int UNUSED(value))
{
return 42;
}
int main(){
foo(value);
}
(for this I added c++ tag in my question)
Additionally,
(3): The use of #define _M(x) x is as follows, just to makes code line up uniformly:
/* Signed. */
# define INT8_C(c) c
# define INT16_C(c) c
# define INT32_C(c) c
# if __WORDSIZE == 64
# define INT64_C(c) c ## L
# else
# define INT64_C(c) c ## LL
# endif
the file is: /usr/include/stdint.h
It means that code that uses that macro will conditionally preprocess away to nothing.
As simple examples, consider debug code, logging or assertions.
This is probably a debug macro or an platform macro. For example lets say I have a debugger attached to INT3. I might have this when I'm debugging
#define debug() INT3()
Then to be safe I'll add this to production code (to make sure I took them all out)
#define debug()
This looks like something similar
It could be that in some cases on some systems this code needs to make a call -- for example on a certain CPU architecture or OS. But on your system it is just no-oped.