Skip to content

Commit 2a62be5

Browse files
committed
pttc: Add support for nasm assembler
Replace yasm with nasm as the default assembler for pttc. Yasm is no longer actively maintained and has known security vulnerabilities, while nasm is actively developed and provides equivalent functionality. Key changes: 1. Update assembler invocation from 'yasm' to 'nasm' 2. Remove '-L nasm' option (nasm doesn't need this flag) 3. Adjust argv array indices after removing the flag 4. Support both yasm and nasm org directive formats: - yasm: [org 0x100000] - nasm: org 0x100000 5. Handle nasm's listing format which lacks %line directives by implementing 1:1 line mapping fallback for source correlation The changes maintain backward compatibility with existing .ptt test files while enabling nasm as the preferred assembler. Tested with: - test/src/loop-tnt.ptt - test/src/dump-all-packets.ptt Both tests generate valid PT traces that can be decoded with ptdump. Signed-off-by: Yogesh Tyagi <yogesh.tyagi@intel.com>
1 parent 63e1c17 commit 2a62be5

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

doc/howto_pttc.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Testing the Intel(R) Processor Trace (Intel PT) Decoder Library and Samples {#pt
3131
!-->
3232

3333
This chapter documents how to use the pttc tool to generate and run tests.
34-
Pttc takes a yasm assembly file and creates a Processor Trace stream from
34+
Pttc takes a nasm assembly file and creates a Processor Trace stream from
3535
special directives in its input.
3636

3737

@@ -49,7 +49,7 @@ directory:
4949
file-<tool>.exp
5050
file-<src>.sb
5151

52-
The `.lst` and `.bin` files are generated by a call to yasm. The `.pt` file
52+
The `.lst` and `.bin` files are generated by a call to nasm. The `.pt` file
5353
contains the Processor Trace and the `.exp` files contain the content of the
5454
comments after the `.exp` directive for tool `<tool>` (see below). The `.sb`
5555
files contain sideband infomrmation from source `<src>` (see below).
@@ -60,7 +60,7 @@ Pttc prints the filenames of the generated `.exp` and `.sb` files to stdout.
6060
Syntax
6161
------
6262

63-
Pttc allows annotations in the comments of yasm assembler source files. The
63+
Pttc allows annotations in the comments of nasm assembler source files. The
6464
parser recognizes all comments that contain the `@pt` directive marker.
6565

6666
Every pt directive can be preceded by a label name followed by a colon (`:`).

pttc/src/yasm.c

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static int lookup_section_vstart(struct label *l, char *line,
155155
}
156156

157157
static const char key_section[] = "[section";
158-
static const char key_org[] = "[org";
158+
static const char *key_org[] = {"[org", "org", NULL};
159159

160160
int parse_yasm_labels(struct label *l, const struct text *t)
161161
{
@@ -192,12 +192,33 @@ int parse_yasm_labels(struct label *l, const struct text *t)
192192
continue;
193193
}
194194

195-
tmp = strstr(line, key_org);
196-
if (tmp) {
195+
/* Try both yasm format "[org" and nasm format "org" */
196+
tmp = NULL;
197+
int org_style = -1;
198+
for (int j = 0; key_org[j] != NULL; j++) {
199+
tmp = strstr(line, key_org[j]);
200+
if (tmp) {
201+
org_style = j;
202+
break;
203+
}
204+
}
205+
206+
if (tmp && org_style >= 0) {
197207
char *org;
198208

199-
org = tmp + sizeof(key_org) - 1;
200-
tmp = strstr(org, "]");
209+
org = tmp + strlen(key_org[org_style]);
210+
/* For yasm format "[org", look for ] */
211+
if (org_style == 0) {
212+
tmp = strstr(org, "]");
213+
} else {
214+
/* For nasm, skip whitespace to find hex value */
215+
while (isspace(*org))
216+
org++;
217+
tmp = org;
218+
/* Find end of hex number */
219+
while (*tmp && !isspace(*tmp))
220+
tmp++;
221+
}
201222
if (!tmp)
202223
return -err_no_org_directive;
203224

@@ -720,18 +741,17 @@ struct yasm *yasm_alloc(const char *pttfile)
720741
static int yasm_run(struct yasm *y)
721742
{
722743
char *argv[] = {
723-
"yasm",
744+
"nasm",
724745
"<pttfile>",
725746
"-f", "bin",
726747
"-o", "<binfile>",
727-
"-L", "nasm",
728748
"-l", "<lstfile>",
729749
NULL,
730750
};
731751

732752
argv[1] = y->pttfile;
733753
argv[5] = y->binfile;
734-
argv[9] = y->lstfile;
754+
argv[7] = y->lstfile;
735755

736756
return run(argv[0], argv);
737757
}
@@ -825,9 +845,32 @@ static int yasm_advance_next_line(struct yasm *y)
825845
/* if line number or increment in the previous line
826846
* directive is <= 0, the current lst line has no
827847
* corresponding line in the source file.
848+
*
849+
* For nasm compatibility: if no %line directives have been
850+
* seen yet, assume 1:1 mapping with source file.
828851
*/
829-
if (y->st_asm->n <= 0 || y->st_asm->inc <= 0)
830-
continue;
852+
if (y->st_asm->n <= 0 || y->st_asm->inc <= 0) {
853+
/* If we haven't seen any %line directive, try to use
854+
* the source file directly with 1:1 line mapping.
855+
*/
856+
if (!y->st_asm->filename || y->st_asm->filename[0] == '\0') {
857+
/* Set to source .ptt file for first time */
858+
st_set_file(y->st_asm, y->pttfile, 1, 1);
859+
}
860+
861+
/* Calculate source line from listing line for nasm */
862+
asm_line = (int)y->lst_curr_line;
863+
864+
/* Read from source file at same line number */
865+
errcode = fl_getline(y->fl, s, (size_t) sizeof(s),
866+
y->st_asm->filename,
867+
(size_t) asm_line - 1u);
868+
if (errcode < 0)
869+
continue; /* Skip if can't read source line */
870+
871+
errcode = st_update(y->st_asm, s);
872+
break;
873+
}
831874

832875
/* finally the current line in the lst file can be
833876
* correlated to the source file, so we retrieve the

0 commit comments

Comments
 (0)