Skip to content

Commit

Permalink
JACOBIN-498 Initial commits for the trace system, which replaces prev…
Browse files Browse the repository at this point in the history
…ious logging functions (PR #244 from texadactyl)

JACOBIN-498 cli.go, cli_test.go, option_table_loader.go
  • Loading branch information
platypusguy authored Oct 24, 2024
2 parents 8c112ca + dba154c commit 22fd911
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 957 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
uses: actions/setup-java@main
with:
distribution: 'oracle'
java-version: '17'
java-version: '21'

- name: Build
run: |
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
uses: actions/setup-java@main
with:
distribution: 'oracle'
java-version: '17'
java-version: '21'

- name: Build
run: |
Expand Down
118 changes: 4 additions & 114 deletions src/classloader/classes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"io"
"jacobin/globals"
"jacobin/log"
"jacobin/stringPool"
"jacobin/types"
"os"
Expand All @@ -18,116 +17,10 @@ import (
"testing"
)

// test insertion of klass into the method area (called MethArea[])
func TestInsertValid(t *testing.T) {
// Testing the changes made as a result of JACOBIN-103
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.CLASS)

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

normalStdout := os.Stdout
_, wout, _ := os.Pipe()
os.Stdout = wout

MethArea = &sync.Map{}
currLen := MethAreaSize()
k := Klass{
Status: 0,
Loader: "",
Data: &ClData{},
}
k.Data.Name = "testClass"
k.Loader = "testLoader"
k.Status = 'F'
MethAreaInsert("TestEntry", &k)

newLen := MethAreaSize()
if newLen != currLen+1 {
t.Errorf("Expected post-insertion MethArea[] to have length of %d, got: %d",
currLen+1, newLen)
}

// restore stderr and stdout to what they were before
_ = w.Close()
out, _ := io.ReadAll(r)
os.Stderr = normalStderr

msg := string(out[:])

_ = wout.Close()
os.Stdout = normalStdout

if !strings.Contains(msg, "Method area insert: testClass, loader: testLoader") {
t.Errorf("Expecting log message containing 'testClass', got: %s", msg)
}
}

// TODO: This test does not appear to test what it contends. Further note:
// the coverage of the missing main() method is tested below and is the test
// responsible for code coverage of the missing main() method, not this one.
func TestInvalidLookupOfMethod_Test0(t *testing.T) {
// Testing the changes made as a result of JACOBIN-103
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.CLASS)

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

normalStdout := os.Stdout
_, wout, _ := os.Pipe()
os.Stdout = wout

MethArea = &sync.Map{}
currLen := MethAreaSize()
k := Klass{
Status: 0,
Loader: "",
Data: &ClData{},
}
k.Data.Name = "testClass"
k.Loader = ""
k.Status = 'F'
MethAreaInsert("TestEntry", &k)

newLen := MethAreaSize()
if newLen != currLen+1 {
t.Errorf("Expected post-insertion MethArea[] to have length of %d, got: %d",
currLen+1, newLen)
}

_, err := FetchMethodAndCP("TestEntry", "main", "([L)V")
if err == nil {
t.Errorf("Expecting an err msg for invalid MethAreaFetch in MTable, but got none")
}

// restore stderr and stdout to what they were before
_ = w.Close()
out, _ := io.ReadAll(r)
os.Stderr = normalStderr

msg := string(out[:])

_ = wout.Close()
os.Stdout = normalStdout

if !strings.Contains(msg, "Method area insert: testClass, loader:") {
t.Errorf("Expecting log message containing 'Class: testClass', got: %s", msg)
}
}

func TestInvalidLookupOfMethod_Test1(t *testing.T) {
// Testing the changes made as a result of JACOBIN-103
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.CLASS)
globals.TraceClass = true

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
Expand Down Expand Up @@ -179,8 +72,7 @@ func TestInvalidLookupOfMethod_Test1(t *testing.T) {
func TestInvalidLookupOfMethod_Test2(t *testing.T) {
// Testing the changes made as a result of JACOBIN-103
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.FINE)
globals.TraceClass = true

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
Expand Down Expand Up @@ -284,8 +176,7 @@ func TestFetchUTF8stringFromCPEntryNumber(t *testing.T) {
func TestInvalidMainMethod(t *testing.T) {
// Testing the changes made as a result of JACOBIN-103
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.FINE)
globals.TraceClass = true

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
Expand Down Expand Up @@ -326,8 +217,7 @@ func TestInvalidMainMethod(t *testing.T) {

func TestInvalidClassName(t *testing.T) {
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.FINE)
globals.TraceClass = true

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
Expand Down
5 changes: 4 additions & 1 deletion src/classloader/classloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"jacobin/log"
"jacobin/shutdown"
"jacobin/stringPool"
"jacobin/trace"
"jacobin/types"
"jacobin/util"
"os"
Expand Down Expand Up @@ -454,7 +455,9 @@ func ParseAndPostClass(cl *Classloader, filename string, rawBytes []byte) (uint3
_ = log.Log("ParseAndPostClass: error format-checking "+filename+". Exiting.", log.SEVERE)
return types.InvalidStringIndex, types.InvalidStringIndex, fmt.Errorf("format-checking error")
}
_ = log.Log("Class "+fullyParsedClass.className+" has been format-checked.", log.FINEST)
if globals.TraceClass {
trace.Trace("Class " + fullyParsedClass.className + " has been format-checked.")
}

// prepare the class for posting
classToPost := convertToPostableClass(&fullyParsedClass)
Expand Down
45 changes: 0 additions & 45 deletions src/classloader/classloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"jacobin/types"
"os"
"strings"
"sync"
"testing"
)

Expand Down Expand Up @@ -270,50 +269,6 @@ func TestMainClassFromInvalidJar(t *testing.T) {
}
}

func TestInsertionIntoMethodArea(t *testing.T) {
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.CLASS)

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

normalStdout := os.Stdout
_, wout, _ := os.Pipe()
os.Stdout = wout

MethArea = &sync.Map{}

k := Klass{}
k.Status = 'F'
k.Loader = "application"
clData := ClData{}
clData.Name = "WillyWonkaClass"
k.Data = &clData
MethAreaInsert("WillyWonkaClass", &k)

// restore stderr and stdout to what they were before
_ = w.Close()
out, _ := io.ReadAll(r)
os.Stderr = normalStderr

msg := string(out[:])

_ = wout.Close()
os.Stdout = normalStdout

if !strings.Contains(msg, "WillyWonkaClass") || !strings.Contains(msg, "application") {
t.Error("Got unexpected logging message for insertion of Klass into method area: " + msg)
}

if MethAreaSize() != 9 { // the 1 from here + 8 preloaded synthetic array classes
t.Errorf("Expecting method area to have a size of 9, got: %d",
MethAreaSize())
}
}

func TestInvalidMagicNumberViaParseAndPostFunction(t *testing.T) {

normalStderr := os.Stderr
Expand Down
14 changes: 0 additions & 14 deletions src/classloader/cpUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ package classloader
import (
"jacobin/frames"
"jacobin/globals"
"jacobin/log"
"jacobin/types"
"math"
"os"
"testing"
)

func TestMeInfoFromMethRefInvalid(t *testing.T) {
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.CLASS)

// set up a class with a constant pool containing entries
// that will fail the following tests
Expand Down Expand Up @@ -49,13 +45,6 @@ func TestMeInfoFromMethRefInvalid(t *testing.T) {

func TestMeInfoFromMethRefValid(t *testing.T) {
globals.InitGlobals("test")
log.Init()
log.SetLogLevel(log.WARNING)

// redirect stderr so as not to pollute the test output with the expected error message
normalStderr := os.Stderr
_, w, _ := os.Pipe()
os.Stderr = w

// Initialize classloaders and method area
err := Init()
Expand Down Expand Up @@ -100,9 +89,6 @@ func TestMeInfoFromMethRefValid(t *testing.T) {
t.Errorf("Expect to get a method: <init>()V, got %s%s", s2, s2)
}

// restore stderr
_ = w.Close()
os.Stderr = normalStderr
}

func TestGetClassNameFromCPclassref(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions src/classloader/methArea.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"jacobin/globals"
"jacobin/log"
"jacobin/stringPool"
"jacobin/trace"
"jacobin/types"
"sort"
"sync"
Expand Down Expand Up @@ -88,8 +89,10 @@ func MethAreaInsert(name string, klass *Klass) {
methAreaSize++
MethAreaMutex.Unlock()

if klass.Status == 'F' || klass.Status == 'V' || klass.Status == 'L' {
_ = log.Log("Method area insert: "+klass.Data.Name+", loader: "+klass.Loader, log.CLASS)
if globals.TraceClass {
if klass.Status == 'F' || klass.Status == 'V' || klass.Status == 'L' {
trace.Trace("Method area insert: " + klass.Data.Name + ", loader: " + klass.Loader)
}
}
}

Expand Down
22 changes: 1 addition & 21 deletions src/classloader/methodParser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ import (
// test a valid Code attribute of a method
func TestValidCodeMethodAttribute(t *testing.T) {
globals.InitGlobals("test")
log.Init()
_ = log.SetLogLevel(log.FINEST)

// redirect stderr & stdout to capture results from stderr
normalStderr := os.Stderr
_, w, _ := os.Pipe()
os.Stderr = w

normalStdout := os.Stdout
_, wout, _ := os.Pipe()
os.Stdout = wout

// variables we'll need.
klass := ParsedClass{}
Expand Down Expand Up @@ -70,16 +59,6 @@ func TestValidCodeMethodAttribute(t *testing.T) {
t.Error("Unexpected error in processing valid Exceptions attribute of method")
}

// restore stderr and stdout to what they were before
_ = w.Close()
// out, _ := io.ReadAll(r)
os.Stderr = normalStderr

// msg := string(out[:])

_ = wout.Close()
os.Stdout = normalStdout

if len(meth.codeAttr.code) != 2 {
t.Error("Expected code length of 2. Got: " + strconv.Itoa(len(meth.codeAttr.code)))
}
Expand All @@ -95,6 +74,7 @@ func TestValidCodeMethodAttribute(t *testing.T) {
t.Error("Expected 0 attributes of Code attribute. Got: " + strconv.Itoa(len(meth.codeAttr.attributes)))
}
}

func Test1ValidMethodExceptionsAttribute(t *testing.T) {
globals.InitGlobals("test")
log.Init()
Expand Down
6 changes: 6 additions & 0 deletions src/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ func InitGlobals(progName string) Globals {
FuncThrowException: fakeThrowEx,
}

TraceInit = false
TraceCloadi = false
TraceInst = false
TraceClass = false
TraceVerbose = false

// ----- String Pool and other values
InitStringPool()

Expand Down
Loading

0 comments on commit 22fd911

Please sign in to comment.