[Gambas-user] Get last errno from external function call

Matheus Castello matheus at castello.eng.br
Wed Oct 5 15:33:59 CEST 2022



On 10/5/2022 10:21 AM, Bruce Steers wrote:
> Is errno global to the application or the entire system?
> I would imagine just the application.
>

It's thread-local https://man7.org/linux/man-pages/man3/errno.3.html

> I would also imagine the gambas error system works independent of the 
> libraries one?
> 

Yeah, and I think this would be an important feature to add the Gambas3 
runtime, something similar to SetLastError from .NET, because not all 
libraries use error code as a return and the runtime overwrite errno 
while processing the external call.


> How to get the libraries errno I do not know, sorry.
>

Including errno.h : https://man7.org/linux/man-pages/man3/errno.3.html

BR,
Matheus Castello

> Good luck
> BruceS
> 
> On Wed, 5 Oct 2022, 05:09 Matheus Castello, <matheus at castello.eng.br 
> <mailto:matheus at castello.eng.br>> wrote:
> 
>     For example some calls from libgpiod return structs, if the return is a
>     null pointer then you have an error. For this cases libgpiod set the
>     error code to the `errno` and you need to read it as soon the function
>     returns the null pointer.
> 
>     Reference:
>     https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/include/gpiod.h#n28 <https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/include/gpiod.h#n28>
> 
> 
>     On 10/5/2022 12:25 AM, Jussi Lahtinen wrote:
>      > You mean an error occurring inside the library? I don't think
>     there is
>      > any other way, than the library itself reporting the error code.
>      > I don't see why using __errno_location inside the library would
>     fail.
>      > Are you calling it from the Gambas code?
> 
>     Yeah, I add an `External` declaration to it to try to get the errno,
>     but
>     as I said "surely the runtime (I mean the Gambas3 runtime) must be
>     making other calls that are overwriting the errno" and I'm always
>     getting `errno = 0` that means success.
> 
>     .NET for example have an annotation to store the errno from an external
>     function the `SetLastError`:
>     https://github.com/dotnet/iot/blob/main/src/System.Device.Gpio/Interop/Unix/libgpiod/Interop.libgpiod.cs#L49 <https://github.com/dotnet/iot/blob/main/src/System.Device.Gpio/Interop/Unix/libgpiod/Interop.libgpiod.cs#L49>
> 
>     I was looking for something similar in Gambas3.
> 
>     BR,
>     Matheus Castello
> 
> 
>      > If this is about a missing error message, then are you reading it
>     from
>      > stderr?
>      > You need to give more details and/or some code to see.
>      >
>      > Jussi
>      >
>      >
>      >
>      >
>      > On Wed, Oct 5, 2022 at 5:54 AM Matheus Castello
>     <matheus at castello.eng.br <mailto:matheus at castello.eng.br>
>      > <mailto:matheus at castello.eng.br
>     <mailto:matheus at castello.eng.br>>> wrote:
>      >
>      >     Hi guys,
>      >
>      >     I'm new to Gambas3, I'm trying to use it for embedded devices
>     and I'm
>      >     doing some external function calls. There is some way in
>     Gambas3 to
>      >     get,
>      >     or Gambas runtime stores, the last errno from a external
>     function call?
>      >
>      >     I tried to get it directly from libc `__errno_location` but
>     surely the
>      >     runtime must be making other calls that are overwriting the
>     errno.
>      >
>      >     Thanks in advance,
>      >     Matheus Castello
>      >
>      >     ----[ http://gambaswiki.org/wiki/doc/netiquette
>     <http://gambaswiki.org/wiki/doc/netiquette>
>      >     <http://gambaswiki.org/wiki/doc/netiquette
>     <http://gambaswiki.org/wiki/doc/netiquette>> ]----
>      >
>      >
>      >
>      > ----[ http://gambaswiki.org/wiki/doc/netiquette
>     <http://gambaswiki.org/wiki/doc/netiquette> ]----
> 
>     ----[ http://gambaswiki.org/wiki/doc/netiquette
>     <http://gambaswiki.org/wiki/doc/netiquette> ]----
> 
> 
> 
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----


More information about the User mailing list