diff mbox

[U-Boot,01/19] lib: errno: introduce errno_str(): returns errno related message

Message ID 1412801335-1591-2-git-send-email-p.marczak@samsung.com
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Przemyslaw Marczak Oct. 8, 2014, 8:48 p.m. UTC
The functions error's numbers are standarized - but the error
messages are not.

The errors are often handled with unclear error messages,
so why not use an errno standarized messages.

Advantages:
- This could decrease the binary size.
- Appended with a detailed information,
  the error message will be clear.

This commit introduces new function:
- const char *errno_to_str(int errno)

The functions returns a pointer to the errno corresponding text message:
- if errno is null or positive number - a pointer to "Success" message
- if errno is negative - a pointer to errno related message

Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
---
 include/errno.h |   3 ++
 lib/Makefile    |   1 +
 lib/errno_str.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 lib/errno_str.c

Comments

Joakim Tjernlund Oct. 9, 2014, 6:46 a.m. UTC | #1
> From: Przemyslaw Marczak <p.marczak@samsung.com>
> 
> The functions error's numbers are standarized - but the error
> messages are not.
> 
> The errors are often handled with unclear error messages,
> so why not use an errno standarized messages.
> 
> Advantages:
> - This could decrease the binary size.

Having an array of string ptrs adds some extra space needs.
Each str needs a ptr and that ptr needs relocation, 8 bytes on 32 bits

If you want to save space do this instead
static const char const errno_message[] = 
  "Success\0Operation not permitted\0No such file or directory" etc.
Then count "\0" to find the error msg.

      Jocke
Przemyslaw Marczak Oct. 9, 2014, 4:23 p.m. UTC | #2
Hello Joakim,

On 10/09/2014 08:46 AM, Joakim Tjernlund wrote:
>> From: Przemyslaw Marczak <p.marczak@samsung.com>
>>
>> The functions error's numbers are standarized - but the error
>> messages are not.
>>
>> The errors are often handled with unclear error messages,
>> so why not use an errno standarized messages.
>>
>> Advantages:
>> - This could decrease the binary size.
>
> Having an array of string ptrs adds some extra space needs.
> Each str needs a ptr and that ptr needs relocation, 8 bytes on 32 bits
>
> If you want to save space do this instead
> static const char const errno_message[] =
>    "Success\0Operation not permitted\0No such file or directory" etc.
> Then count "\0" to find the error msg.
>
>        Jocke
>

Is this really a problem to add some array with the pointers?

You are right, this array requires some additional space, but this is 
not the main reason of introducing this function. This can be enabled 
optional, so maybe for the less memory and slower devices this shouldn't 
be used - but in the other way, we see many text messages in the code 
that could be replaced with the one from that array.
So, which is better?

This helps me sometimes, so I added this as some extra feature.

Best Regards,
Simon Glass Oct. 9, 2014, 10:53 p.m. UTC | #3
On 9 October 2014 10:23, Przemyslaw Marczak <p.marczak@samsung.com> wrote:

> Hello Joakim,
>
> On 10/09/2014 08:46 AM, Joakim Tjernlund wrote:
>
>> From: Przemyslaw Marczak <p.marczak@samsung.com>
>>>
>>> The functions error's numbers are standarized - but the error
>>> messages are not.
>>>
>>> The errors are often handled with unclear error messages,
>>> so why not use an errno standarized messages.
>>>
>>> Advantages:
>>> - This could decrease the binary size.
>>>
>>
>> Having an array of string ptrs adds some extra space needs.
>> Each str needs a ptr and that ptr needs relocation, 8 bytes on 32 bits
>>
>> If you want to save space do this instead
>> static const char const errno_message[] =
>>    "Success\0Operation not permitted\0No such file or directory" etc.
>> Then count "\0" to find the error msg.
>>
>>        Jocke
>>
>>
> Is this really a problem to add some array with the pointers?
>
> You are right, this array requires some additional space, but this is not
> the main reason of introducing this function. This can be enabled optional,
> so maybe for the less memory and slower devices this shouldn't be used -
> but in the other way, we see many text messages in the code that could be
> replaced with the one from that array.
> So, which is better?
>
> This helps me sometimes, so I added this as some extra feature.
>

I think it looks good as it is as it is simple. My only suggestion is that
the CONFIG should not remove the function, merely make it return an empty
string or "Error strings unavailable". Otherwise you will force #ifdefs in
the code.

Regards,
Simon
Joakim Tjernlund Oct. 10, 2014, 5:03 a.m. UTC | #4
Przemyslaw Marczak <p.marczak@samsung.com> wrote on 2014/10/09 18:23:54:
> 
> Hello Joakim,
> 
> On 10/09/2014 08:46 AM, Joakim Tjernlund wrote:
> >> From: Przemyslaw Marczak <p.marczak@samsung.com>
> >>
> >> The functions error's numbers are standarized - but the error
> >> messages are not.
> >>
> >> The errors are often handled with unclear error messages,
> >> so why not use an errno standarized messages.
> >>
> >> Advantages:
> >> - This could decrease the binary size.
> >
> > Having an array of string ptrs adds some extra space needs.
> > Each str needs a ptr and that ptr needs relocation, 8 bytes on 32 bits
> >
> > If you want to save space do this instead
> > static const char const errno_message[] =
> >    "Success\0Operation not permitted\0No such file or directory" etc.
> > Then count "\0" to find the error msg.
> >
> >        Jocke
> >
> 
> Is this really a problem to add some array with the pointers?

Probably not, I only mentioned this because you claimed it could reduces 
size
as if that was important to you.

 Jocke

> 
> You are right, this array requires some additional space, but this is 
> not the main reason of introducing this function. This can be enabled 
> optional, so maybe for the less memory and slower devices this shouldn't 

> be used - but in the other way, we see many text messages in the code 
> that could be replaced with the one from that array.
> So, which is better?
> 
> This helps me sometimes, so I added this as some extra feature.
Przemyslaw Marczak Oct. 10, 2014, 11:49 a.m. UTC | #5
Hello Joakim,

On 10/10/2014 07:03 AM, Joakim Tjernlund wrote:
> Przemyslaw Marczak <p.marczak@samsung.com> wrote on 2014/10/09 18:23:54:
>>
>> Hello Joakim,
>>
>> On 10/09/2014 08:46 AM, Joakim Tjernlund wrote:
>>>> From: Przemyslaw Marczak <p.marczak@samsung.com>
>>>>
>>>> The functions error's numbers are standarized - but the error
>>>> messages are not.
>>>>
>>>> The errors are often handled with unclear error messages,
>>>> so why not use an errno standarized messages.
>>>>
>>>> Advantages:
>>>> - This could decrease the binary size.
>>>
>>> Having an array of string ptrs adds some extra space needs.
>>> Each str needs a ptr and that ptr needs relocation, 8 bytes on 32 bits
>>>
>>> If you want to save space do this instead
>>> static const char const errno_message[] =
>>>     "Success\0Operation not permitted\0No such file or directory" etc.
>>> Then count "\0" to find the error msg.
>>>
>>>         Jocke
>>>
>>
>> Is this really a problem to add some array with the pointers?
>
> Probably not, I only mentioned this because you claimed it could reduces
> size
> as if that was important to you.
>
>   Jocke
>

I meant that it will reduce the size, if we could change the whole code 
"common message string parts" with a pointer to some standard message.

>>
>> You are right, this array requires some additional space, but this is
>> not the main reason of introducing this function. This can be enabled
>> optional, so maybe for the less memory and slower devices this shouldn't
>
>> be used - but in the other way, we see many text messages in the code
>> that could be replaced with the one from that array.
>> So, which is better?
>>
>> This helps me sometimes, so I added this as some extra feature.
>
>
>

Your notice about the size was right, but I hope that U-Boot is used for 
machines, in which this doesn't make a difference - and the code
is still simple and the "error messages" - means what they should mean.

Best Regards,
Tom Rini Oct. 22, 2014, 3:31 p.m. UTC | #6
On Wed, Oct 08, 2014 at 10:48:37PM +0200, Przemyslaw Marczak wrote:

> The functions error's numbers are standarized - but the error
> messages are not.
> 
> The errors are often handled with unclear error messages,
> so why not use an errno standarized messages.
> 
> Advantages:
> - This could decrease the binary size.
> - Appended with a detailed information,
>   the error message will be clear.
> 
> This commit introduces new function:
> - const char *errno_to_str(int errno)
> 
> The functions returns a pointer to the errno corresponding text message:
> - if errno is null or positive number - a pointer to "Success" message
> - if errno is negative - a pointer to errno related message
> 
> Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>

In and of itself,

Reviewed-by: Tom Rini <trini@ti.com>
Simon Glass Dec. 11, 2014, 3:25 a.m. UTC | #7
On 22 October 2014 at 09:31, Tom Rini <trini@ti.com> wrote:
> On Wed, Oct 08, 2014 at 10:48:37PM +0200, Przemyslaw Marczak wrote:
>
>> The functions error's numbers are standarized - but the error
>> messages are not.
>>
>> The errors are often handled with unclear error messages,
>> so why not use an errno standarized messages.
>>
>> Advantages:
>> - This could decrease the binary size.
>> - Appended with a detailed information,
>>   the error message will be clear.
>>
>> This commit introduces new function:
>> - const char *errno_to_str(int errno)
>>
>> The functions returns a pointer to the errno corresponding text message:
>> - if errno is null or positive number - a pointer to "Success" message
>> - if errno is negative - a pointer to errno related message
>>
>> Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
>
> In and of itself,
>
> Reviewed-by: Tom Rini <trini@ti.com>

Applied to u-boot-dm, thanks!

- Simon
Przemyslaw Marczak Dec. 11, 2014, 10:11 a.m. UTC | #8
Hello Simon,

On 12/11/2014 04:25 AM, Simon Glass wrote:
> On 22 October 2014 at 09:31, Tom Rini <trini@ti.com> wrote:
>> On Wed, Oct 08, 2014 at 10:48:37PM +0200, Przemyslaw Marczak wrote:
>>
>>> The functions error's numbers are standarized - but the error
>>> messages are not.
>>>
>>> The errors are often handled with unclear error messages,
>>> so why not use an errno standarized messages.
>>>
>>> Advantages:
>>> - This could decrease the binary size.
>>> - Appended with a detailed information,
>>>    the error message will be clear.
>>>
>>> This commit introduces new function:
>>> - const char *errno_to_str(int errno)
>>>
>>> The functions returns a pointer to the errno corresponding text message:
>>> - if errno is null or positive number - a pointer to "Success" message
>>> - if errno is negative - a pointer to errno related message
>>>
>>> Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
>>
>> In and of itself,
>>
>> Reviewed-by: Tom Rini <trini@ti.com>
>
> Applied to u-boot-dm, thanks!
>
> - Simon
>

Thank, maybe today or tomorrow I will send the I2C for exynos and next I 
can continue work on pmic.

Best regards,
Simon Glass Dec. 11, 2014, 1:24 p.m. UTC | #9
Hi Przemyslaw,

On 11 December 2014 at 03:11, Przemyslaw Marczak <p.marczak@samsung.com> wrote:
> Hello Simon,
>
>
> On 12/11/2014 04:25 AM, Simon Glass wrote:
>>
>> On 22 October 2014 at 09:31, Tom Rini <trini@ti.com> wrote:
>>>
>>> On Wed, Oct 08, 2014 at 10:48:37PM +0200, Przemyslaw Marczak wrote:
>>>
>>>> The functions error's numbers are standarized - but the error
>>>> messages are not.
>>>>
>>>> The errors are often handled with unclear error messages,
>>>> so why not use an errno standarized messages.
>>>>
>>>> Advantages:
>>>> - This could decrease the binary size.
>>>> - Appended with a detailed information,
>>>>    the error message will be clear.
>>>>
>>>> This commit introduces new function:
>>>> - const char *errno_to_str(int errno)
>>>>
>>>> The functions returns a pointer to the errno corresponding text message:
>>>> - if errno is null or positive number - a pointer to "Success" message
>>>> - if errno is negative - a pointer to errno related message
>>>>
>>>> Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
>>>
>>>
>>> In and of itself,
>>>
>>> Reviewed-by: Tom Rini <trini@ti.com>
>>
>>
>> Applied to u-boot-dm, thanks!
>>
>> - Simon
>>
>
> Thank, maybe today or tomorrow I will send the I2C for exynos and next I can
> continue work on pmic.

OK sounds good. I've pushed DM I2C things to dm/master, although not
Tegra yet while I wait on the nyan-big patch.

Regards,
Simon
diff mbox

Patch

diff --git a/include/errno.h b/include/errno.h
index e24a33b..14ac3cb 100644
--- a/include/errno.h
+++ b/include/errno.h
@@ -6,4 +6,7 @@  extern int errno;
 
 #define __set_errno(val) do { errno = val; } while (0)
 
+#ifdef CONFIG_ERRNO_STR
+const char *errno_str(int errno);
+#endif
 #endif /* _ERRNO_H */
diff --git a/lib/Makefile b/lib/Makefile
index 320197a..6bb2e9b 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -53,6 +53,7 @@  endif
 obj-$(CONFIG_ADDR_MAP) += addr_map.o
 obj-y += hashtable.o
 obj-y += errno.o
+obj-$(CONFIG_ERRNO_STR) += errno_str.o
 obj-y += display_options.o
 obj-$(CONFIG_BCH) += bch.o
 obj-y += crc32.o
diff --git a/lib/errno_str.c b/lib/errno_str.c
new file mode 100644
index 0000000..0ba950e
--- /dev/null
+++ b/lib/errno_str.c
@@ -0,0 +1,147 @@ 
+/*
+ * Copyright (C) 2014 Samsung Electronics
+ * Przemyslaw Marczak <p.marczak@samsung.com>
+ *
+ * SDPX-License-Identifier:	GPL-2.0+
+ */
+#include <common.h>
+#include <errno.h>
+
+#define ERRNO_MSG(errno, msg)	msg
+#define SAME_AS(x)		(const char *)&errno_message[x]
+
+static const char * const errno_message[] = {
+	ERRNO_MSG(0, "Success"),
+	ERRNO_MSG(EPERM, "Operation not permitted"),
+	ERRNO_MSG(ENOEN, "No such file or directory"),
+	ERRNO_MSG(ESRCH, "No such process"),
+	ERRNO_MSG(EINTR, "Interrupted system call"),
+	ERRNO_MSG(EIO, "I/O error"),
+	ERRNO_MSG(ENXIO, "No such device or address"),
+	ERRNO_MSG(E2BIG, "Argument list too long"),
+	ERRNO_MSG(ENOEXEC, "Exec format error"),
+	ERRNO_MSG(EBADF, "Bad file number"),
+	ERRNO_MSG(ECHILD, "No child processes"),
+	ERRNO_MSG(EAGAIN, "Try again"),
+	ERRNO_MSG(ENOMEM, "Out of memory"),
+	ERRNO_MSG(EACCES, "Permission denied"),
+	ERRNO_MSG(EFAULT, "Bad address"),
+	ERRNO_MSG(ENOTBL, "Block device required"),
+	ERRNO_MSG(EBUSY, "Device or resource busy"),
+	ERRNO_MSG(EEXIST, "File exists"),
+	ERRNO_MSG(EXDEV, "Cross-device link"),
+	ERRNO_MSG(ENODEV, "No such device"),
+	ERRNO_MSG(ENOTDIR, "Not a directory"),
+	ERRNO_MSG(EISDIR, "Is a directory"),
+	ERRNO_MSG(EINVAL, "Invalid argument"),
+	ERRNO_MSG(ENFILE, "File table overflow"),
+	ERRNO_MSG(EMFILE, "Too many open files"),
+	ERRNO_MSG(ENOTTY, "Not a typewriter"),
+	ERRNO_MSG(ETXTBSY, "Text file busy"),
+	ERRNO_MSG(EFBIG, "File too large"),
+	ERRNO_MSG(ENOSPC, "No space left on device"),
+	ERRNO_MSG(ESPIPE, "Illegal seek"),
+	ERRNO_MSG(EROFS, "Read-only file system"),
+	ERRNO_MSG(EMLINK, "Too many links"),
+	ERRNO_MSG(EPIPE, "Broken pipe"),
+	ERRNO_MSG(EDOM, "Math argument out of domain of func"),
+	ERRNO_MSG(ERANGE, "Math result not representable"),
+	ERRNO_MSG(EDEADLK, "Resource deadlock would occur"),
+	ERRNO_MSG(ENAMETOOLONG, "File name too long"),
+	ERRNO_MSG(ENOLCK, "No record locks available"),
+	ERRNO_MSG(ENOSYS, "Function not implemented"),
+	ERRNO_MSG(ENOTEMPTY, "Directory not empty"),
+	ERRNO_MSG(ELOOP, "Too many symbolic links encountered"),
+	ERRNO_MSG(EWOULDBLOCK, SAME_AS(EAGAIN)),
+	ERRNO_MSG(ENOMSG, "No message of desired type"),
+	ERRNO_MSG(EIDRM, "Identifier removed"),
+	ERRNO_MSG(ECHRNG, "Channel number out of range"),
+	ERRNO_MSG(EL2NSYNC, "Level 2 not synchronized"),
+	ERRNO_MSG(EL3HLT, "Level 3 halted"),
+	ERRNO_MSG(EL3RST, "Level 3 reset"),
+	ERRNO_MSG(ELNRNG, "Link number out of range"),
+	ERRNO_MSG(EUNATCH, "Protocol driver not attached"),
+	ERRNO_MSG(ENOCSI, "No CSI structure available"),
+	ERRNO_MSG(EL2HLT, "Level 2 halted"),
+	ERRNO_MSG(EBADE, "Invalid exchange"),
+	ERRNO_MSG(EBADR, "Invalid request descriptor"),
+	ERRNO_MSG(EXFULL, "Exchange full"),
+	ERRNO_MSG(ENOANO, "No anode"),
+	ERRNO_MSG(EBADRQC, "Invalid request code"),
+	ERRNO_MSG(EBADSLT, "Invalid slot"),
+	ERRNO_MSG(EDEADLOCK, SAME_AS(EDEADLK)),
+	ERRNO_MSG(EBFONT, "Bad font file format"),
+	ERRNO_MSG(ENOSTR, "Device not a stream"),
+	ERRNO_MSG(ENODATA, "No data available"),
+	ERRNO_MSG(ETIME, "Timer expired"),
+	ERRNO_MSG(ENOSR, "Out of streams resources"),
+	ERRNO_MSG(ENONET, "Machine is not on the network"),
+	ERRNO_MSG(ENOPKG, "Package not installed"),
+	ERRNO_MSG(EREMOTE, "Object is remote"),
+	ERRNO_MSG(ENOLINK, "Link has been severed"),
+	ERRNO_MSG(EADV, "Advertise error"),
+	ERRNO_MSG(ESRMNT, "Srmount error"),
+	ERRNO_MSG(ECOMM, "Communication error on send"),
+	ERRNO_MSG(EPROTO, "Protocol error"),
+	ERRNO_MSG(EMULTIHOP, "Multihop attempted"),
+	ERRNO_MSG(EDOTDOT, "RFS specific error"),
+	ERRNO_MSG(EBADMSG, "Not a data message"),
+	ERRNO_MSG(EOVERFLOW, "Value too large for defined data type"),
+	ERRNO_MSG(ENOTUNIQ, "Name not unique on network"),
+	ERRNO_MSG(EBADFD, "File descriptor in bad state"),
+	ERRNO_MSG(EREMCHG, "Remote address changed"),
+	ERRNO_MSG(ELIBACC, "Can not access a needed shared library"),
+	ERRNO_MSG(ELIBBAD, "Accessing a corrupted shared library"),
+	ERRNO_MSG(ELIBSCN, ".lib section in a.out corrupted"),
+	ERRNO_MSG(ELIBMAX, "Attempting to link in too many shared libraries"),
+	ERRNO_MSG(ELIBEXEC, "Cannot exec a shared library directly"),
+	ERRNO_MSG(EILSEQ, "Illegal byte sequence"),
+	ERRNO_MSG(ERESTART, "Interrupted system call should be restarted"),
+	ERRNO_MSG(ESTRPIPE, "Streams pipe error"),
+	ERRNO_MSG(EUSERS, "Too many users"),
+	ERRNO_MSG(ENOTSOCK, "Socket operation on non-socket"),
+	ERRNO_MSG(EDESTADDRREQ, "Destination address required"),
+	ERRNO_MSG(EMSGSIZE, "Message too long"),
+	ERRNO_MSG(EPROTOTYPE, "Protocol wrong type for socket"),
+	ERRNO_MSG(ENOPROTOOPT, "Protocol not available"),
+	ERRNO_MSG(EPROTONOSUPPORT, "Protocol not supported"),
+	ERRNO_MSG(ESOCKTNOSUPPORT, "Socket type not supported"),
+	ERRNO_MSG(EOPNOTSUPP, "Operation not supported on transport endpoint"),
+	ERRNO_MSG(EPFNOSUPPORT, "Protocol family not supported"),
+	ERRNO_MSG(AFNOSUPPORT, "Address family not supported by protocol"),
+	ERRNO_MSG(EADDRINUSE, "Address already in use"),
+	ERRNO_MSG(EADDRNOTAVAIL, "Cannot assign requested address"),
+	ERRNO_MSG(ENETDOWN, "Network is down"),
+	ERRNO_MSG(ENETUNREACH, "Network is unreachable"),
+	ERRNO_MSG(ENETRESET, "Network dropped connection because of reset"),
+	ERRNO_MSG(ECONNABORTED, "Software caused connection abort"),
+	ERRNO_MSG(ECONNRESET, "Connection reset by peer"),
+	ERRNO_MSG(ENOBUFS, "No buffer space available"),
+	ERRNO_MSG(EISCONN, "Transport endpoint is already connected"),
+	ERRNO_MSG(ENOTCONN, "Transport endpoint is not connected"),
+	ERRNO_MSG(ESHUTDOWN, "Cannot send after transport endpoint shutdown"),
+	ERRNO_MSG(ETOOMANYREFS, "Too many references: cannot splice"),
+	ERRNO_MSG(ETIMEDOUT, "Connection timed out"),
+	ERRNO_MSG(ECONNREFUSED, "Connection refused"),
+	ERRNO_MSG(EHOSTDOWN, "Host is down"),
+	ERRNO_MSG(EHOSTUNREACH, "No route to host"),
+	ERRNO_MSG(EALREADY, "Operation already in progress"),
+	ERRNO_MSG(EINPROGRESS, "Operation now in progress"),
+	ERRNO_MSG(ESTALE, "Stale NFS file handle"),
+	ERRNO_MSG(EUCLEAN, "Structure needs cleaning"),
+	ERRNO_MSG(ENOTNAM, "Not a XENIX named type file"),
+	ERRNO_MSG(ENAVAIL, "No XENIX semaphores available"),
+	ERRNO_MSG(EISNAM, "Is a named type file"),
+	ERRNO_MSG(EREMOTEIO, "Remote I/O error"),
+	ERRNO_MSG(EDQUOT, "Quota exceeded"),
+	ERRNO_MSG(ENOMEDIUM, "No medium found"),
+	ERRNO_MSG(EMEDIUMTYPE, "Wrong medium type"),
+};
+
+const char *errno_str(int errno)
+{
+	if (errno >= 0)
+		return errno_message[0];
+
+	return errno_message[abs(errno)];
+}