Commit 206415d9 authored by alex@thinkpad's avatar alex@thinkpad
Browse files

QEMU: imported scnprintf/vscnprintf from Linux kernel and fixed incorrect usage in previous code

(adapted commit e8bb011bcb0b)

--HG--
branch : qemu
parent 880b848bb628
......@@ -3,3 +3,5 @@ obj-y += ../eos/eos.o ../eos/model_list.o ../eos/engine.o
obj-y += ../eos/eos_ml_helpers.o ../eos/mpu.o ../eos/serial_flash.o
obj-y += ../eos/dbi/logging.o ../eos/dbi/memcheck.o ../eos/dbi/debugmsg.o
obj-y += ../eos/dbi/backtrace.o
obj-y += ../eos/scnprintf.o
......@@ -30,7 +30,7 @@ void DebugMsg_log(EOSState * s)
char out[512];
int len = 0;
#define APPEND(fmt,...) do { len += snprintf(out + len, sizeof(out) - len, fmt, ## __VA_ARGS__); } while(0);
#define APPEND(fmt,...) do { len += scnprintf(out + len, sizeof(out) - len, fmt, ## __VA_ARGS__); } while(0);
char format_string[128]; // 128 bytes should be enough for anyone...
int arg_i = 0;
......
......@@ -6,6 +6,7 @@
#include "hw/sd/sd.h"
#include "hw/ide/internal.h"
#include "hw/char/digic-uart.h"
#include "scnprintf.h"
/** Helper macros **/
#define COUNT(x) ((int)(sizeof(x)/sizeof((x)[0])))
......
#include "qemu/osdep.h"
#include "scnprintf.h"
/* from https://github.com/torvalds/linux/blob/master/vsprintf.c
* Canon's vsnprintf returns the number of characters actually written,
* so we were using snprintf like this, even on desktop tools...
* see https://lwn.net/Articles/69419/ */
#undef vsnprintf
/**
* vscnprintf - Format a string and place it in a buffer
* @buf: The buffer to place the result into
* @size: The size of the buffer, including the trailing null space
* @fmt: The format string to use
* @args: Arguments for the format string
*
* The return value is the number of characters which have been written into
* the @buf not including the trailing '\0'. If @size is == 0 the function
* returns 0.
*
* If you're not already dealing with a va_list consider using scnprintf().
*
* See the vsnprintf() documentation for format string extensions over C99.
*/
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
unsigned int i;
i = vsnprintf(buf, size, fmt, args);
if (likely(i < size))
return i;
if (size != 0)
return size - 1;
return 0;
}
/**
* scnprintf - Format a string and place it in a buffer
* @buf: The buffer to place the result into
* @size: The size of the buffer, including the trailing null space
* @fmt: The format string to use
* @...: Arguments for the format string
*
* The return value is the number of characters written into @buf not including
* the trailing '\0'. If @size is == 0 the function returns 0.
*/
int scnprintf(char *buf, size_t size, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i = vscnprintf(buf, size, fmt, args);
va_end(args);
return i;
}
#ifndef __scnprintf_h__
#define __scnprintf_h__
extern int scnprintf( char* str, size_t n, const char* fmt, ... );
extern int vscnprintf( char* str, size_t n, const char* fmt, va_list ap );
/* since Canon's vsnprintf returns the number of chars actually written,
* we were using the return value of snprintf/vsnprintf
* as described in https://lwn.net/Articles/69419/
* disallow this usage; we really meant scnprintf/vscnprintf
* note: if we don't care about return value, they are the same as snprintf/vsnprintf
*/
#define snprintf (void)scnprintf
#define vsnprintf (void)vscnprintf
#endif /* __scnprintf_h__ */
......@@ -15,6 +15,7 @@
#define qemu_log_mask(level, fmt, ...) { qprintf(fmt, ## __VA_ARGS__); }
#define qemu_loglevel_mask(x) 0
#define EOSState void
#define scnprintf snprintf /* FIXME */
static char * eos_get_current_task_name(EOSState *s)
{
......@@ -764,14 +765,14 @@ void eos_backtrace_rebuild(EOSState *s, char * buf, int size)
if (buf)
{
int len = snprintf(buf, size,
int len = scnprintf(buf, size,
"%s stack: %x [%x-%x]\n",
eos_get_current_task_name(s),
sps[0], stack_top, stack_bot
);
while (--i >= 0)
{
len += snprintf(
len += scnprintf(
buf + len, size - len,
"%s @ %x:%x\n",
called_func(lrs[i] - 4), lrs[i] - 4, sps[i]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment