Skip to content
39 changes: 27 additions & 12 deletions smex/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
static int elf_read_sections(struct elf_module *module, bool verbose)
{
Elf32_Ehdr *hdr = &module->hdr;
Elf32_Shdr *section = module->section;
Elf32_Shdr *section;
size_t count;
int i, ret;
uint32_t valid = (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR);
Expand Down Expand Up @@ -128,7 +128,7 @@ static int elf_read_sections(struct elf_module *module, bool verbose)
static int elf_read_programs(struct elf_module *module, bool verbose)
{
Elf32_Ehdr *hdr = &module->hdr;
Elf32_Phdr *prg = module->prg;
Elf32_Phdr *prg;
size_t count;
int i, ret;

Expand Down Expand Up @@ -408,10 +408,17 @@ int elf_find_section(const struct elf_module *module, const char *name)
ret = -errno;
goto out;
}
buffer[section->size - 1] = '\0';

/* find section with name */
for (i = 0; i < hdr->shnum; i++) {
s = &module->section[i];
if (s->name >= section->size) {
fprintf(stderr, "error: invalid section name string index %d\n", s->name);
ret = -EINVAL;
goto out;
}

if (!strcmp(name, buffer + s->name)) {
ret = i;
goto out;
Expand All @@ -431,8 +438,8 @@ int elf_read_section(const struct elf_module *module, const char *section_name,
const Elf32_Shdr **dst_section, void **dst_buff)
{
const Elf32_Shdr *section;
int section_index = -1;
int read;
int section_index;
int ret;

section_index = elf_find_section(module, section_name);
if (section_index < 0) {
Expand All @@ -451,17 +458,25 @@ int elf_read_section(const struct elf_module *module, const char *section_name,
return -ENOMEM;

/* fill buffer with section content */
fseek(module->fd, section->off, SEEK_SET);
read = fread(*dst_buff, 1, section->size, module->fd);
if (read != section->size) {
fprintf(stderr,
"error: can't read %s section %d\n", section_name,
-errno);
free(*dst_buff);
return -errno;
ret = fseek(module->fd, section->off, SEEK_SET);
if (ret) {
fprintf(stderr, "error: can't seek to %s section %d\n", section_name, -errno);
ret = -errno;
goto error;
}

ret = fread(*dst_buff, 1, section->size, module->fd);
if (ret != section->size) {
fprintf(stderr, "error: can't read %s section %d\n", section_name, -errno);
ret = -errno;
goto error;
}

return section->size;

error:
free(*dst_buff);
return ret;
}

int elf_read_module(struct elf_module *module, const char *name, bool verbose)
Expand Down
49 changes: 21 additions & 28 deletions tools/logger/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ struct proc_ldc_entry {
uintptr_t params[TRACE_MAX_PARAMS_COUNT];
};

static const char *BAD_PTR_STR = "<bad uid ptr 0x%.8x>";

#define BAD_PTR_STR "<bad uid ptr 0x%.8x>"
#define UUID_LOWER "%s%s%s<%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x>%s%s%s"
#define UUID_UPPER "%s%s%s<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>%s%s%s"

Expand Down Expand Up @@ -313,7 +312,7 @@ static unsigned int timestamp_width(unsigned int precision)
* gcc 9.3, this avoids a very long precision causing snprintf()
* to truncate time_fmt
*/
assert(precision >= 0 && precision < 20);
assert(precision < 20);
/*
* 12 digits for units is enough for 1M seconds = 11 days which
* should be enough for most test runs.
Expand All @@ -327,7 +326,6 @@ static inline void print_table_header(void)
{
FILE *out_fd = global_config->out_fd;
int hide_location = global_config->hide_location;
char time_fmt[32];

char date_string[64];
const time_t epoc_secs = time(NULL);
Expand All @@ -337,16 +335,14 @@ static inline void print_table_header(void)

if (gettime_ret) {
log_err("clock_gettime() failed: %s\n",
strerror(gettime_ret));
strerror(errno));
exit(1);
}

if (global_config->time_precision >= 0) {
const unsigned int ts_width =
timestamp_width(global_config->time_precision);
snprintf(time_fmt, sizeof(time_fmt), "%%-%ds(us)%%%ds ",
ts_width, ts_width);
fprintf(out_fd, time_fmt, " TIMESTAMP", "DELTA");
const unsigned int ts_width = timestamp_width(global_config->time_precision);

fprintf(out_fd, "%*s(us)%*s ", -ts_width, " TIMESTAMP", ts_width, "DELTA");
}

fprintf(out_fd, "%2s %-18s ", "C#", "COMPONENT");
Expand Down Expand Up @@ -461,7 +457,6 @@ static void print_entry_params(const struct log_entry_header *dma_log,
char ids[TRACE_MAX_IDS_STR];
float dt = to_usecs(dma_log->timestamp - last_timestamp);
struct proc_ldc_entry proc_entry;
static char time_fmt[64];
int ret;

if (raw_output)
Expand Down Expand Up @@ -502,23 +497,20 @@ static void print_entry_params(const struct log_entry_header *dma_log,
ids[0] = '\0';

if (raw_output) { /* "raw" means script-friendly (not all hex) */
const char *entry_fmt = "%s%u %u %s%s%s ";

if (time_precision >= 0)
snprintf(time_fmt, sizeof(time_fmt), "%%.%df %%.%df ",
time_precision, time_precision);

fprintf(out_fd, entry_fmt,
fprintf(out_fd, "%s%u %u %s%s%s ",
entry->header.level == use_colors ?
(LOG_LEVEL_CRITICAL ? KRED : KNRM) : "",
dma_log->core_id,
entry->header.level,
get_component_name(entry->header.component_class, dma_log->uid),
raw_output && strlen(ids) ? "-" : "",
ids);

if (time_precision >= 0)
fprintf(out_fd, time_fmt,
to_usecs(dma_log->timestamp - timestamp_origin), dt);
fprintf(out_fd, "%.*f %.*f ",
time_precision, to_usecs(dma_log->timestamp - timestamp_origin),
time_precision, dt);

if (!hide_location)
fprintf(out_fd, "(%s:%u) ",
format_file_name(entry->file_name, raw_output),
Expand All @@ -527,13 +519,11 @@ static void print_entry_params(const struct log_entry_header *dma_log,
if (time_precision >= 0) {
const unsigned int ts_width = timestamp_width(time_precision);

snprintf(time_fmt, sizeof(time_fmt),
"%%s[%%%d.%df] (%%%d.%df)%%s ",
ts_width, time_precision, ts_width, time_precision);

fprintf(out_fd, time_fmt,
fprintf(out_fd, "%s[%*.*f] (%*.*f)%s ",
use_colors ? KGRN : "",
to_usecs(dma_log->timestamp - timestamp_origin), dt,
ts_width, time_precision,
to_usecs(dma_log->timestamp - timestamp_origin),
ts_width, time_precision, dt,
use_colors ? KNRM : "");
}

Expand Down Expand Up @@ -625,7 +615,7 @@ static int read_entry_from_ldc_file(struct ldc_entry *entry, uint32_t log_entry_
ret = -EINVAL;
goto out;
}
entry->file_name = (char *)malloc(entry->header.file_name_len);
entry->file_name = (char *)malloc(entry->header.file_name_len + 1);

if (!entry->file_name) {
log_err("can't allocate %d byte for entry.file_name\n",
Expand All @@ -636,6 +626,8 @@ static int read_entry_from_ldc_file(struct ldc_entry *entry, uint32_t log_entry_

ret = fread(entry->file_name, sizeof(char), entry->header.file_name_len,
global_config->ldc_fd);
entry->file_name[entry->header.file_name_len] = '\0';

if (ret != entry->header.file_name_len) {
log_err("Failed to read source filename for offset 0x%x in dictionary.\n",
entry_offset);
Expand All @@ -649,7 +641,7 @@ static int read_entry_from_ldc_file(struct ldc_entry *entry, uint32_t log_entry_
ret = -EINVAL;
goto out;
}
entry->text = (char *)malloc(entry->header.text_len);
entry->text = (char *)malloc(entry->header.text_len + 1);
if (!entry->text) {
log_err("can't allocate %d byte for entry.text\n", entry->header.text_len);
ret = -ENOMEM;
Expand All @@ -662,6 +654,7 @@ static int read_entry_from_ldc_file(struct ldc_entry *entry, uint32_t log_entry_
ret = -1;
goto out;
}
entry->text[entry->header.text_len] = '\0';

return 0;

Expand Down
4 changes: 3 additions & 1 deletion tools/topology/topology1/m4/buffer.m4
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ dnl COMP_BUFFER_SIZE( num_periods, sample_size, channels, fmames)
define(`COMP_BUFFER_SIZE', `eval(`$1 * $2 * $3 * $4')')

dnl COMP_PERIOD_FRAMES( sample_rate, period_us)
define(`COMP_PERIOD_FRAMES', `eval(`($1 * $2) / 1000000')')
dnl note: m4 eval arithmetic is 32bit signed, so split the 10^6
dnl division to avoid overflow.
define(`COMP_PERIOD_FRAMES', `eval(`$1 / 100 * $2 / 10000')')

divert(0)dnl