2

Hello I'm programming under Linux ( In C ).

When i use ptrace() to read data, it returns a word. In all the examples I see people using a long to read the input. Does a Long always have the same size of a word? I know that a word is the natural size with which a processor is handling data (the register size). But does that also apply to long's on different architectures etc?

 OValue_t outputValue;
 //.su_word is a long
 outputValue.su_word = ptrace(PTRACE_PEEKDATA,Process.ProcId,address,0); 
 printf("word  : %ld\n", outputValue.su_word);
 printf("int8 : %i\n", outputValue.su_int8);

EDIT: Thanks to Krzysztof Kosiński/unwind and the answer by Jonathan Leffler here I understand that ptrace returns a long and a long is big enough for a word.

http://docs.oracle.com/cd/E19620-01/805-3024/lp64-1/index.html

Community
  • 1
  • 1
Jona
  • 1,747
  • 2
  • 16
  • 22

3 Answers3

4

Examples use long since that's how the function is documented to work, the prototype is:

long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data);

There's even a note in the manual page saying:

The size of a "word" is determined by the operating-system variant (e.g., for 32-bit Linux it is 32 bits).

I think it just as well have been declared to return int, since int is supposed to be a platform's "natural" integer size, which I think is the "word size" for that platform in typical cases.

The function does not assume that the long has more precision than "a word", as far as I could tell from the manual page. It uses a return value of -1 to signal errors, but since that can of course be a valid value as well, requires you to also check errno.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Thank you, but in the manual it says 'Read a word at the address', so how can they assume a long is large enough to store a word on different platforms? – Jona Jan 07 '14 at 14:35
  • http://stackoverflow.com/questions/589575/size-of-int-long-etc Jonathan's answer gave me the answer – Jona Jan 07 '14 at 14:47
  • 1
    `int` is not equal to word size on x86-64 and most other 64-bit systems. – Krzysztof Kosiński Jan 07 '14 at 14:49
3

The Linux API defines ptrace to always return long.

long ptrace(enum __ptrace_request request, pid_t pid,
            void *addr, void *data);

On Linux, the size of long is equal to the machine word size (32-bit on 32-bit machines, 64-bit on 64-bit machines, and so on). As far as I know, this is true on all major architectures which have Linux ports.

Note that this is not true on Windows, where long is 32-bit even on x64 - but since ptrace is a Linux-specific call, you don't have to worry about it.

Krzysztof Kosiński
  • 4,225
  • 2
  • 18
  • 23
-2

The only rule is that a long must be at least the size of a word (int). Beyond that it's up to the machine architecture and the compiler writer. You could legally have char = short = int = long = long long.

Phil Perry
  • 2,126
  • 14
  • 18
  • 1
    `int` is not defined as equal to the word size, there is only a recommendation that it should be the fastest available integer type. In practice, `int` is 32-bit even on 64-bit machines, which means it is smaller than word size. – Krzysztof Kosiński Jan 07 '14 at 14:31