I used the code below. The IAR code screen has a vertical line after the 80th. In the code below, there is a vertical line before the "src" variable.
__STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
{
uint32_t i;
for (i = 0U; i < len; ++i)
{
dst[i] = src[i];
}
}
Then, error this below
Error[Pe018]: expected a ")"
Error[Pe020]: identifier "len" is undefined
Error[Pe020]: identifier "src" is undefined
But if I change the location of the "len" variable setting as below, The "len" variable comes to the left of the vertical line
__STATIC_INLINE void orderedCpy(volatile uint32_t* dst, uint32_t len, const uint32_t* __RESTRICT src)
{
uint32_t i;
for (i = 0U; i < len; ++i)
{
dst[i] = src[i];
}
}
Then, error is changed as this below. "len" has been defined.
Error[Pe018]: expected a ")"
Error[Pe020]: identifier "src" is undefined
Why IAR can't read after 80th character in this condition?
I found the answer! IAR v7 can't take "_RESTRICT". so I'v change 'cmsis_iccarm.h'
like as below
#ifndef __RESTRICT
#if __ICCARM_V8
#define __RESTRICT __restrict
#else
#define __RESTRICT
#endif
#endif
Related
I'd like to dump the VDSO to disk in a way that I can verify it is correct with objdump -D.
We can get the base address of the VDSO with getauxval(AT_SYSINFO_EHDR) as documented in vdso(7). But how does one get the size of the object?
I happen to know it is exactly two pages long, but I'm not certain I can rely on that.
I can't see anything in the ELF header that would indicate the size of the object as a whole, and I've also tried iterating and dumping the sections via dl_iterate_phdr(3) to no joy (presumably this would skip the ELF header?).
Any ideas? Do I really have to scrape the size out of the proc maps?
The VDSO header gives you the start of the file. To find the end, calculate the maximum of:
the end of all segments from the program header table (offset + size)
the end of the section header table
the end of the program header table
Then write everything between the start and end to disk. The following program should do the trick:
#include <stdlib.h>
#include <stdio.h>
#include <sys/auxv.h>
struct elf_fhdr_64
{
uint32_t magic;
uint8_t ei_class;
uint8_t ei_data;
uint8_t ei_version;
uint8_t ei_osabi;
uint8_t ei_abiversion;
uint8_t pad[7];
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint64_t e_entry;
uint64_t e_phoff; // program header offset
uint64_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum; // number of program headers
uint16_t e_shentsize;
uint16_t e_shnum;
// ...
};
struct elf_phdr_64
{
uint32_t p_type;
uint32_t p_flags;
uint64_t p_offset; // offset in file
uint64_t p_vaddr;
uint64_t p_paddr;
uint64_t p_filesz; // size in file
// ...
};
struct elf_shdr_64
{
uint32_t sh_name;
uint32_t sh_type;
uint64_t sh_flags;
uint64_t sh_addr; // virtual addr when loaded
uint64_t sh_offset; // offset in file
uint64_t sh_size; // size in file
// ...
};
int main(int argc, char **argv)
{
unsigned long vdso_hdr = getauxval(AT_SYSINFO_EHDR);
uint32_t elf_magic = * (uint32_t *)vdso_hdr;
if (elf_magic == 0x464C457F) {
printf("has elf magic, proceeding...\n");
}
else {
printf("no elf magic.\n");
exit(1);
}
struct elf_fhdr_64 * fhdrp = (struct elf_fhdr_64 *) vdso_hdr;
int num_phdrs = fhdrp->e_phnum;
uint16_t phentsize = fhdrp->e_phentsize;
long max_offs = 0;
for (int i = 0; i < num_phdrs; i++) {
struct elf_phdr_64 * phdr = (struct elf_phdr_64 *)(vdso_hdr
+ fhdrp->e_phoff + i * phentsize);
long file_offs = phdr->p_offset + phdr->p_filesz;
if (max_offs < file_offs) max_offs = file_offs;
}
int num_shdrs = fhdrp->e_shnum;
int shentsize = fhdrp->e_shentsize;
for (int i = 0; i < num_shdrs; i++) {
struct elf_shdr_64 * shdr = (struct elf_shdr_64 *)(vdso_hdr
+ fhdrp->e_shoff + i * shentsize);
long file_offs = shdr->sh_offset + shdr->sh_size;
if (max_offs < file_offs) max_offs = file_offs;
}
// section table:
int section_table_max = fhdrp->e_shoff + (num_shdrs * shentsize);
if (max_offs < section_table_max) max_offs = section_table_max;
// phdrs table:
int phdr_table_max = fhdrp->e_phoff + (num_phdrs * phentsize);
if (max_offs < phdr_table_max) max_offs = section_table_max;
FILE * outfile = fopen("test-vdso.so", "wb");
if (outfile) {
fwrite((void *) vdso_hdr, 1, max_offs, outfile);
fclose(outfile);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct string_count_struct{
char fname;
char str;
long long count;
};
void* filesearch(void* arg)
{
//get the file name
struct string_count_struct *arg_ptr = (struct string_count_struct*) arg;
int line_num = 1;
int find_result = 0;
char temp[512];
//create a file pointer
FILE *fp;
fp = fopen(arg_ptr -> fname, "r");
//dont forget error handling
if (fp == NULL){
printf("File could not be opened");
return(-1);
}
while (fgets(temp, 512, fp) != NULL) {
if ((strstr(temp, arg_ptr -> str)) != NULL) {
find_result++;
}
line_num++;
}
if(find_result = 0) {
printf("\nSorry, couldn't find a match.\n");
}
arg_ptr -> count = find_result;
//close the file
if (fp){
fclose(fp);
}
pthread_exit(0);
}
int main (int argc, char **argv)
{
if (argc < 3) {
printf("Usage: <file> <string> <arg1> <arg2>...<argN>\n", argv[0]);
exit(-1);
}
int num_args = argc - 2;
struct string_count_struct args[num_args];
//Thread Creation:
pthread_t tids[num_args];
for(int i = 0; i < num_args; i++) {
args[i].fname = atoll(argv[i + 2]);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tids[i], &attr, filesearch, &args[i]);
}
//Wait until work is completed
for (int i = 0; i < num_args; i ++){
pthread_join(tids[i], NULL);
printf("blah is blah %lld\n", args[i].count);
}
return 0;
}
Here are my warnings
root#kali:~/Desktop# gcc prog2.c -lbthread
prog2.c: In function ‘filesearch’:
prog2.c:29:13: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
fp = fopen(arg_ptr -> fname, "r");
^~~~~~~
In file included from prog2.c:1:0:
/usr/include/stdio.h:274:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern FILE *fopen (const char *__restrict __filename,
^~~~~
prog2.c:34:8: warning: return makes pointer from integer without a cast [-Wint-conversion]
return(-1);
^
prog2.c:38:21: warning: passing argument 2 of ‘strstr’ makes pointer from integer without a cast [-Wint-conversion]
if ((strstr(temp, arg_ptr -> str)) != NULL) {
^~~~~~~
In file included from prog2.c:4:0:
/usr/include/string.h:337:14: note: expected ‘const char *’ but argument is of type ‘char’
extern char *strstr (const char *__haystack, const char *__needle)
^~~~~~
prog2.c: In function ‘main’:
prog2.c:78:17: error: assignment of read-only member ‘fname’
args[i].fname = atoll(argv[i + 2]);
I am unsure of what I am doing wrong, these errors are preventing my program from correctly reading through the desired files and calculating the # of occurrences of a particular string that the user will select. I have fixed my error but not the warnings.
The program will take a command line argument, create a separate thread for each file to be searched through, search through each file, and then give the results. I plan on using a Mutex for further refinement, but right now I am just trying to solve my I/O issues.
Just addressing some of the warnings, I'm not at all sure if this will make the code work:
In line 29, fopen expects a filename (char *) but fname is just a char in string_count_struct. Make it a char*. In the main function you convert one of the arguments from ASCII to long long and assign it to fname, which shall later be used as a file name for fopen(). That's probably not what you want to do.
In line 34, you return -1 which is not a pointer. You declared the function to be returning a void pointer. Make it return(0) (or return(NULL)).
Same as in line 29 happens in line 38: str in the struct string_count_struct is just a char, but strstr expects a char*. Make it a char*, too.
Your "Usage" is missing a "%s" format string to actually print the argument argv[0].
main.cpp
#include<iostream>
#include "cuda.h"
using namespace std;
void cuda_calculation();
int main()
{
cuda_calculation();
return 0;
}
cu.h
void call(int , int ,float* , int );
cuda.cpp
#include <stdio.h>
#include <cuda.h>
#include "cu.h"
void cuda_calculation()
{
float *a_h, *a_d; // Pointer to host & device arrays
const int N = 10; // Number of elements in arrays
size_t size = N * sizeof(float);
a_h = (float *)malloc(size); // Allocate array on host
cudaMalloc((void **) &a_d, size); // Allocate array on device
// Initialize host array and copy it to CUDA device
for (int i=0; i<N; i++) a_h[i] = (float)i;
cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
// Do calculation on device:
int block_size = 4;
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
void call(n_blocks, block_size,&a_d, N);
/*square_array <<< n_blocks, block_size >>> (a_d, N);*/
// Retrieve result from device and store it in host array
cudaMemcpy(a_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
// Print results
for (int i=0; i<N; i++) printf("%d %f\n", i, a_h[i]);
// Cleanup
free(a_h); cudaFree(a_d);
}
cu.cu
#include <stdio.h>
#include "cu.h"
#include <cuda.h>
// Kernel that executes on the CUDA device
__global__ void square_array(float *a, int N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx<N) a[idx] = a[idx] * a[idx];
}
//}
void call(int a,int b,float* c,int d)
{
square_array <<< 3,4 >>> (c,d);
}
I tried to seperate the kernal code and host code in a cpp file, however the following error prevails:
Error 'cudaMemcpy': identifier not found and the other cuda related identifier is not identified.
how to use the cuda related identifier in cpp file and call the kernal functions
There are some errors: void cuda_calculation(); needs to be visible to main.cpp through a header file (cu.h).
Also make sure to compile your .cu files with nvcc and NOT as a standard C++ file. Use CUDA compilation rules to make this process easy (installed by default as part of CUDA toolkit)
after a long trial ,I came with the proper output,
to include the cuda identifier in the cpp files we not only need to include cuda.h but also we need to include cuda_runtime.h as
cuda.cpp as
#include <stdio.h>
#include <cuda.h>
#include<cuda_runtime.h>
#include "cu.h"
#include "cud.h"
//void call(int , int ,float * , int );
void cuda_calculation()
{
float *a_h, *a_d; // Pointer to host & device arrays
const int N = 10; // Number of elements in arrays
size_t size = N * sizeof(float);
a_h = (float *)malloc(size); // Allocate array on host
cudaMalloc((void **) &a_d, size); // Allocate array on device
// Initialize host array and copy it to CUDA device
for (int i=0; i<N; i++) a_h[i] = (float)i;
cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
// Do calculation on device:
int block_size = 4;
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
call(n_blocks, block_size,a_d, N);
/*square_array <<< n_blocks, block_size >>> (a_d, N);*/
// Retrieve result from device and store it in host array
cudaMemcpy(a_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
// Print results
for (int i=0; i<N; i++) printf("%d %f\n", i, a_h[i]);
// Cleanup
free(a_h);
cudaFree(a_d);
}
so the others files are
main.cpp
#include<iostream>
#include "cud.h"
using namespace std;
int main()
{
cuda_calculation();
return 0;
}
cud.h
void cuda_calculation();
cu.h
void call(int , int ,float* , int );
cu.cu
#include <stdio.h>
#include "cu.h"
#include <cuda.h>
// Kernel that executes on the CUDA device
__global__ void square_array(float *a, int N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx<N) a[idx] = a[idx] * a[idx];
}
//}
void call(int a,int b,float* c,int d)
{
square_array <<< 3,4 >>> (c,d);
}
I have two typedef struct as shown below:
typedef struct{
UInt32 length;
void* data;
UInt16 value;
} my_type;
typedef struct{
UInt8 type;
UInt32 length;
void* value;
} tlv_t;
What I trying next is to allocate memory for an my_type struct, a tlv_t struct that is pointed to from the created my_type object and for a float number, which is pointed to from the tlv_t object.If I'm executing the code without the last line of the code below it is working well. I can store the value and I can access it.But as soon as I try to access it a second time the uploaded code isn't running at all anymore on the STM32F105 Contiki-based board. The odd part is that this is only the case when using floating point numbers. No problems at all with other datatypes like int. Unfortunately, I really need to use float... What am I doing wrong?
Another problem is that printf doesn't support some flags like %f or %ul. Does anybody know how to add support for it on Contiki?
my_type* t = malloc(sizeof(my_type));
t->data = malloc(sizeof(tlv_t));
tlv_t* tv = t->data;
tv->type = 10;
tv->length = sizeof(float);
tv->value = malloc(sizeof(float));
*(float*) tv->value = 212.32;
printf("tv->value: %i\n", (int) *(float*) tv->value);
printf("tv->value: %i\n", (int) *(float*) tv->value); // without this line it is working
EDIT:
I forgot to add these typedefs:
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef unsigned long UInt32;
EDIT2:
Here is the complete code:
#include <contiki.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cfs/cfs.h>
#include <cfs/cfs-coffee.h>
#include "arg.h"
/*---------------------------------------------------------------------------*/
PROCESS(main_process, "Contiki CLV build015_1");
AUTOSTART_PROCESSES(&main_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(main_process, ev, data)
{
PROCESS_BEGIN();
my_type* t = malloc(sizeof(my_type));
t->data = malloc(sizeof(tlv_t));
tlv_t* tv = t->data;
tv->type = 10;
tv->length = sizeof(float);
tv->value = malloc(sizeof(float));
*(float*) tv->value = 212.32;
printf("tv->value: %i\n", (int) *(float*) tv->value);
printf("tv->value: %i\n", (int) *(float*) tv->value); // without this line it is working
while (1) {
PROCESS_YIELD();
}
PROCESS_END();
}
EDIT3:
I'm using the latest arm-none-eabi-gcc (version 4_8-2013q4-20131204). Are there any known issues when dealing with structs, floats or memory management?
Try
PROCESS_THREAD(main_process, ev, data)
{
static my_type *t;
static tlv_t *tv;
static float f = 212.32;
PROCESS_BEGIN();
t = (my_type *)malloc(sizeof(my_type));
t->data = malloc(sizeof(tlv_t));
tv = (tlv_t *)t->data;
tv->type = 10;
tv->length = sizeof(float);
tv->value = malloc(sizeof(float));
//*(float *) tv->value = 212.32;
memmove(tv->value, &f, 4);
printf("tv->value: %i\n", (int) *(float*) tv->value);
printf("tv->value: %i\n", (int) *(float*) tv->value); // without this line it is working
printf("t address: %x \n", (unsigned int)t);
while (1) {
PROCESS_YIELD();
}
PROCESS_END();
}
I suggest you fix your code, so you get no compiler warnings anymore (don't turn them off). Add casts as needed.
After I did those fixes your code worked for me, so the code is ugly but ok.
#define UInt32 unsigned int
#define UInt16 unsigned short
#define UInt8 unsigned char
typedef struct{
UInt32 length;
void* data;
UInt16 value;
} my_type;
typedef struct{
UInt8 type;
UInt32 length;
void* value;
} tlv_t;
int _tmain(int argc, _TCHAR* argv[])
{
my_type* t = (my_type*)malloc(sizeof(my_type));
t->data = malloc(sizeof(tlv_t));
tlv_t* tv = (tlv_t*)t->data;
tv->type = 10;
tv->length = sizeof(float);
tv->value = malloc(sizeof(float));
*(float*) tv->value = (float)212.32;
printf("tv->value: %i\n", (int) *(float*) tv->value);
printf("tv->value: %i\n", (int) *(float*) tv->value); // without this line it
getchar();
}
gives
tv->value: 212
tv->value: 212
I'm trying to make my first native function with NDK and I'm in trouble with very basic stuff.
Please consider the following c code:
#include <jni.h>
#include <string.h>
JNIEXPORT jint JNICALL Java_eu_elevelcbt_sm_YCrCbUtils_toARGB(
JNIEnv* env, jbyteArray src, jintArray out, jint width, jint height){
jbyte *c_src = (*env)->GetByteArrayElements(env, src, NULL);
jint *c_out = (*env)->GetDirectBufferAddress(env, out);
if (c_out==NULL)
return -1;
int length = width * height;
int co;
unsigned int color;
for (co=0; co<length; co++) {
color = c_src[co] & 0xFF;
color = 0xFF000000 | (color<<16) | (color<<8) | color;
c_out[co] = color;
}
(*env)->ReleaseByteArrayElements(env, src, c_src, 0);
return 0;
}
JNIEXPORT jint JNICALL Java_eu_elevelcbt_sm_YCrCbUtils_sum(jint a, jint b){
return a+b;
}
and the following Java class:
public class YCrCbUtils {
public native int toARGB(byte[] src, int[] out, final int width, final int height);
public native int sum(int a, int b);
static {
System.loadLibrary("yuv");
}
}
Problem 1: If I call the second function
Log.v("DBG", "sum is: " + new YCrCbUtils().sum(10, 5));
This is what I get: "sum is 1079199776" !!!! WHY?!??!? :(
If I try calling first function like this:
int[] colors = new int[size.width * size.height]; // where width=800 and height=480
new YCrCbUtils().toARGB(data, colors, size.width, size.height); // data is a byte[]
I get a SIGSEGV error...
HELP please!!!
PS: my dev environment is MAC OSX Snow Leopard, NDK-r5b. My runtime env is Nexus One 2.3.3
...ok I'm stupid...
My methods signatures were WRONG... They always must have "JNIEnv* env, jobject obj" as first two members... well I spend an afternoon on this but the good thing is that now I'll never forget it!
Also, on my first method I had to change
jint *c_out = (*env)->GetDirectBufferAddress(env, out);
with
jint *c_out = (*env)->GetIntArrayElements(env, out, NULL);
as the previous one was returning a NULL pointer