Heap dump fails with jemalloc mcllctl - heap-memory

I have tried to use the following to dump the mem-profile for a demo program:
export MALLOC_CONF="prof:true,prof_prefix:jeprof.out"
int main()
{
int i;
for (i = 0; i < 1000; i++) {
malloc(i * 100);
}
const char *fileName = "heap_info.out";
mallctl("prof.dump", NULL, NULL, &fileName, sizeof(const char *));
}
This fails with the following errors(no o/p:
: Invalid conf pair: prof:true
: Malformed conf string
Can some on tell me if I am doing something wrong?

Use --enable-prof switch when building jemalloc.

Related

How to easily parse an array from an INI file to an array in C?

In my code I use the iniparser (https://github.com/ndevilla/iniparser) to parse double, int and strings. However, I'm interested into parsing arrays, delimited with comma such as
arr = val1, val2, ..., valn
Any easy and quick way, like the parser above?
The best way is to make your own structure.
You can find easily on the web structures that you can import for your code. Another solution, not so great is to put your values as void pointers. And when you want to take your values back you take the void pointer and cast it with which kind of value you want( int,double,char ect). But this may conflict the values so you must be careful. You must know what kind value is in which cell of the pointer. It is not the ideal way but its a cheat way to avoid making your own structure.
You can use libconfini, which has array support.
test.conf:
arr = val1, val2, ..., valn
test.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <confini.h>
struct my_conf_T {
size_t arrlen;
char ** arr;
};
static int my_listnr (IniDispatch * this, void * v_conf) {
#define conf ((struct my_conf_T *) v_conf)
if (ini_string_match_si("arr", this->data, this->format)) {
conf->arrlen = ini_array_get_length(this->value, INI_COMMA, this->format);
if (!conf->arrlen || !this->v_len) {
/* Array is empty */
return 0;
}
conf->arr = (char **) malloc(conf->arrlen * sizeof(char *) + this->v_len + 1);
if (!conf->arr) {
fprintf(stderr, "malloc() failed\n");
exit(1);
}
char * remnant = (char *) ((char **) conf->arr + conf->arrlen);
memcpy(remnant, this->value, this->v_len + 1);
for (size_t idx = 0; idx < conf->arrlen; idx++) {
conf->arr[idx] = ini_array_release(&remnant, INI_COMMA, this->format);
ini_string_parse(conf->arr[idx], this->format);
}
}
return 0;
#undef conf
}
int main () {
struct my_conf_T my_conf = (struct my_conf_T) { 0 };
if (load_ini_path("test.conf", INI_DEFAULT_FORMAT, NULL, my_listnr, &my_conf)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
if (my_conf.arr) {
/* Do something with `my_conf.arr`... */
for (size_t idx = 0; idx < my_conf.arrlen; idx++) {
printf("arr[%zu] = %s\n", idx, my_conf.arr[idx]);
}
free(my_conf.arr);
}
return 0;
}
Output:
arr[0] = val1
arr[1] = val2
arr[2] = ...
arr[3] = valn
P.S. I happen to be the author.

Programmatically mock and test C function at execution

Yesterday I was asking myself a question.
Does it is possible to programmatically "brute force" all the calls to a specific function into a program, and test if the error cases of this call is always properly handled ?
Example:
int main(void)
{
char *mallocforfun = NULL;
char **matrix = NULL;
if ((matrix = (char **)malloc(sizeof(char*) * 42)))
{
for (int i = 0; i < 42; i++)
{
matrix[i] = (char *)malloc(sizeof(char) * 42);
bzero(matrix[i], 42);
}
matrix[i] = NULL;
}
mallocforfun = (char*)malloc(sizeof(char) * 42);
...
// do some stuff and free everything
return (0);
}
So in this example, if we would test malloc function, the tester will put three breakpoint:
int main(void)
{
char *mallocforfun = NULL;
char **matrix = NULL;
1st: if ((matrix = (char **)malloc(sizeof(char*) * 42)))
{
for (int i = 0; i < 42; i++)
{
2nd: matrix[i] = (char *)malloc(sizeof(char) * 42);
bzero(matrix[i], 42);
}
matrix[i] = NULL;
}
3rd: mallocforfun = (char*)malloc(sizeof(char) * 42);
...
// do some stuff and free everything
return (0);
}
Run the program, change malloc function return into an error value, see if it crash, delete last tested breakpoint, rerun, and so on.
I want to verify that I have handled all error returns by running the program repeatedly in an environment where malloc fails once at each call site in turn on subsequent runs of the program. (Thank's to #Mic for the explicit formulation)
I think it's possible to do something like this with non-stripped binary with gdb script. I searched by myself but can't find anything looking like that.
So I feel like I have a "keyword" missing. Do you know if this kind of test have a specific name, or a tool doing something like that ?

Segmentation fault happened in Linux but works in Mac

I have a c code which wrote in my Mac laptop in Xcode but it didn't work in Linux system.
I run this code by two ways:
1.One is run in Eclipse but the while loop didn't look like finish. Please find the message below:
Please wait while calculating...
But not more message in the console. It looks like while loop can't finish by some reason.
2.The second way is that I complier the code directly in Linux environment by the command:
cc -std=c99 main.c -o main
Then run by the command:
./main
The message shows that:
Please wait while calculating... Segmentation fault (core dumped)
I checked by gdb
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a9bd4a in ?? () from /lib/x86_64-linux-gnu/libc.so.6
My data is saved in:
/home/alan_yu/workspace/scandi.csv
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char **split(char *line, char sep, int fields) {
char **r = (char **)malloc(fields * sizeof(char*));
int lptr = 0, fptr = 0;
r[fptr++] = line;
while (line[lptr]) {
if (line[lptr] == sep) {
line[lptr] = '\0';
r[fptr] = &(line[lptr+1]);
fptr++;
}
lptr++;
}
return r;
}
int cmpfunc (const void * a, const void * b)
{
return *(double *)a > *(double *)b ? 1 : -1;
}
#define LINE_SIZE 1000000
#define EXPECTED_STOCK_SIZE 10000000
void calculate2(char * fileName) {
printf("Please wait while calculating...\n");
// Open the file for reading.
FILE *file = fopen(fileName, "r");
// maximun size of the line to read.
// memory allocation for the line to read.
char* line = malloc(LINE_SIZE);
// char **stockNameArray = malloc( sizeof(char *) * EXPECTED_STOCK_SIZE);
// int stockNameArrayPos = 0;
double *bidArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE );
int bidArrayPos = 0;
double *askArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE);
int askArrayPos = 0;
double *spreadArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE);
int spreadArrayPos = 0;
double sum=0;
int i=0,j=0;
while (fgets(line, LINE_SIZE, file)!= NULL){
// printf("Please wait while ...%d\n ", j);
j++;
char **fields = split(line, ',', 15);
const char * volvbEquity = "VOLVB SS Equity";
int comp = strcmp(fields[0], volvbEquity);
if (comp == 0) {
double bidValue = atof(fields[2]);
double askValue = atof(fields[3]);
bidArray[bidArrayPos++] = bidValue;
askArray[askArrayPos++] = askValue;
if (askValue - bidValue > 0) {
double spreadValue = ((askValue - bidValue) / (askValue + bidValue) * 20000);
spreadArray[spreadArrayPos++] = spreadValue;
sum = sum + spreadValue;
}
}
}
//quick sort the spread
qsort(spreadArray, spreadArrayPos, sizeof(double), cmpfunc);
int mediumPos;
double mean;
double medium;
if(spreadArrayPos % 2 == 0) {
mediumPos = spreadArrayPos / 2;
medium = (spreadArray[mediumPos] + spreadArray[mediumPos+1]) / 2;
} else {
mediumPos = (spreadArrayPos)/2 + 1;
medium = spreadArray[mediumPos];
}
mean = sum / spreadArrayPos;
printf("Please find mean and medium %f %f\n", mean, medium);
free(bidArray);
free(askArray);
free(spreadArray);
}
int main(int argc, char **argv) {
calculate2("/home/alan_yu/workspace/scandi.csv");
return(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void split(char *line, char sep, char **fields) {
int lptr = 0, fptr = 0;
fields[fptr++] = line;
while (line[lptr]) {
if (line[lptr] == sep) {
line[lptr] = '\0';
fields[fptr] = &(line[lptr+1]);
fptr++;
}
lptr++;
}
}
int cmpfunc (const void * a, const void * b) {
return *(double *)a > *(double *)b ? 1 : -1;
}
#define LINE_SIZE 1000000
#define EXPECTED_STOCK_SIZE 10000000
#define COLUMN_NUM 15
void calculate2(char * fileName) {
printf("Please wait while calculating...\n");
// Open the file for reading.
FILE *file = fopen(fileName, "r");
// maximun size of the line to read.
// memory allocation for the line to read.
char* line = malloc(LINE_SIZE);
// char **stockNameArray = malloc( sizeof(char *) * EXPECTED_STOCK_SIZE);
// int stockNameArrayPos = 0;
double *bidArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE );
int bidArrayPos = 0;
double *askArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE);
int askArrayPos = 0;
double *spreadArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE);
int spreadArrayPos = 0;
double sum=0;
int i=0,j=0;
while (fgets(line, LINE_SIZE, file)!= NULL){
// printf("Please wait while ...%d\n ", j);
j++;
char **fields = malloc(sizeof(char *) * COLUMN_NUM);
split(line, ',', fields);
const char * volvbEquity = "VOLVB SS Equity";
int comp = strcmp(fields[0], volvbEquity);
if (comp == 0) {
double bidValue = atof(fields[2]);
double askValue = atof(fields[3]);
bidArray[bidArrayPos++] = bidValue;
askArray[askArrayPos++] = askValue;
if (askValue - bidValue > 0) {
double spreadValue = ((askValue - bidValue) / (askValue + bidValue) * 20000);
spreadArray[spreadArrayPos++] = spreadValue;
sum = sum + spreadValue;
}
}
// free memory for fields.
free(fields);
}
// free memory for the line variable.
free(line);
//quick sort the spread
qsort(spreadArray, spreadArrayPos, sizeof(double), cmpfunc);
int mediumPos;
double mean;
double medium;
if(spreadArrayPos % 2 == 0) {
mediumPos = spreadArrayPos / 2;
medium = (spreadArray[mediumPos] + spreadArray[mediumPos+1]) / 2;
} else {
mediumPos = (spreadArrayPos)/2 + 1;
medium = spreadArray[mediumPos];
}
mean = sum / spreadArrayPos;
printf("Please find mean and medium %f %f\n", mean, medium);
free(bidArray);
free(askArray);
free(spreadArray);
}
int main(int argc, char **argv) {
calculate2("/home/alan_yu/workspace/scandi.csv");
return(0);
}
On Linux, always run the program in valgrind if it is crashing.
It will not only tell you exactly what is wrong in your code, but also specify what code lines are responsible for the error.
You need to check the return value of fopen! I would suggest doing the same thing for malloc, but it's much less likely that malloc is failing due to a missing file or typographical error particularly if you're allocating large chunks! You wouldn't want to dereference a null pointer, right?
I'm assuming each line has at least four comma-separated fields, because you're using fields[3]. You should probably work out how to guard against using uninitialised values here. I'd start by re-engineering split so that you have some terminal NULL value or something in its output (and while we're on that topic, don't forget to free the return value).
Is it possible that you might be dividing by zero? That'd be something else you need to guard against.
Shouldn't cmpfunc return 0 when items are equal? I've seen implementations crash when return values for comparison functions for qsort and bsearch are inconsistent.
You claimed below in a comment that your lines have fifteen commas. This implies that you have sixteen fields (count them below), since the number of fields is n+1 where n is the number of separators.
field1, field2, field3, field4,
field4, field6, field7, field8,
field9, field10,field11,field12,
field13,field14,field15,field16
There are fifteen commas and sixteen fields in this table. You're only allocating enough for fifteen fields, however. This is a buffer overflow, more typical undefined behaviour.
Finally, I find out the problem comes from the
while (fgets(line, LINE_SIZE, file)!= NULL){
char **fields = split(line, ',', 15);
I have changed it to
char **fields = malloc(sizeof(char *) * 15 * 10000);
while (fgets(line, LINE_SIZE, file)!= NULL){
I haven't try to allocate a large memory to **fields after the while loop due to it takes too much memory to my machine.
It looks like under gcc compile that if I do:
while (fgets(line, LINE_SIZE, file)!= NULL){
char **fields = split(line, ',', 15);
It won't overwrite the **fields from last time. But it works in Mac
Not sure is that correct?
In the end, thanks for all of you guys help for my problem.

How to create an array consisting of the tokens created using the strtok function?

I am new with .ini files and thus this qn(which might seem silly) .I have created a .ini file and access it via my C program. The ini file looks like this:
[key]
title = A,H,D
The C program accesses it using:
LPCSTR ini ="C:\\conf.ini;
char var[100];
GetPrivateProfileString("key", "title", 0, var, 100, ini);
printf("%s", var);
char* buffer = strtok(var, ", ");
do{
printf("%s", buffer);
if (strcmp(buffer, "A")==0)
printf("Hello");
puts("");
}while ((buffer=strtok(NULL, ", "))!= NULL);
output looks as :
A H D F G IAHello
H
D
F
G
Now what I need to do is use these individual tokens again to form an array with indices within my C program. For example:
char x[A, H, D, F, G]
so that when I refer to the index 2, x[2] should give me 'D'. Could somebody suggest a way to do this. I have never used strtok before and thus very confused. Thank you in advance.
This question is quite similar to others regarding getting external information and storing it in an array.
The problem here is the amount of elements in your array to store.
You could use Link-lists, but for this example, I would scan the file, getting the total amount of items needed for the array - and then parse the file data again - storing the items in the array.
The first loop, goes through and counts the items to be store, as per your example posted. I will do the second loop just as an example - please note in my example you would of created nTotalItems and have counted the amount of items, storing that in nTotalItems ... I am assuming you want to store a string, not just a char...
Also please note this a draft example, done at work - only to show a method of storing the tokens into an array, therefore there is no error checking ec
// nTotalItems has already been calculated via the first loop...
char** strArray = malloc( nTotalItems * sizeof( char* ));
int nIndex = 0;
// re-setup buffer
buffer = strtok(var, ", ");
do {
// allocate the buffer for string and copy...
strArray[ nIndex ] = malloc( strlen( buffer ) + 1 );
strcpy( strArray[ nIndex ], buffer );
printf( "Array %d = '%s'\n", nIndex, strArray[ nIndex ] );
nIndex++;
} while ((buffer=strtok(NULL, ", "))!= NULL);
Just use an INI parser that supports arrays.
INI file:
[my_section]
title = A,H,D
C program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <confini.h>
#define MY_ARRAY_DELIMITER ','
struct configuration {
char ** title;
size_t title_length;
};
static char ** make_strarray (size_t * arrlen, const char * src, const size_t buffsize, IniFormat ini_format) {
*arrlen = ini_array_get_length(src, MY_ARRAY_DELIMITER, ini_format);
char ** const dest = *arrlen ? (char **) malloc(*arrlen * sizeof(char *) + buffsize) : NULL;
if (!dest) { return NULL; }
memcpy(dest + *arrlen, src, buffsize);
char * iter = (char *) (dest + *arrlen);
for (size_t idx = 0; idx < *arrlen; idx++) {
dest[idx] = ini_array_release(&iter, MY_ARRAY_DELIMITER, ini_format);
ini_string_parse(dest[idx], ini_format);
}
return dest;
}
static int ini_handler (IniDispatch * this, void * v_conf) {
struct configuration * conf = (struct configuration *) v_conf;
if (this->type == INI_KEY && ini_string_match_si("my_section", this->append_to, this->format)) {
if (ini_string_match_si("title", this->data, this->format)) {
/* Save memory (not strictly needed) */
this->v_len = ini_array_collapse(this->value, MY_ARRAY_DELIMITER, this->format);
/* Allocate a new array of strings */
if (conf->title) { free(conf->title); }
conf->title = make_strarray(&conf->title_length, this->value, this->v_len + 1, this->format);
if (!conf->title) { return 1; }
}
}
return 0;
}
static int conf_init (IniStatistics * statistics, void * v_conf) {
*((struct configuration *) v_conf) = (struct configuration) { NULL, 0 };
return 0;
}
int main () {
struct configuration my_conf;
/* Parse the INI file */
if (load_ini_path("C:\\conf.ini", INI_DEFAULT_FORMAT, conf_init, ini_handler, &my_conf)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
/* Print the parsed data */
for (size_t idx = 0; idx < my_conf.title_length; idx++) {
printf("my_conf.title[%d] = %s\n", idx, my_conf.title[idx]);
}
/* Free the parsed data */
if (my_conf.title_length) {
free(my_conf.title);
}
return 0;
}
Output:
my_conf.title[0] = A
my_conf.title[1] = H
my_conf.title[2] = D

C: Returning String From Another Function

I'm new to C / pointers / memory management and am having trouble implementing a few functions for a project I'm working on.
In my builtins.c file, I have a function called printalias that is called to print all the alias names and corresponding values stored in my program. At the end, I want to print one of the alias names by retrieving it via another function called getal.
int x_printalias(int nargs, char *args[]) {
int i = 0;
// Loop through, print names and values
for(i = 0; i< 100; i++)
{
if(alias_names[i][0]!='\0' && !alias_disabled[i])
{
char * var = alias_names[i];
char * val = alias_vals[i];
fprintf(stderr,"%s = %s\n", var, val );
}
}
// This is where I want to retrieve the string from another function
char * hello = "brett";
hello = getal(hello);
fprintf(stderr,"Got alias for brett --> %s",hello);
return 0;
}
My getal function exists in my shellParser.c file and looks like this, generally performing the same looping and returning when it is found:
const char * getal(int nargs, char *args[])
{
fprintf(stderr,"\nRetrieving alias...\n");
int i = 0;
fprintf(stderr, "check1\n" );
fprintf(stderr,"Got args[0]: %s\n", args[0]);
while (alias_names[i][0]!='\0' && i < MAX_ALIAS_LENGTH ) // Find empty slot in variables array
{
fprintf(stderr, "check2\n" );
fprintf(stderr,"I is currently %i and current varible in slot is %s\n",i,alias_names[i]);
//strncpy(hello, variables[i], MAX_VAR_LENGTH); // Variable at current slot
if(strcmp(alias_names[i], args[0]) == 0) // If we have an entry, need to overwrite it
{
fprintf(stderr,"Found alias %s = %s at spot %i\n",args[0],alias_vals[i], i); // Not at end if here
return alias_vals[i];
}
i++;
}
fprintf(stderr, "check3\n" );
// Elided....
return '\0';
}
In the end of my printalias function, I want to test that this getal function is working by calling it on a hardcoded string "brett". However, when I call my printalias function from the command line, it makes it to the "Check 1" print statement and then simply quits without error or return value.
I think this has something to do with my memory management or incorrect declaration of variables with pointers. Can anybody spot something (or a lot of things) that I'm doing wrong here?
You must declarete list of argument for to call getal and it call with these
list.
And pointer of return values getal must be const char*
//....
// This is where I want to retrieve the string from another function
char * hello[] = {"brett"}; // this list argument for getal function
const char *strGetal;
strGetal = getal(1,hello);
fprintf(stderr,"Got alias for brett --> %s",strGetal);
return 0;
}
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** get_all(int argc, char **argv)
{
char *value;
char **values = NULL;
int i;
values = (char**) malloc(sizeof (char) * argc);
if (values == NULL) {
perror("malloc");
return NULL;
}
for (i = 0; i < argc; i++, argv++) {
value = strchr(*argv, ':');
values[i] = (value + 1);
}
return values;
}
int main()
{
char *args[] = {"key:a", "key:b", "key:c"};
char **values;
int i;
values = get_all(3, args);
for (i = 0; i < 3; i++) {
puts(values[i]);
}
return 0;
}

Resources